Skip to content

Commit 99b50d0

Browse files
RUST-596 Fix and run serde tests (#219)
1 parent 8d8cffe commit 99b50d0

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod modules;
2+
mod serde;
23
mod spec;
34

45
use lazy_static::lazy_static;

src/tests/serde.rs

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#![allow(clippy::blacklisted_name)]
22

3-
use bson::{bson, doc, spec::BinarySubtype, Binary, Bson, Deserializer, Serializer};
3+
use crate::{
4+
bson,
5+
compat::u2f,
6+
doc,
7+
from_bson,
8+
spec::BinarySubtype,
9+
tests::LOCK,
10+
to_bson,
11+
Binary,
12+
Bson,
13+
Deserializer,
14+
Serializer,
15+
};
416
use serde::{Deserialize, Serialize};
517
use serde_json::json;
618

@@ -75,13 +87,13 @@ fn test_ser_timestamp() {
7587
},
7688
};
7789

78-
let x = bson::to_bson(&foo).unwrap();
90+
let x = to_bson(&foo).unwrap();
7991
assert_eq!(
8092
x.as_document().unwrap(),
8193
&doc! { "ts": Bson::Timestamp(Timestamp { time: 0x0000_000C, increment: 0x0000_000A }) }
8294
);
8395

84-
let xfoo: Foo = bson::from_bson(x).unwrap();
96+
let xfoo: Foo = from_bson(x).unwrap();
8597
assert_eq!(xfoo, foo);
8698
}
8799

@@ -95,7 +107,7 @@ fn test_de_timestamp() {
95107
ts: Timestamp,
96108
}
97109

98-
let foo: Foo = bson::from_bson(Bson::Document(doc! {
110+
let foo: Foo = from_bson(Bson::Document(doc! {
99111
"ts": Bson::Timestamp(Timestamp { time: 0x0000_000C, increment: 0x0000_000A }),
100112
}))
101113
.unwrap();
@@ -128,13 +140,13 @@ fn test_ser_regex() {
128140
regex: regex.clone(),
129141
};
130142

131-
let x = bson::to_bson(&foo).unwrap();
143+
let x = to_bson(&foo).unwrap();
132144
assert_eq!(
133145
x.as_document().unwrap(),
134146
&doc! { "regex": Bson::RegularExpression(regex) }
135147
);
136148

137-
let xfoo: Foo = bson::from_bson(x).unwrap();
149+
let xfoo: Foo = from_bson(x).unwrap();
138150
assert_eq!(xfoo, foo);
139151
}
140152

@@ -153,7 +165,7 @@ fn test_de_regex() {
153165
options: "01".into(),
154166
};
155167

156-
let foo: Foo = bson::from_bson(Bson::Document(doc! {
168+
let foo: Foo = from_bson(Bson::Document(doc! {
157169
"regex": Bson::RegularExpression(regex.clone()),
158170
}))
159171
.unwrap();
@@ -180,13 +192,13 @@ fn test_ser_code_with_scope() {
180192
code_with_scope: code_with_scope.clone(),
181193
};
182194

183-
let x = bson::to_bson(&foo).unwrap();
195+
let x = to_bson(&foo).unwrap();
184196
assert_eq!(
185197
x.as_document().unwrap(),
186198
&doc! { "code_with_scope": Bson::JavaScriptCodeWithScope(code_with_scope) }
187199
);
188200

189-
let xfoo: Foo = bson::from_bson(x).unwrap();
201+
let xfoo: Foo = from_bson(x).unwrap();
190202
assert_eq!(xfoo, foo);
191203
}
192204

@@ -205,7 +217,7 @@ fn test_de_code_with_scope() {
205217
scope: doc! { "x": 12 },
206218
};
207219

208-
let foo: Foo = bson::from_bson(Bson::Document(doc! {
220+
let foo: Foo = from_bson(Bson::Document(doc! {
209221
"code_with_scope": Bson::JavaScriptCodeWithScope(code_with_scope.clone()),
210222
}))
211223
.unwrap();
@@ -234,13 +246,13 @@ fn test_ser_datetime() {
234246
date: From::from(now),
235247
};
236248

237-
let x = bson::to_bson(&foo).unwrap();
249+
let x = to_bson(&foo).unwrap();
238250
assert_eq!(
239251
x.as_document().unwrap(),
240252
&doc! { "date": (Bson::DateTime(now)) }
241253
);
242254

243-
let xfoo: Foo = bson::from_bson(x).unwrap();
255+
let xfoo: Foo = from_bson(x).unwrap();
244256
assert_eq!(xfoo, foo);
245257
}
246258

@@ -249,15 +261,15 @@ fn test_compat_u2f() {
249261
let _guard = LOCK.run_concurrently();
250262
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
251263
struct Foo {
252-
#[serde(with = "bson::compat::u2f")]
264+
#[serde(with = "u2f")]
253265
x: u32,
254266
}
255267

256268
let foo = Foo { x: 20 };
257-
let b = bson::to_bson(&foo).unwrap();
269+
let b = to_bson(&foo).unwrap();
258270
assert_eq!(b, Bson::Document(doc! { "x": (Bson::Double(20.0)) }));
259271

260-
let de_foo = bson::from_bson::<Foo>(b).unwrap();
272+
let de_foo = from_bson::<Foo>(b).unwrap();
261273
assert_eq!(de_foo, foo);
262274
}
263275

@@ -276,13 +288,13 @@ fn test_binary_generic_roundtrip() {
276288
}),
277289
};
278290

279-
let b = bson::to_bson(&x).unwrap();
291+
let b = to_bson(&x).unwrap();
280292
assert_eq!(
281293
b.as_document().unwrap(),
282294
&doc! {"data": Bson::Binary(Binary { subtype: BinarySubtype::Generic, bytes: b"12345abcde".to_vec() })}
283295
);
284296

285-
let f = bson::from_bson::<Foo>(b).unwrap();
297+
let f = from_bson::<Foo>(b).unwrap();
286298
assert_eq!(x, f);
287299
}
288300

@@ -301,13 +313,13 @@ fn test_binary_non_generic_roundtrip() {
301313
}),
302314
};
303315

304-
let b = bson::to_bson(&x).unwrap();
316+
let b = to_bson(&x).unwrap();
305317
assert_eq!(
306318
b.as_document().unwrap(),
307319
&doc! {"data": Bson::Binary(Binary { subtype: BinarySubtype::BinaryOld, bytes: b"12345abcde".to_vec() })}
308320
);
309321

310-
let f = bson::from_bson::<Foo>(b).unwrap();
322+
let f = from_bson::<Foo>(b).unwrap();
311323
assert_eq!(x, f);
312324
}
313325

@@ -326,13 +338,13 @@ fn test_binary_helper_generic_roundtrip() {
326338
},
327339
};
328340

329-
let b = bson::to_bson(&x).unwrap();
341+
let b = to_bson(&x).unwrap();
330342
assert_eq!(
331343
b.as_document().unwrap(),
332344
&doc! {"data": Bson::Binary(Binary { subtype: BinarySubtype::Generic, bytes: b"12345abcde".to_vec() })}
333345
);
334346

335-
let f = bson::from_bson::<Foo>(b).unwrap();
347+
let f = from_bson::<Foo>(b).unwrap();
336348
assert_eq!(x, f);
337349
}
338350

@@ -351,13 +363,13 @@ fn test_binary_helper_non_generic_roundtrip() {
351363
},
352364
};
353365

354-
let b = bson::to_bson(&x).unwrap();
366+
let b = to_bson(&x).unwrap();
355367
assert_eq!(
356368
b.as_document().unwrap(),
357369
&doc! {"data": Bson::Binary(Binary { subtype: BinarySubtype::BinaryOld, bytes: b"12345abcde".to_vec() })}
358370
);
359371

360-
let f = bson::from_bson::<Foo>(b).unwrap();
372+
let f = from_bson::<Foo>(b).unwrap();
361373
assert_eq!(x, f);
362374
}
363375

@@ -374,11 +386,11 @@ fn test_byte_vec() {
374386
challenge: b"18762b98b7c34c25bf9dc3154e4a5ca3",
375387
};
376388

377-
let b = bson::to_bson(&x).unwrap();
389+
let b = to_bson(&x).unwrap();
378390
assert_eq!(
379391
b,
380392
Bson::Document(
381-
doc! { "challenge": (Bson::Binary(Binary { subtype: bson::spec::BinarySubtype::Generic, bytes: x.challenge.to_vec() }))}
393+
doc! { "challenge": (Bson::Binary(Binary { subtype: BinarySubtype::Generic, bytes: x.challenge.to_vec() }))}
382394
)
383395
);
384396

@@ -402,13 +414,13 @@ fn test_serde_bytes() {
402414
data: b"12345abcde".to_vec(),
403415
};
404416

405-
let b = bson::to_bson(&x).unwrap();
417+
let b = to_bson(&x).unwrap();
406418
assert_eq!(
407419
b.as_document().unwrap(),
408-
&doc! {"data": Bson::Binary(Binary { subtype: bson::spec::BinarySubtype::Generic, bytes: b"12345abcde".to_vec() })}
420+
&doc! {"data": Bson::Binary(Binary { subtype: BinarySubtype::Generic, bytes: b"12345abcde".to_vec() })}
409421
);
410422

411-
let f = bson::from_bson::<Foo>(b).unwrap();
423+
let f = from_bson::<Foo>(b).unwrap();
412424
assert_eq!(x, f);
413425
}
414426

@@ -419,12 +431,12 @@ fn test_serde_newtype_struct() {
419431
struct Email(String);
420432

421433
let email_1 = Email(String::from("[email protected]"));
422-
let b = bson::to_bson(&email_1).unwrap();
434+
let b = to_bson(&email_1).unwrap();
423435
assert_eq!(b, Bson::String(email_1.0));
424436

425437
let s = String::from("[email protected]");
426438
let de = Bson::String(s.clone());
427-
let email_2 = bson::from_bson::<Email>(de).unwrap();
439+
let email_2 = from_bson::<Email>(de).unwrap();
428440
assert_eq!(email_2, Email(s));
429441
}
430442

@@ -435,12 +447,12 @@ fn test_serde_tuple_struct() {
435447
struct Name(String, String); // first, last
436448

437449
let name_1 = Name(String::from("Graydon"), String::from("Hoare"));
438-
let b = bson::to_bson(&name_1).unwrap();
450+
let b = to_bson(&name_1).unwrap();
439451
assert_eq!(b, bson!([name_1.0.clone(), name_1.1]));
440452

441453
let (first, last) = (String::from("Donald"), String::from("Knuth"));
442454
let de = bson!([first.clone(), last.clone()]);
443-
let name_2 = bson::from_bson::<Name>(de).unwrap();
455+
let name_2 = from_bson::<Name>(de).unwrap();
444456
assert_eq!(name_2, Name(first, last));
445457
}
446458

@@ -456,12 +468,12 @@ fn test_serde_newtype_variant() {
456468

457469
let n = 42;
458470
let num_1 = Number::Int(n);
459-
let b = bson::to_bson(&num_1).unwrap();
471+
let b = to_bson(&num_1).unwrap();
460472
assert_eq!(b, bson!({ "type": "Int", "value": n }));
461473

462474
let x = 1337.0;
463475
let de = bson!({ "type": "Float", "value": x });
464-
let num_2 = bson::from_bson::<Number>(de).unwrap();
476+
let num_2 = from_bson::<Number>(de).unwrap();
465477
assert_eq!(num_2, Number::Float(x));
466478
}
467479

@@ -477,12 +489,12 @@ fn test_serde_tuple_variant() {
477489
#[allow(clippy::approx_constant)]
478490
let (x1, y1) = (3.14, -2.71);
479491
let p1 = Point::TwoDim(x1, y1);
480-
let b = bson::to_bson(&p1).unwrap();
492+
let b = to_bson(&p1).unwrap();
481493
assert_eq!(b, bson!({ "TwoDim": [x1, y1] }));
482494

483495
let (x2, y2, z2) = (0.0, -13.37, 4.2);
484496
let de = bson!({ "ThreeDim": [x2, y2, z2] });
485-
let p2 = bson::from_bson::<Point>(de).unwrap();
497+
let p2 = from_bson::<Point>(de).unwrap();
486498
assert_eq!(p2, Point::ThreeDim(x2, y2, z2));
487499
}
488500

@@ -510,13 +522,13 @@ fn test_ser_db_pointer() {
510522
db_pointer: db_pointer.clone(),
511523
};
512524

513-
let x = bson::to_bson(&foo).unwrap();
525+
let x = to_bson(&foo).unwrap();
514526
assert_eq!(
515527
x.as_document().unwrap(),
516528
&doc! {"db_pointer": Bson::DbPointer(db_pointer.clone()) }
517529
);
518530

519-
let xfoo: Foo = bson::from_bson(x).unwrap();
531+
let xfoo: Foo = from_bson(x).unwrap();
520532
assert_eq!(xfoo, foo);
521533
}
522534

@@ -539,7 +551,7 @@ fn test_de_db_pointer() {
539551
.unwrap();
540552
let db_pointer = db_pointer.as_db_pointer().unwrap();
541553

542-
let foo: Foo = bson::from_bson(Bson::Document(
554+
let foo: Foo = from_bson(Bson::Document(
543555
doc! {"db_pointer": Bson::DbPointer(db_pointer.clone())},
544556
))
545557
.unwrap();

0 commit comments

Comments
 (0)