Debian Codegraph @

src/raw/test/mod.rs

raw
1 mod append;
2 mod props;
3
4 use super::*;
5 use crate::{
6 doc,
7 oid::ObjectId,
8 raw::error::ValueAccessErrorKind,
9 spec::BinarySubtype,
10 Binary,
11 Bson,
12 DateTime,
13 Regex,
14 Timestamp,
15 };
16
17 #[test]
18 fn string_from_document() {
19 let rawdoc = rawdoc! {
20 "this": "first",
21 "that": "second",
22 "something": "else",
23 };
24 assert_eq!(
25 rawdoc.get("that").unwrap().unwrap().as_str().unwrap(),
26 "second",
27 );
28 }
29
30 #[test]
31 fn nested_document() {
32 let rawdoc = rawdoc! {
33 "outer": {
34 "inner": "surprise",
35 "i64": 6_i64
36 }
37 };
38 let subdoc = rawdoc
39 .get("outer")
40 .expect("get doc result")
41 .expect("get doc option")
42 .as_document()
43 .expect("as doc");
44 assert_eq!(
45 subdoc
46 .get("inner")
47 .expect("get str result")
48 .expect("get str option")
49 .as_str()
50 .expect("as str"),
51 "surprise",
52 );
53
54 assert_eq!(
55 subdoc
56 .get("i64")
57 .expect("get i64 result")
58 .expect("get i64 option")
59 .as_i64()
60 .expect("as i64 result"),
61 6
62 );
63 }
64
65 #[test]
66 fn iterate() {
67 let rawdoc = rawdoc! {
68 "apples": "oranges",
69 "peanut butter": "chocolate",
70 "easy as": {"do": 1, "re": 2, "mi": 3},
71 };
72 let mut dociter = rawdoc.into_iter();
73 let next = dociter.next().expect("no result").expect("invalid bson");
74 assert_eq!(next.0, "apples");
75 assert_eq!(next.1.as_str().expect("result was not a str"), "oranges");
76 let next = dociter.next().expect("no result").expect("invalid bson");
77 assert_eq!(next.0, "peanut butter");
78 assert_eq!(next.1.as_str().expect("result was not a str"), "chocolate");
79 let next = dociter.next().expect("no result").expect("invalid bson");
80 assert_eq!(next.0, "easy as");
81 let _doc = next.1.as_document().expect("result was a not a document");
82 let next = dociter.next();
83 assert!(next.is_none());
84 }
85
86 #[test]
87 fn rawdoc_to_doc() {
88 let rawdoc = rawdoc! {
89 "f64": 2.5,
90 "string": "hello",
91 "document": {},
92 "array": ["binary", "serialized", "object", "notation"],
93 "binary": Binary { subtype: BinarySubtype::Generic, bytes: vec![1, 2, 3] },
94 "object_id": ObjectId::from_bytes([1, 2, 3, 4, 5,6,7,8,9,10, 11,12]),
95 "boolean": true,
96 "datetime": DateTime::now(),
97 "null": RawBson::Null,
98 "regex": Regex { pattern: String::from(r"end\s*$"), options: String::from("i")},
99 "javascript": RawBson::JavaScriptCode(String::from("console.log(console);")),
100 "symbol": RawBson::Symbol(String::from("artist-formerly-known-as")),
101 "javascript_with_scope": RawJavaScriptCodeWithScope {
102 code: String::from("console.log(msg);"),
103 scope: rawdoc! { "ok": true }
104 },
105 "int32": 23i32,
106 "timestamp": Timestamp { time: 3542578, increment: 0 },
107 "int64": 46i64,
108 "end": "END",
109 };
110
111 let doc: crate::Document = rawdoc.clone().try_into().expect("invalid bson");
112 let round_tripped_bytes = crate::to_vec(&doc).expect("serialize should work");
113 assert_eq!(round_tripped_bytes.as_slice(), rawdoc.as_bytes());
114
115 let mut vec_writer_bytes = vec![];
116 doc.to_writer(&mut vec_writer_bytes)
117 .expect("to writer should work");
118 assert_eq!(vec_writer_bytes, rawdoc.into_bytes());
119 }
120
121 #[test]
122 fn f64() {
123 #![allow(clippy::float_cmp)]
124
125 let rawdoc = rawdoc! { "f64": 2.5 };
126 assert_eq!(
127 rawdoc
128 .get("f64")
129 .expect("error finding key f64")
130 .expect("no key f64")
131 .as_f64()
132 .expect("result was not a f64"),
133 2.5,
134 );
135 }
136
137 #[test]
138 fn string() {
139 let rawdoc = rawdoc! { "string": "hello" };
140
141 assert_eq!(
142 rawdoc
143 .get("string")
144 .expect("error finding key string")
145 .expect("no key string")
146 .as_str()
147 .expect("result was not a string"),
148 "hello",
149 );
150 }
151
152 #[test]
153 fn document() {
154 let rawdoc = rawdoc! {"document": {}};
155
156 let doc = rawdoc
157 .get("document")
158 .expect("error finding key document")
159 .expect("no key document")
160 .as_document()
161 .expect("result was not a document");
162 assert_eq!(doc.as_bytes(), [5u8, 0, 0, 0, 0].as_ref()); // Empty document
163 }
164
165 #[test]
166 fn array() {
167 let rawdoc = rawdoc! { "array": ["binary", "serialized", "object", "notation"] };
168 let array = rawdoc
169 .get("array")
170 .expect("error finding key array")
171 .expect("no key array")
172 .as_array()
173 .expect("result was not an array");
174 assert_eq!(array.get_str(0), Ok("binary"));
175 assert_eq!(array.get_str(3), Ok("notation"));
176 assert_eq!(
177 array.get_str(4).unwrap_err().kind,
178 ValueAccessErrorKind::NotPresent
179 );
180 }
181
182 #[test]
183 fn binary() {
184 let rawdoc = rawdoc! {
185 "binary": Binary { subtype: BinarySubtype::Generic, bytes: vec![1u8, 2, 3] }
186 };
187 let binary: bson_ref::RawBinaryRef<'_> = rawdoc
188 .get("binary")
189 .expect("error finding key binary")
190 .expect("no key binary")
191 .as_binary()
192 .expect("result was not a binary object");
193 assert_eq!(binary.subtype, BinarySubtype::Generic);
194 assert_eq!(binary.bytes, &[1, 2, 3]);
195 }
196
197 #[test]
198 fn object_id() {
199 let rawdoc = rawdoc! {
200 "object_id": ObjectId::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
201 };
202 let oid = rawdoc
203 .get("object_id")
204 .expect("error finding key object_id")
205 .expect("no key object_id")
206 .as_object_id()
207 .expect("result was not an object id");
208 assert_eq!(oid.to_hex(), "0102030405060708090a0b0c");
209 }
210
211 #[test]
212 fn boolean() {
213 let rawdoc = rawdoc! {
214 "boolean": true,
215 };
216
217 let boolean = rawdoc
218 .get("boolean")
219 .expect("error finding key boolean")
220 .expect("no key boolean")
221 .as_bool()
222 .expect("result was not boolean");
223
224 assert!(boolean);
225 }
226
227 #[test]
228 fn datetime() {
229 use time::macros::datetime;
230
231 let rawdoc = rawdoc! {
232 "boolean": true,
233 "datetime": DateTime::from_time_0_3(datetime!(2000-10-31 12:30:45 UTC)),
234 };
235 let datetime = rawdoc
236 .get("datetime")
237 .expect("error finding key datetime")
238 .expect("no key datetime")
239 .as_datetime()
240 .expect("result was not datetime");
241 assert_eq!(
242 datetime.try_to_rfc3339_string().unwrap(),
243 "2000-10-31T12:30:45Z"
244 );
245 }
246
247 #[test]
248 fn null() {
249 let rawdoc = rawdoc! {
250 "null": null,
251 };
252 rawdoc
253 .get("null")
254 .expect("error finding key null")
255 .expect("no key null")
256 .as_null()
257 .expect("was not null");
258 }
259
260 #[test]
261 fn regex() {
262 let rawdoc = rawdoc! {
263 "regex": Regex { pattern: String::from(r"end\s*$"), options: String::from("i")},
264 };
265 let regex = rawdoc
266 .get("regex")
267 .expect("error finding key regex")
268 .expect("no key regex")
269 .as_regex()
270 .expect("was not regex");
271 assert_eq!(regex.pattern, r"end\s*$");
272 assert_eq!(regex.options, "i");
273 }
274 #[test]
275 fn javascript() {
276 let rawdoc = rawdoc! {
277 "javascript": RawBson::JavaScriptCode(String::from("console.log(console);")),
278 };
279 let js = rawdoc
280 .get("javascript")
281 .expect("error finding key javascript")
282 .expect("no key javascript")
283 .as_javascript()
284 .expect("was not javascript");
285 assert_eq!(js, "console.log(console);");
286 }
287
288 #[test]
289 fn symbol() {
290 let rawdoc = rawdoc! {
291 "symbol": RawBson::Symbol(String::from("artist-formerly-known-as")),
292 };
293
294 let symbol = rawdoc
295 .get("symbol")
296 .expect("error finding key symbol")
297 .expect("no key symbol")
298 .as_symbol()
299 .expect("was not symbol");
300 assert_eq!(symbol, "artist-formerly-known-as");
301 }
302
303 #[test]
304 fn javascript_with_scope() {
305 let rawdoc = rawdoc! {
306 "javascript_with_scope": RawJavaScriptCodeWithScope {
307 code: String::from("console.log(msg);"),
308 scope: rawdoc! { "ok": true }
309 },
310 };
311 let js_with_scope = rawdoc
312 .get("javascript_with_scope")
313 .expect("error finding key javascript_with_scope")
314 .expect("no key javascript_with_scope")
315 .as_javascript_with_scope()
316 .expect("was not javascript with scope");
317 assert_eq!(js_with_scope.code, "console.log(msg);");
318 let (scope_key, scope_value_bson) = js_with_scope
319 .scope
320 .into_iter()
321 .next()
322 .expect("no next value in scope")
323 .expect("invalid element");
324 assert_eq!(scope_key, "ok");
325 let scope_value = scope_value_bson.as_bool().expect("not a boolean");
326 assert!(scope_value);
327 }
328
329 #[test]
330 fn int32() {
331 let rawdoc = rawdoc! {
332 "int32": 23i32,
333 };
334 let int32 = rawdoc
335 .get("int32")
336 .expect("error finding key int32")
337 .expect("no key int32")
338 .as_i32()
339 .expect("was not int32");
340 assert_eq!(int32, 23i32);
341 }
342
343 #[test]
344 fn timestamp() {
345 let rawdoc = rawdoc! {
346 "timestamp": Timestamp { time: 3542578, increment: 7 },
347 };
348 let ts = rawdoc
349 .get("timestamp")
350 .expect("error finding key timestamp")
351 .expect("no key timestamp")
352 .as_timestamp()
353 .expect("was not a timestamp");
354
355 assert_eq!(ts.increment, 7);
356 assert_eq!(ts.time, 3542578);
357 }
358
359 #[test]
360 fn int64() {
361 let rawdoc = rawdoc! {
362 "int64": 46i64,
363 };
364 let int64 = rawdoc
365 .get("int64")
366 .expect("error finding key int64")
367 .expect("no key int64")
368 .as_i64()
369 .expect("was not int64");
370 assert_eq!(int64, 46i64);
371 }
372 #[test]
373 fn document_iteration() {
374 let rawdoc = rawdoc! {
375 "f64": 2.5,
376 "string": "hello",
377 "document": {},
378 "array": ["binary", "serialized", "object", "notation"],
379 "binary": Binary { subtype: BinarySubtype::Generic, bytes: vec![1u8, 2, 3] },
380 "object_id": ObjectId::from_bytes([1, 2, 3, 4, 5,6,7,8,9,10, 11,12]),
381 "boolean": true,
382 "datetime": DateTime::now(),
383 "null": RawBson::Null,
384 "regex": Regex { pattern: String::from(r"end\s*$"), options: String::from("i") },
385 "javascript": RawBson::JavaScriptCode(String::from("console.log(console);")),
386 "symbol": RawBson::Symbol(String::from("artist-formerly-known-as")),
387 "javascript_with_scope": RawJavaScriptCodeWithScope {
388 code: String::from("console.log(msg);"),
389 scope: rawdoc! { "ok": true }
390 },
391 "int32": 23i32,
392 "timestamp": Timestamp { time: 3542578, increment: 0 },
393 "int64": 46i64,
394 "end": "END",
395 };
396
397 assert_eq!(
398 rawdoc
399 .into_iter()
400 .collect::<Result<Vec<(&str, _)>>>()
401 .expect("collecting iterated doc")
402 .len(),
403 17
404 );
405 let end = rawdoc
406 .get("end")
407 .expect("error finding key end")
408 .expect("no key end")
409 .as_str()
410 .expect("was not str");
411 assert_eq!(end, "END");
412 }
413
414 #[test]
415 fn into_bson_conversion() {
416 let rawdoc = rawdoc! {
417 "f64": 2.5,
418 "string": "hello",
419 "document": {},
420 "array": ["binary", "serialized", "object", "notation"],
421 "object_id": ObjectId::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
422 "binary": Binary { subtype: BinarySubtype::Generic, bytes: vec![1u8, 2, 3] },
423 "boolean": false,
424 };
425 let rawbson = RawBsonRef::Document(RawDocument::from_bytes(rawdoc.as_bytes()).unwrap());
426 let b: Bson = rawbson.try_into().expect("invalid bson");
427 let doc = b.as_document().expect("not a document");
428 assert_eq!(*doc.get("f64").expect("f64 not found"), Bson::Double(2.5));
429 assert_eq!(
430 *doc.get("string").expect("string not found"),
431 Bson::String(String::from("hello"))
432 );
433 assert_eq!(
434 *doc.get("document").expect("document not found"),
435 Bson::Document(doc! {})
436 );
437 assert_eq!(
438 *doc.get("array").expect("array not found"),
439 Bson::Array(
440 vec!["binary", "serialized", "object", "notation"]
441 .into_iter()
442 .map(|s| Bson::String(String::from(s)))
443 .collect()
444 )
445 );
446 assert_eq!(
447 *doc.get("object_id").expect("object_id not found"),
448 Bson::ObjectId(ObjectId::from_bytes([
449 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
450 ]))
451 );
452 assert_eq!(
453 *doc.get("binary").expect("binary not found"),
454 Bson::Binary(Binary {
455 subtype: BinarySubtype::Generic,
456 bytes: vec![1, 2, 3]
457 })
458 );
459 assert_eq!(
460 *doc.get("boolean").expect("boolean not found"),
461 Bson::Boolean(false)
462 );
463 }
464
465 use props::arbitrary_bson;
466 use proptest::prelude::*;
467 use std::convert::TryInto;
468
469 proptest! {
470 #[test]
471 fn no_crashes(s: Vec<u8>) {
472 let _ = RawDocumentBuf::from_bytes(s);
473 }
474
475 #[test]
476 fn roundtrip_bson(bson in arbitrary_bson()) {
477 let doc = doc! { "bson": bson };
478 let raw = crate::to_vec(&doc);
479 prop_assert!(raw.is_ok());
480 let raw = RawDocumentBuf::from_bytes(raw.unwrap());
481 prop_assert!(raw.is_ok());
482 let raw = raw.unwrap();
483 let roundtrip: Result<crate::Document> = raw.try_into();
484 prop_assert!(roundtrip.is_ok());
485 let roundtrip = roundtrip.unwrap();
486 prop_assert_eq!(doc, roundtrip);
487 }
488 }