Replace all bson document handling with macro

pull/5/merge
Thijs Cadier 9 years ago
parent 2cd8d988db
commit 1830144125

@ -122,12 +122,9 @@ impl Drop for Bsonc {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bson;
#[test] #[test]
fn test_bsonc_from_and_as_document() { fn test_bsonc_from_and_as_document() {
let mut document = bson::Document::new(); let document = doc! { "key" => "value" };
document.insert("key".to_string(), bson::Bson::String("value".to_string()));
let bsonc = super::Bsonc::from_document(&document).unwrap(); let bsonc = super::Bsonc::from_document(&document).unwrap();

@ -232,9 +232,10 @@ mod tests {
{ {
let bulk_operation = collection.create_bulk_operation(None); let bulk_operation = collection.create_bulk_operation(None);
let mut document = bson::Document::new(); let document = doc! {
document.insert("key_1".to_string(), bson::Bson::String("Value 1".to_string())); "key_1" => "Value 1",
document.insert("key_2".to_string(), bson::Bson::String("Value 2".to_string())); "key_2" => "Value 2"
};
for _ in 0..5 { for _ in 0..5 {
bulk_operation.insert(&document).unwrap(); bulk_operation.insert(&document).unwrap();
} }
@ -246,15 +247,14 @@ mod tests {
result.ok().unwrap().get("nInserted").unwrap().to_json(), result.ok().unwrap().get("nInserted").unwrap().to_json(),
bson::Bson::I32(5).to_json() bson::Bson::I32(5).to_json()
); );
assert_eq!(5, collection.count(&bson::Document::new(), None).unwrap()); assert_eq!(5, collection.count(&doc!{}, None).unwrap());
} }
let query = bson::Document::new(); let query = doc!{};
let mut update_document = bson::Document::new(); let update_document = doc! {
let mut set = bson::Document::new(); "$set" => {"key_1" => "Value update"}
set.insert("key_1".to_string(), bson::Bson::String("Value update".to_string())); };
update_document.insert("$set".to_string(), bson::Bson::Document(set));
// Update one // Update one
{ {
@ -274,7 +274,7 @@ mod tests {
bson::Bson::I32(1).to_json() bson::Bson::I32(1).to_json()
); );
let first_document = collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap();
assert_eq!( assert_eq!(
first_document.get("key_1").unwrap().to_json(), first_document.get("key_1").unwrap().to_json(),
bson::Bson::String("Value update".to_string()).to_json() bson::Bson::String("Value update".to_string()).to_json()
@ -301,8 +301,8 @@ mod tests {
bson::Bson::I32(4).to_json() bson::Bson::I32(4).to_json()
); );
collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap();
let second_document = collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); let second_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap();
assert_eq!( assert_eq!(
second_document.get("key_1").unwrap().to_json(), second_document.get("key_1").unwrap().to_json(),
bson::Bson::String("Value update".to_string()).to_json() bson::Bson::String("Value update".to_string()).to_json()
@ -313,8 +313,7 @@ mod tests {
// Replace one // Replace one
{ {
let mut replace_document = bson::Document::new(); let replace_document = doc! { "key_1" => "Value replace" };
replace_document.insert("key_1".to_string(), bson::Bson::String("Value replace".to_string()));
let bulk_operation = collection.create_bulk_operation(None); let bulk_operation = collection.create_bulk_operation(None);
bulk_operation.replace_one( bulk_operation.replace_one(
@ -331,7 +330,7 @@ mod tests {
bson::Bson::I32(1).to_json() bson::Bson::I32(1).to_json()
); );
let first_document = collection.find(&bson::Document::new(), None).unwrap().next().unwrap().unwrap(); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap();
assert_eq!( assert_eq!(
first_document.get("key_1").unwrap().to_json(), first_document.get("key_1").unwrap().to_json(),
bson::Bson::String("Value replace".to_string()).to_json() bson::Bson::String("Value replace".to_string()).to_json()

@ -4,7 +4,7 @@ use std::borrow::Cow;
use mongo_c_driver_wrapper::bindings; use mongo_c_driver_wrapper::bindings;
use bson::{Bson,Document}; use bson::Document;
use super::Result; use super::Result;
use super::CommandAndFindOptions; use super::CommandAndFindOptions;
@ -378,12 +378,10 @@ impl<'a> Collection<'a> {
find_options: Option<CommandAndFindOptions>, find_options: Option<CommandAndFindOptions>,
tail_options: Option<TailOptions> tail_options: Option<TailOptions>
) -> TailingCursor<'a> { ) -> TailingCursor<'a> {
let mut query_with_options = Document::new(); let query_with_options = doc! {
query_with_options.insert( "$query" => query,
"$query".to_string(), "$natural" => 1
Bson::Document(query) };
);
query_with_options.insert("$natural".to_string(), Bson::I32(1));
TailingCursor::new( TailingCursor::new(
self, self,
@ -417,8 +415,7 @@ mod tests {
let client = pool.pop(); let client = pool.pop();
let collection = client.get_collection("rust_driver_test", "items"); let collection = client.get_collection("rust_driver_test", "items");
let mut command = bson::Document::new(); let command = doc! { "ping" => 1 };
command.insert("ping".to_string(), bson::Bson::I32(1));
let result = collection.command(command, None).unwrap().next().unwrap().unwrap(); let result = collection.command(command, None).unwrap().next().unwrap().unwrap();
assert!(result.contains_key("ok")); assert!(result.contains_key("ok"));
@ -434,16 +431,18 @@ mod tests {
assert_eq!("items", collection.get_name().to_mut()); assert_eq!("items", collection.get_name().to_mut());
let mut document = bson::Document::new(); let document = doc! {
document.insert("key_1".to_string(), bson::Bson::String("Value 1".to_string())); "key_1" => "Value 1",
document.insert("key_2".to_string(), bson::Bson::String("Value 2".to_string())); "key_2" => "Value 2"
};
assert!(collection.insert(&document, None).is_ok()); assert!(collection.insert(&document, None).is_ok());
let mut second_document = bson::Document::new(); let second_document = doc! {
second_document.insert("key_1".to_string(), bson::Bson::String("Value 3".to_string())); "key_1" => "Value 3"
};
assert!(collection.insert(&second_document, None).is_ok()); assert!(collection.insert(&second_document, None).is_ok());
let query = bson::Document::new(); let query = doc!{};
// Count the documents in the collection // Count the documents in the collection
assert_eq!(2, collection.count(&query, None).unwrap()); assert_eq!(2, collection.count(&query, None).unwrap());
@ -486,14 +485,12 @@ mod tests {
// Find the document with fields set // Find the document with fields set
{ {
let mut fields = bson::Document::new();
fields.insert("key_1".to_string(), bson::Bson::Boolean(true));
let options = super::super::CommandAndFindOptions { let options = super::super::CommandAndFindOptions {
query_flags: flags::Flags::new(), query_flags: flags::Flags::new(),
skip: 0, skip: 0,
limit: 0, limit: 0,
batch_size: 0, batch_size: 0,
fields: Some(fields), fields: Some(doc! { "key_1" => true }),
read_prefs: None read_prefs: None
}; };
@ -520,7 +517,7 @@ mod tests {
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let collection = client.get_collection("rust_driver_test", "items"); let collection = client.get_collection("rust_driver_test", "items");
let document = bson::Document::new(); let document = doc! {};
let result = collection.insert(&document, None); let result = collection.insert(&document, None);
assert!(result.is_err()); assert!(result.is_err());

@ -179,9 +179,7 @@ impl<'a> Iterator for TailingCursor<'a> {
// Add the last seen id to the query if it's present. // Add the last seen id to the query if it's present.
match self.last_seen_id.take() { match self.last_seen_id.take() {
Some(id) => { Some(id) => {
let mut gt_id = Document::new(); self.query.insert("_id".to_string(), Bson::Document(doc! { "$gt" => id }));
gt_id.insert("$gt".to_string(), Bson::ObjectId(id));
self.query.insert("_id".to_string(), Bson::Document(gt_id));
}, },
None => () None => ()
}; };
@ -245,15 +243,14 @@ mod tests {
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "cursor_items"); let mut collection = client.get_collection("rust_driver_test", "cursor_items");
let mut document = bson::Document::new(); let document = doc! { "key" => "value" };
document.insert("key".to_string(), bson::Bson::String("value".to_string()));
collection.drop().unwrap_or(()); collection.drop().unwrap_or(());
for _ in 0..10 { for _ in 0..10 {
assert!(collection.insert(&document, None).is_ok()); assert!(collection.insert(&document, None).is_ok());
} }
let query = bson::Document::new(); let query = doc! {};
let cursor = collection.find(&query, None).unwrap(); let cursor = collection.find(&query, None).unwrap();
assert!(cursor.is_alive()); assert!(cursor.is_alive());
@ -275,14 +272,15 @@ mod tests {
database.get_collection("capped").drop().unwrap_or(()); database.get_collection("capped").drop().unwrap_or(());
database.get_collection("not_capped").drop().unwrap_or(()); database.get_collection("not_capped").drop().unwrap_or(());
let mut options = bson::Document::new(); let options = doc! {
options.insert("capped".to_string(), bson::Bson::Boolean(true)); "capped" => true,
options.insert("size".to_string(), bson::Bson::I32(100000)); "size" => 100000
};
let capped_collection = database.create_collection("capped", Some(&options)).unwrap(); let capped_collection = database.create_collection("capped", Some(&options)).unwrap();
let normal_collection = database.create_collection("not_capped", None).unwrap(); let normal_collection = database.create_collection("not_capped", None).unwrap();
// Try to tail on a normal collection // Try to tail on a normal collection
let failing_cursor = normal_collection.tail(bson::Document::new(), None, None); let failing_cursor = normal_collection.tail(doc!{}, None, None);
let failing_result = failing_cursor.into_iter().next().unwrap(); let failing_result = failing_cursor.into_iter().next().unwrap();
assert!(failing_result.is_err()); assert!(failing_result.is_err());
assert_eq!( assert_eq!(
@ -290,8 +288,7 @@ mod tests {
format!("{:?}", failing_result.err().unwrap()) format!("{:?}", failing_result.err().unwrap())
); );
let mut document = bson::Document::new(); let document = doc! { "key_1" => "Value 1" };
document.insert("key_1".to_string(), bson::Bson::String("Value 1".to_string()));
// Insert a first document into the collection // Insert a first document into the collection
capped_collection.insert(&document, None).unwrap(); capped_collection.insert(&document, None).unwrap();
@ -300,7 +297,7 @@ mod tests {
let guard = thread::spawn(move || { let guard = thread::spawn(move || {
let client = cloned_pool.pop(); let client = cloned_pool.pop();
let collection = client.get_collection("rust_test", "capped"); let collection = client.get_collection("rust_test", "capped");
let cursor = collection.tail(bson::Document::new(), None, None); let cursor = collection.tail(doc!{}, None, None);
let mut counter = 0usize; let mut counter = 0usize;
for result in cursor.into_iter() { for result in cursor.into_iter() {
assert!(result.is_ok()); assert!(result.is_ok());

@ -133,7 +133,6 @@ impl<'a> Drop for Database<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use bson;
use super::super::uri::Uri; use super::super::uri::Uri;
use super::super::client::ClientPool; use super::super::client::ClientPool;
@ -144,8 +143,7 @@ mod tests {
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");
let mut command = bson::Document::new(); let command = doc! { "ping" => 1 };
command.insert("ping".to_string(), bson::Bson::I32(1));
let result = database.command(command, None).unwrap().next().unwrap().unwrap(); let result = database.command(command, None).unwrap().next().unwrap().unwrap();
assert!(result.contains_key("ok")); assert!(result.contains_key("ok"));

@ -2,6 +2,7 @@
extern crate libc; extern crate libc;
extern crate mongo_c_driver_wrapper; extern crate mongo_c_driver_wrapper;
#[macro_use]
extern crate bson; extern crate bson;
use std::result; use std::result;

Loading…
Cancel
Save