Skip to content

Commit 37aa822

Browse files
committed
Merge pull request #14 from DenisKolodin/master
Add to_json test and example
2 parents 5cf8d5a + a957727 commit 37aa822

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

tests/modules/bson.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use bson::{Bson, Document};
2+
3+
#[test]
4+
fn to_json() {
5+
let mut doc = Document::new();
6+
doc.insert("first".to_owned(), Bson::I32(1));
7+
doc.insert("second".to_owned(), Bson::String("foo".to_owned()));
8+
doc.insert("alphanumeric".to_owned(), Bson::String("bar".to_owned()));
9+
let data = Bson::Document(doc).to_json();
10+
11+
assert!(data.is_object());
12+
let obj = data.as_object().unwrap();
13+
14+
let first = obj.get("first").unwrap();
15+
assert!(first.is_number());
16+
assert_eq!(first.as_i64().unwrap(), 1);
17+
18+
let second = obj.get("second").unwrap();
19+
assert!(second.is_string());
20+
assert_eq!(second.as_string().unwrap(), "foo");
21+
22+
let alphanumeric = obj.get("alphanumeric").unwrap();
23+
assert!(alphanumeric.is_string());
24+
assert_eq!(alphanumeric.as_string().unwrap(), "bar");
25+
26+
}

tests/modules/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ mod encoder;
22
mod macros;
33
mod oid;
44
mod ordered;
5+
mod bson;

0 commit comments

Comments
 (0)