Debian Codegraph @

src/tests/modules/macros.rs

raw ◀ ▶
1 Debian patch touch this file
•1 use crate::{
2 doc,
3 oid::ObjectId,
4 spec::BinarySubtype,
5 tests::LOCK,
6 Binary,
7 Bson,
8 RawBson,
9 Regex,
10 Timestamp,
11 };
12 use pretty_assertions::assert_eq;
13
14 #[test]
15 fn standard_format() {
16 let _guard = LOCK.run_concurrently();
17 let id_string = "thisismyname";
18 let string_bytes: Vec<_> = id_string.bytes().collect();
19 let mut bytes = [0; 12];
20 bytes[..12].clone_from_slice(&string_bytes[..12]);
21
22 let id = ObjectId::from_bytes(bytes);
23 let date = time::OffsetDateTime::now_utc();
24
25 let doc = doc! {
26 "float": 2.4,
27 "string": "hello",
28 "array": ["testing", 1, true, [1, 2]],
29 "doc": {
30 "fish": "in",
31 "a": "barrel",
32 "!": 1,
33 },
34 "bool": true,
35 "null": null,
36 "regexp": Bson::RegularExpression(Regex { pattern: "s[ao]d".to_owned(), options: "i".to_owned() }),
37 "with_wrapped_parens": (-20),
38 "code": Bson::JavaScriptCode("function(x) { return x._id; }".to_owned()),
39 "i32": 12,
40 "i64": -55,
41 "timestamp": Bson::Timestamp(Timestamp { time: 0, increment: 229_999_444 }),
42 "binary": Binary { subtype: BinarySubtype::Md5, bytes: "thingies".to_owned().into_bytes() },
43 "encrypted": Binary { subtype: BinarySubtype::Encrypted, bytes: "secret".to_owned().into_bytes() },
44 "_id": id,
45 "date": Bson::DateTime(crate::DateTime::from_time_0_3(date)),
46 };
47
48 let rawdoc = rawdoc! {
49 "float": 2.4,
50 "string": "hello",
51 "array": ["testing", 1, true, [1, 2]],
52 "doc": {
53 "fish": "in",
54 "a": "barrel",
55 "!": 1,
56 },
57 "bool": true,
58 "null": null,
59 "regexp": Regex { pattern: "s[ao]d".to_owned(), options: "i".to_owned() },
60 "with_wrapped_parens": (-20),
61 "code": RawBson::JavaScriptCode("function(x) { return x._id; }".to_owned()),
62 "i32": 12,
63 "i64": -55,
64 "timestamp": Timestamp { time: 0, increment: 229_999_444 },
65 "binary": Binary { subtype: BinarySubtype::Md5, bytes: "thingies".to_owned().into_bytes() },
66 "encrypted": Binary { subtype: BinarySubtype::Encrypted, bytes: "secret".to_owned().into_bytes() },
67 "_id": id,
68 "date": crate::DateTime::from_time_0_3(date),
69 };
70
71 let ts_nanos = date.unix_timestamp_nanos();
72 let ts_millis = ts_nanos - (ts_nanos % 1_000_000);
73 let date_trunc = time::OffsetDateTime::from_unix_timestamp_nanos(ts_millis).unwrap();
74 let expected = format!(
75 "{{ \"float\": 2.4, \"string\": \"hello\", \"array\": [\"testing\", 1, true, [1, 2]], \
76 \"doc\": {{ \"fish\": \"in\", \"a\": \"barrel\", \"!\": 1 }}, \"bool\": true, \"null\": \
77 null, \"regexp\": /s[ao]d/i, \"with_wrapped_parens\": -20, \"code\": function(x) {{ \
78 return x._id; }}, \"i32\": 12, \"i64\": -55, \"timestamp\": Timestamp(0, 229999444), \
79 \"binary\": Binary(0x5, {}), \"encrypted\": Binary(0x6, {}), \"_id\": ObjectId(\"{}\"), \
80 \"date\": DateTime(\"{}\") }}",
81 base64::encode("thingies"),
82 base64::encode("secret"),
83 hex::encode(id_string),
84 date_trunc,
85 );
86
87 assert_eq!(expected, format!("{}", doc));
88
89 assert_eq!(rawdoc.into_bytes(), crate::to_vec(&doc).unwrap());
90 }
91
92 #[test]
93 fn non_trailing_comma() {
94 let _guard = LOCK.run_concurrently();
95 let doc = doc! {
96 "a": "foo",
97 "b": { "ok": "then" }
98 };
99
100 let expected = "{ \"a\": \"foo\", \"b\": { \"ok\": \"then\" } }".to_string();
101 assert_eq!(expected, format!("{}", doc));
102 }
103
104 #[test]
105 #[allow(clippy::float_cmp)]
106 fn recursive_macro() {
107 let _guard = LOCK.run_concurrently();
108 let doc = doc! {
109 "a": "foo",
110 "b": {
111 "bar": {
112 "harbor": ["seal", false],
113 "jelly": 42.0,
114 },
115 "grape": 27,
116 },
117 "c": [-7],
118 "d": [
119 {
120 "apple": "ripe",
121 }
122 ],
123 "e": { "single": "test" },
124 "n": (Bson::Null),
125 };
126 let rawdoc = rawdoc! {
127 "a": "foo",
128 "b": {
129 "bar": {
130 "harbor": ["seal", false],
131 "jelly": 42.0,
132 },
133 "grape": 27,
134 },
135 "c": [-7],
136 "d": [
137 {
138 "apple": "ripe",
139 }
140 ],
141 "e": { "single": "test" },
142 "n": (RawBson::Null),
143 };
144
145 match doc.get("a") {
146 Some(Bson::String(s)) => assert_eq!("foo", s),
147 _ => panic!("String 'foo' was not inserted correctly."),
148 }
149
150 // Inner Doc 1
151 match doc.get("b") {
152 Some(Bson::Document(doc)) => {
153 // Inner doc 2
154 match doc.get("bar") {
155 Some(Bson::Document(inner_doc)) => {
156 // Inner array
157 match inner_doc.get("harbor") {
158 Some(Bson::Array(arr)) => {
159 assert_eq!(2, arr.len());
160
161 // Match array items
162 match arr.first() {
163 Some(Bson::String(ref s)) => assert_eq!("seal", s),
164 _ => panic!(
165 "String 'seal' was not inserted into inner array correctly."
166 ),
167 }
168 match arr.get(1) {
169 Some(Bson::Boolean(ref b)) => assert!(!b),
170 _ => panic!(
171 "Bool 'false' was not inserted into inner array correctly."
172 ),
173 }
174 }
175 _ => panic!("Inner array was not inserted correctly."),
176 }
177
178 // Inner floating point
179 match inner_doc.get("jelly") {
180 Some(Bson::Double(fp)) => assert_eq!(42.0, *fp),
181 _ => panic!("Floating point 42.0 was not inserted correctly."),
182 }
183 }
184 _ => panic!("Second inner document was not inserted correctly."),
185 }
186 }
187 _ => panic!("Inner document was not inserted correctly."),
188 }
189
190 // Single-item array
191 match doc.get("c") {
192 Some(Bson::Array(arr)) => {
193 assert_eq!(1, arr.len());
194
195 // Integer type
196 match arr.first() {
197 Some(Bson::Int32(ref i)) => assert_eq!(-7, *i),
198 _ => panic!("I32 '-7' was not inserted correctly."),
199 }
200 }
201 _ => panic!("Single-item array was not inserted correctly."),
202 }
203
204 // Document nested in array
205 match doc.get("d") {
206 Some(Bson::Array(arr)) => {
207 assert_eq!(1, arr.len());
208
209 // Nested document
210 match arr.first() {
211 Some(Bson::Document(ref doc)) => {
212 // String
213 match doc.get("apple") {
214 Some(Bson::String(s)) => assert_eq!("ripe", s),
215 _ => panic!("String 'ripe' was not inserted correctly."),
216 }
217 }
218 _ => panic!("Document was not inserted into array correctly."),
219 }
220 }
221 _ => panic!("Array was not inserted correctly."),
222 }
223
224 // Single-item document
225 match doc.get("e") {
226 Some(Bson::Document(bdoc)) => {
227 // String
228 match bdoc.get("single") {
229 Some(Bson::String(s)) => assert_eq!("test", s),
230 _ => panic!("String 'test' was not inserted correctly."),
231 }
232 }
233 _ => panic!("Single-item document was not inserted correctly."),
234 }
235
236 match doc.get("n") {
237 Some(&Bson::Null) => {
238 // It was null
239 }
240 _ => panic!("Null was not inserted correctly."),
241 }
242
243 assert_eq!(rawdoc.into_bytes(), crate::to_vec(&doc).unwrap());
244 }
245
246 #[test]
247 #[allow(clippy::from_over_into)]
248 fn can_use_macro_with_into_bson() {
249 struct Custom;
250
251 impl Into<Bson> for Custom {
252 fn into(self) -> Bson {
253 "foo".into()
254 }
255 }
256
257 impl Into<RawBson> for Custom {
258 fn into(self) -> RawBson {
259 "foo".into()
260 }
261 }
262
263 _ = bson!({
264 "a": Custom,
265 });
266 _ = doc! {
267 "a": Custom,
268 };
269 _ = rawbson!({
270 "a": Custom,
271 });
272 _ = rawdoc! {
273 "a": Custom,
274 };
275 }