From 494914c638dc0fe4c859bb512c99227601c50100 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Wed, 23 Sep 2015 23:11:41 +0200 Subject: [PATCH] Add bson encoding and decoding test --- Cargo.toml | 3 +++ tests/bson_encode_decode.rs | 39 +++++++++++++++++++++++++++++++++++++ tests/tests.rs | 3 ++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/bson_encode_decode.rs diff --git a/Cargo.toml b/Cargo.toml index 3bfd29a..9460198 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,6 @@ git = "https://github.com/zonyitoo/bson-rs.git" [dependencies.mongoc-sys] path = "mongoc-sys" version = "1.1.10" + +[dev-dependencies] +chrono = "*" diff --git a/tests/bson_encode_decode.rs b/tests/bson_encode_decode.rs new file mode 100644 index 0000000..2268769 --- /dev/null +++ b/tests/bson_encode_decode.rs @@ -0,0 +1,39 @@ +use chrono::*; + +use mongo_driver::uri::Uri; +use mongo_driver::client::ClientPool; + +use bson::oid::ObjectId; +use bson::spec::BinarySubtype; + +// Sanity check to make sure the bson implementation +// properly encodes and decodes when passing through +// the database. + +#[test] +fn test_bson_encode_decode() { + let uri = Uri::new("mongodb://localhost:27017/").unwrap(); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let mut collection = client.get_collection("rust_driver_test", "bson"); + collection.drop().unwrap_or(()); + + let datetime = UTC.ymd(2014, 7, 8).and_hms(9, 10, 11); + let document = doc! { + "_id" => (ObjectId::new().unwrap()), + "floating_point" => 10.0, + "string" => "a value", + "array" => [10, 20, 30], + "doc" => {"key" => 1}, + "bool" => true, + "i32" => 1i32, + "i64" => 1i64, + "datetime" => datetime, + "binary_generic" => (BinarySubtype::Generic, vec![0, 1, 2, 3, 4]) + }; + assert!(collection.insert(&document, None).is_ok()); + + let found_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); + + assert_eq!(document, found_document); +} diff --git a/tests/tests.rs b/tests/tests.rs index 80fa378..6f83d62 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,8 +1,9 @@ extern crate mongo_driver; - +extern crate chrono; #[macro_use] extern crate bson; +mod bson_encode_decode; mod bulk_operation; mod client; mod collection;