From fc71370c0fc71a3679c5e488953633242555f1e8 Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Thu, 27 Aug 2015 17:52:55 +0200 Subject: [PATCH] Move integration tests to own dir --- Cargo.toml | 3 + src/bulk_operation.rs | 172 ---------------------------------------- src/client.rs | 85 -------------------- src/collection.rs | 127 ----------------------------- src/cursor.rs | 96 ---------------------- src/database.rs | 48 ----------- src/flags.rs | 41 ---------- src/read_prefs.rs | 9 --- src/uri.rs | 9 --- src/write_concern.rs | 9 --- tests/bulk_operation.rs | 169 +++++++++++++++++++++++++++++++++++++++ tests/client.rs | 82 +++++++++++++++++++ tests/collection.rs | 125 +++++++++++++++++++++++++++++ tests/cursor.rs | 94 ++++++++++++++++++++++ tests/database.rs | 44 ++++++++++ tests/flags.rs | 37 +++++++++ tests/read_prefs.rs | 7 ++ tests/tests.rs | 7 ++ tests/uri.rs | 10 +++ tests/write_concern.rs | 10 +++ 20 files changed, 588 insertions(+), 596 deletions(-) create mode 100644 tests/bulk_operation.rs create mode 100644 tests/client.rs create mode 100644 tests/collection.rs create mode 100644 tests/cursor.rs create mode 100644 tests/database.rs create mode 100644 tests/flags.rs create mode 100644 tests/read_prefs.rs create mode 100644 tests/tests.rs create mode 100644 tests/uri.rs create mode 100644 tests/write_concern.rs diff --git a/Cargo.toml b/Cargo.toml index cb5af6d..01db1fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ repository = "https://github.com/thijsc/mongo-rust-driver" keywords = ["mongodb", "database"] license = "MIT" +[[test]] +name = "tests" + [dependencies] libc = "*" diff --git a/src/bulk_operation.rs b/src/bulk_operation.rs index ac47a11..6e27325 100644 --- a/src/bulk_operation.rs +++ b/src/bulk_operation.rs @@ -198,175 +198,3 @@ impl<'a> Drop for BulkOperation<'a> { } } } - -#[cfg(test)] -mod tests { - use bson; - use super::super::uri::Uri; - use super::super::client::ClientPool; - - #[test] - fn test_execute_error() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let collection = client.get_collection("rust_driver_test", "bulk_operation_error"); - let bulk_operation = collection.create_bulk_operation(None); - - let result = bulk_operation.execute(); - assert!(result.is_err()); - - let error_message = format!("{:?}", result.err().unwrap()); - assert_eq!(error_message, "MongoError (BsoncError: Cannot do an empty bulk write)"); - } - - #[test] - fn test_insert_remove_replace_update() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let mut collection = client.get_collection("rust_driver_test", "bulk_operation_insert"); - collection.drop().unwrap_or(()); - - // Insert 5 documents - { - let bulk_operation = collection.create_bulk_operation(None); - - let document = doc! { - "key_1" => "Value 1", - "key_2" => "Value 2" - }; - for _ in 0..5 { - bulk_operation.insert(&document).unwrap(); - } - - let result = bulk_operation.execute(); - assert!(result.is_ok()); - - assert_eq!( - result.ok().unwrap().get("nInserted").unwrap().to_json(), - bson::Bson::I32(5).to_json() - ); - assert_eq!(5, collection.count(&doc!{}, None).unwrap()); - } - - let query = doc!{}; - - let update_document = doc! { - "$set" => {"key_1" => "Value update"} - }; - - // Update one - { - let bulk_operation = collection.create_bulk_operation(None); - bulk_operation.update_one( - &query, - &update_document, - false - ).unwrap(); - - let result = bulk_operation.execute(); - println!("{:?}", result); - assert!(result.is_ok()); - - assert_eq!( - result.ok().unwrap().get("nModified").unwrap().to_json(), - bson::Bson::I32(1).to_json() - ); - - let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); - assert_eq!( - first_document.get("key_1").unwrap().to_json(), - bson::Bson::String("Value update".to_string()).to_json() - ); - // Make sure it was updated, it should have other keys - assert!(first_document.get("key_2").is_some()); - } - - // Update all - { - let bulk_operation = collection.create_bulk_operation(None); - bulk_operation.update( - &query, - &update_document, - false - ).unwrap(); - - let result = bulk_operation.execute(); - println!("{:?}", result); - assert!(result.is_ok()); - - assert_eq!( - result.ok().unwrap().get("nModified").unwrap().to_json(), - bson::Bson::I32(4).to_json() - ); - - collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); - let second_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); - assert_eq!( - second_document.get("key_1").unwrap().to_json(), - bson::Bson::String("Value update".to_string()).to_json() - ); - // Make sure it was updated, it should have other keys - assert!(second_document.get("key_2").is_some()); - } - - // Replace one - { - let replace_document = doc! { "key_1" => "Value replace" }; - - let bulk_operation = collection.create_bulk_operation(None); - bulk_operation.replace_one( - &query, - &replace_document, - false - ).unwrap(); - - let result = bulk_operation.execute(); - assert!(result.is_ok()); - - assert_eq!( - result.ok().unwrap().get("nModified").unwrap().to_json(), - bson::Bson::I32(1).to_json() - ); - - let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); - assert_eq!( - first_document.get("key_1").unwrap().to_json(), - bson::Bson::String("Value replace".to_string()).to_json() - ); - // Make sure it was replaced, it shouldn't have other keys - assert!(first_document.get("key_2").is_none()); - } - - // Remove one - { - let bulk_operation = collection.create_bulk_operation(None); - bulk_operation.remove_one(&query).unwrap(); - - let result = bulk_operation.execute(); - assert!(result.is_ok()); - - assert_eq!( - result.ok().unwrap().get("nRemoved").unwrap().to_json(), - bson::Bson::I32(1).to_json() - ); - assert_eq!(4, collection.count(&query, None).unwrap()); - } - - // Remove all remaining documents - { - let bulk_operation = collection.create_bulk_operation(None); - bulk_operation.remove(&query).unwrap(); - - let result = bulk_operation.execute(); - assert!(result.is_ok()); - - assert_eq!( - result.ok().unwrap().get("nRemoved").unwrap().to_json(), - bson::Bson::I32(4).to_json() - ); - assert_eq!(0, collection.count(&query, None).unwrap()); - } - } -} diff --git a/src/client.rs b/src/client.rs index ba76a69..8532762 100644 --- a/src/client.rs +++ b/src/client.rs @@ -272,88 +272,3 @@ impl<'a> Drop for Client<'a> { } } } - -#[cfg(test)] -mod tests { - use std::path::PathBuf; - use std::thread; - use super::super::uri::Uri; - use super::{ClientPool,SslOptions}; - - #[test] - fn test_new_pool_and_pop_client() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - - // Pop a client and get a database and collection - let client = pool.pop(); - pool.pop(); - - let database = client.get_database("rust_test"); - assert_eq!("rust_test", database.get_name().to_mut()); - - let collection = client.get_collection("rust_test", "items"); - assert_eq!("items", collection.get_name().to_mut()); - } - - #[test] - fn test_new_pool_and_pop_client_in_threads() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - - let pool1 = pool.clone(); - let guard1 = thread::spawn(move || { - let client = pool1.pop(); - client.get_collection("test", "items"); - }); - - let pool2 = pool.clone(); - let guard2 = thread::spawn(move || { - let client = pool2.pop(); - client.get_collection("test", "items"); - }); - - guard1.join().unwrap(); - guard2.join().unwrap(); - } - - #[test] - fn test_get_server_status() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - - let status = client.get_server_status(None).unwrap(); - - assert!(status.contains_key("host")); - assert!(status.contains_key("version")); - } - - #[test] - fn test_new_pool_with_ssl_options() { - // We currently have no way to test full operations - let uri = Uri::new("mongodb://localhost:27017/"); - let ssl_options = SslOptions::new( - Some(PathBuf::from("./README.md")), - Some("password".to_string()), - Some(PathBuf::from("./README.md")), - Some(PathBuf::from("./README.md")), - Some(PathBuf::from("./README.md")), - false - ); - assert!(ssl_options.is_ok()); - ClientPool::new(uri, Some(ssl_options.unwrap())); - } - - #[test] - fn test_ssl_options_nonexistent_file() { - assert!(SslOptions::new( - Some(PathBuf::from("/tmp/aaaaa.aa")), - Some("password".to_string()), - Some(PathBuf::from("/tmp/aaaaa.aa")), - Some(PathBuf::from("/tmp/aaaaa.aa")), - Some(PathBuf::from("/tmp/aaaaa.aa")), - false - ).is_err()); - } -} diff --git a/src/collection.rs b/src/collection.rs index 74a5257..20edae0 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -400,130 +400,3 @@ impl<'a> Drop for Collection<'a> { } } } - -#[cfg(test)] -mod tests { - use bson; - use super::super::uri::Uri; - use super::super::client::ClientPool; - use super::super::flags; - - #[test] - fn test_command() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let collection = client.get_collection("rust_driver_test", "items"); - - let command = doc! { "ping" => 1 }; - - let result = collection.command(command, None).unwrap().next().unwrap().unwrap(); - assert!(result.contains_key("ok")); - } - - #[test] - fn test_mutation_and_finding() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let mut collection = client.get_collection("rust_driver_test", "items"); - collection.drop().unwrap_or(()); - - assert_eq!("items", collection.get_name().to_mut()); - - let document = doc! { - "key_1" => "Value 1", - "key_2" => "Value 2" - }; - assert!(collection.insert(&document, None).is_ok()); - - let second_document = doc! { - "key_1" => "Value 3" - }; - assert!(collection.insert(&second_document, None).is_ok()); - - let query = doc!{}; - - // Count the documents in the collection - assert_eq!(2, collection.count(&query, None).unwrap()); - - // Find the documents - assert_eq!( - collection.find(&document, None).unwrap().next().unwrap().unwrap().get("key_1").unwrap().to_json(), - bson::Bson::String("Value 1".to_string()).to_json() - ); - let mut found_document = collection.find(&second_document, None).unwrap().next().unwrap().unwrap(); - assert_eq!( - found_document.get("key_1").unwrap().to_json(), - bson::Bson::String("Value 3".to_string()).to_json() - ); - - // Update the second document - found_document.insert("key_1".to_string(), bson::Bson::String("Value 4".to_string())); - assert!(collection.save(&found_document, None).is_ok()); - - // Reload and check value - let found_document = collection.find(&found_document, None).unwrap().next().unwrap().unwrap(); - assert_eq!( - found_document.get("key_1").unwrap().to_json(), - bson::Bson::String("Value 4".to_string()).to_json() - ); - - // Remove one - assert!(collection.remove(&found_document, None).is_ok()); - - // Count again - assert_eq!(1, collection.count(&query, None).unwrap()); - - // Find the document and see if it has the keys we expect - { - let mut cursor = collection.find(&query, None).unwrap(); - let next_document = cursor.next().unwrap().unwrap(); - assert!(next_document.contains_key("key_1")); - assert!(next_document.contains_key("key_2")); - } - - // Find the document with fields set - { - let options = super::super::CommandAndFindOptions { - query_flags: flags::Flags::new(), - skip: 0, - limit: 0, - batch_size: 0, - fields: Some(doc! { "key_1" => true }), - read_prefs: None - }; - - // Query a couple of times to make sure the C driver keeps - // access to the fields bson object. - for _ in 0..5 { - collection.find(&query, Some(&options)).unwrap(); - } - - let mut cursor = collection.find(&query, Some(&options)).unwrap(); - let next_document = cursor.next().unwrap().unwrap(); - assert!(next_document.contains_key("key_1")); - assert!(!next_document.contains_key("key_2")); - } - - // Drop collection - collection.drop().unwrap(); - assert_eq!(0, collection.count(&query, None).unwrap()); - } - - #[test] - fn test_insert_failure() { - let uri = Uri::new("mongodb://localhost:27018/"); // There should be no mongo server here - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let collection = client.get_collection("rust_driver_test", "items"); - let document = doc! {}; - - let result = collection.insert(&document, None); - assert!(result.is_err()); - assert_eq!( - "MongoError (BsoncError: Failed to connect to target host: localhost:27018)", - format!("{:?}", result.err().unwrap()) - ); - } -} diff --git a/src/cursor.rs b/src/cursor.rs index aa60b59..14047e1 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -227,99 +227,3 @@ impl<'a> Iterator for TailingCursor<'a> { } } } - -#[cfg(test)] -mod tests { - use std::thread; - use bson; - use super::super::uri::Uri; - use super::super::client::ClientPool; - use super::super::Result; - - #[test] - fn test_cursor() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let mut collection = client.get_collection("rust_driver_test", "cursor_items"); - - let document = doc! { "key" => "value" }; - - collection.drop().unwrap_or(()); - for _ in 0..10 { - assert!(collection.insert(&document, None).is_ok()); - } - - let query = doc! {}; - let cursor = collection.find(&query, None).unwrap(); - - assert!(cursor.is_alive()); - - let documents = cursor.into_iter().collect::>>(); - - // See if we got 10 results and the iterator then stopped - assert_eq!(10, documents.len()); - } - - #[test] - fn test_tailing_cursor() { - // See: http://api.mongodb.org/c/1.1.8/cursors.html#tailable - - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let database = client.get_database("rust_test"); - database.get_collection("capped").drop().unwrap_or(()); - database.get_collection("not_capped").drop().unwrap_or(()); - - let options = doc! { - "capped" => true, - "size" => 100000 - }; - let capped_collection = database.create_collection("capped", Some(&options)).unwrap(); - let normal_collection = database.create_collection("not_capped", None).unwrap(); - - // Try to tail on a normal collection - let failing_cursor = normal_collection.tail(doc!{}, None, None); - let failing_result = failing_cursor.into_iter().next().unwrap(); - assert!(failing_result.is_err()); - assert_eq!( - "MongoError (BsoncError: Unable to execute query: error processing query: ns=rust_test.not_capped limit=0 skip=0\nTree: $and\nSort: {}\nProj: {}\n tailable cursor requested on non capped collection)", - format!("{:?}", failing_result.err().unwrap()) - ); - - let document = doc! { "key_1" => "Value 1" }; - // Insert a first document into the collection - capped_collection.insert(&document, None).unwrap(); - - // Start a tailing iterator in a thread - let cloned_pool = pool.clone(); - let guard = thread::spawn(move || { - let client = cloned_pool.pop(); - let collection = client.get_collection("rust_test", "capped"); - let cursor = collection.tail(doc!{}, None, None); - let mut counter = 0usize; - for result in cursor.into_iter() { - assert!(result.is_ok()); - counter += 1; - if counter == 25 { - break; - } - } - counter - }); - - // Wait for the thread to boot up - thread::sleep_ms(250); - - // Insert some more documents into the collection - for _ in 0..25 { - capped_collection.insert(&document, None).unwrap(); - } - - // See if they appeared while iterating the cursor - // The for loop returns whenever we get more than - // 15 results. - assert_eq!(25, guard.join().unwrap()); - } -} diff --git a/src/database.rs b/src/database.rs index 7814561..8e8cc41 100644 --- a/src/database.rs +++ b/src/database.rs @@ -130,51 +130,3 @@ impl<'a> Drop for Database<'a> { } } } - -#[cfg(test)] -mod tests { - use super::super::uri::Uri; - use super::super::client::ClientPool; - - #[test] - fn test_command() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let database = client.get_database("rust_test"); - - let command = doc! { "ping" => 1 }; - - let result = database.command(command, None).unwrap().next().unwrap().unwrap(); - assert!(result.contains_key("ok")); - } - - #[test] - fn test_get_collection_and_name() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let database = client.get_database("rust_test"); - - assert_eq!("rust_test", database.get_name().to_mut()); - - let collection = database.get_collection("items"); - assert_eq!("items", collection.get_name().to_mut()); - } - - #[test] - fn test_create_collection() { - let uri = Uri::new("mongodb://localhost:27017/"); - let pool = ClientPool::new(uri, None); - let client = pool.pop(); - let database = client.get_database("rust_test"); - database.get_collection("created_collection").drop().unwrap_or(()); - - let collection = database.create_collection( - "created_collection", - None - ).unwrap(); - - assert_eq!("created_collection", collection.get_name().to_mut()); - } -} diff --git a/src/flags.rs b/src/flags.rs index c1bc9a2..61e9354 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -94,44 +94,3 @@ impl FlagsValue for Flags { } } } - -#[cfg(test)] -mod tests { - use super::FlagsValue; - - #[test] - pub fn test_insert_flags() { - let mut flags = super::Flags::new(); - assert_eq!(0, flags.flags()); - - flags.add(super::InsertFlag::ContinueOnError); - assert_eq!(1, flags.flags()); - - flags.add(super::InsertFlag::NoValidate); - flags.add(super::InsertFlag::NoValidate); - assert_eq!(31, flags.flags()); - } - - #[test] - pub fn test_query_flags() { - let mut flags = super::Flags::new(); - assert_eq!(0, flags.flags()); - - flags.add(super::QueryFlag::TailableCursor); - assert_eq!(2, flags.flags()); - - flags.add(super::QueryFlag::Partial); - flags.add(super::QueryFlag::Partial); - assert_eq!(130, flags.flags()); - } - - #[test] - pub fn test_remove_flags() { - let mut flags = super::Flags::new(); - assert_eq!(0, flags.flags()); - - flags.add(super::RemoveFlag::SingleRemove); - flags.add(super::RemoveFlag::SingleRemove); - assert_eq!(1, flags.flags()); - } -} diff --git a/src/read_prefs.rs b/src/read_prefs.rs index ee75a73..d270fad 100644 --- a/src/read_prefs.rs +++ b/src/read_prefs.rs @@ -53,12 +53,3 @@ impl Drop for ReadPrefs { } } } - -#[cfg(test)] -mod tests { - #[test] - fn test_read_prefs() { - let read_prefs = super::ReadPrefs::default(); - assert!(!read_prefs.inner().is_null()); - } -} diff --git a/src/uri.rs b/src/uri.rs index 2b07c48..a992dea 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -58,12 +58,3 @@ impl Drop for Uri { } } } - -#[cfg(test)] -mod tests { - #[test] - fn test_new_uri() { - let uri = super::Uri::new("mongodb://localhost:27017/"); - assert_eq!("mongodb://localhost:27017/", uri.as_str()); - } -} diff --git a/src/write_concern.rs b/src/write_concern.rs index 271e6d4..3d3e00e 100644 --- a/src/write_concern.rs +++ b/src/write_concern.rs @@ -25,12 +25,3 @@ impl Drop for WriteConcern { } } } - -#[cfg(test)] -mod tests { - #[test] - fn test_write_concern() { - let write_concern = super::WriteConcern::new(); - assert!(!write_concern.inner().is_null()); - } -} diff --git a/tests/bulk_operation.rs b/tests/bulk_operation.rs new file mode 100644 index 0000000..61cdd45 --- /dev/null +++ b/tests/bulk_operation.rs @@ -0,0 +1,169 @@ +use bson; + +use mongo_driver::uri::Uri; +use mongo_driver::client::ClientPool; + +#[test] +fn test_execute_error() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let collection = client.get_collection("rust_driver_test", "bulk_operation_error"); + let bulk_operation = collection.create_bulk_operation(None); + + let result = bulk_operation.execute(); + assert!(result.is_err()); + + let error_message = format!("{:?}", result.err().unwrap()); + assert_eq!(error_message, "MongoError (BsoncError: Cannot do an empty bulk write)"); +} + +#[test] +fn test_insert_remove_replace_update() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let mut collection = client.get_collection("rust_driver_test", "bulk_operation_insert"); + collection.drop().unwrap_or(()); + + // Insert 5 documents + { + let bulk_operation = collection.create_bulk_operation(None); + + let document = doc! { + "key_1" => "Value 1", + "key_2" => "Value 2" + }; + for _ in 0..5 { + bulk_operation.insert(&document).unwrap(); + } + + let result = bulk_operation.execute(); + assert!(result.is_ok()); + + assert_eq!( + result.ok().unwrap().get("nInserted").unwrap().to_json(), + bson::Bson::I32(5).to_json() + ); + assert_eq!(5, collection.count(&doc!{}, None).unwrap()); + } + + let query = doc!{}; + + let update_document = doc! { + "$set" => {"key_1" => "Value update"} + }; + + // Update one + { + let bulk_operation = collection.create_bulk_operation(None); + bulk_operation.update_one( + &query, + &update_document, + false + ).unwrap(); + + let result = bulk_operation.execute(); + println!("{:?}", result); + assert!(result.is_ok()); + + assert_eq!( + result.ok().unwrap().get("nModified").unwrap().to_json(), + bson::Bson::I32(1).to_json() + ); + + let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); + assert_eq!( + first_document.get("key_1").unwrap().to_json(), + bson::Bson::String("Value update".to_string()).to_json() + ); + // Make sure it was updated, it should have other keys + assert!(first_document.get("key_2").is_some()); + } + + // Update all + { + let bulk_operation = collection.create_bulk_operation(None); + bulk_operation.update( + &query, + &update_document, + false + ).unwrap(); + + let result = bulk_operation.execute(); + println!("{:?}", result); + assert!(result.is_ok()); + + assert_eq!( + result.ok().unwrap().get("nModified").unwrap().to_json(), + bson::Bson::I32(4).to_json() + ); + + collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); + let second_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); + assert_eq!( + second_document.get("key_1").unwrap().to_json(), + bson::Bson::String("Value update".to_string()).to_json() + ); + // Make sure it was updated, it should have other keys + assert!(second_document.get("key_2").is_some()); + } + + // Replace one + { + let replace_document = doc! { "key_1" => "Value replace" }; + + let bulk_operation = collection.create_bulk_operation(None); + bulk_operation.replace_one( + &query, + &replace_document, + false + ).unwrap(); + + let result = bulk_operation.execute(); + assert!(result.is_ok()); + + assert_eq!( + result.ok().unwrap().get("nModified").unwrap().to_json(), + bson::Bson::I32(1).to_json() + ); + + let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); + assert_eq!( + first_document.get("key_1").unwrap().to_json(), + bson::Bson::String("Value replace".to_string()).to_json() + ); + // Make sure it was replaced, it shouldn't have other keys + assert!(first_document.get("key_2").is_none()); + } + + // Remove one + { + let bulk_operation = collection.create_bulk_operation(None); + bulk_operation.remove_one(&query).unwrap(); + + let result = bulk_operation.execute(); + assert!(result.is_ok()); + + assert_eq!( + result.ok().unwrap().get("nRemoved").unwrap().to_json(), + bson::Bson::I32(1).to_json() + ); + assert_eq!(4, collection.count(&query, None).unwrap()); + } + + // Remove all remaining documents + { + let bulk_operation = collection.create_bulk_operation(None); + bulk_operation.remove(&query).unwrap(); + + let result = bulk_operation.execute(); + assert!(result.is_ok()); + + assert_eq!( + result.ok().unwrap().get("nRemoved").unwrap().to_json(), + bson::Bson::I32(4).to_json() + ); + assert_eq!(0, collection.count(&query, None).unwrap()); + } +} diff --git a/tests/client.rs b/tests/client.rs new file mode 100644 index 0000000..3c60755 --- /dev/null +++ b/tests/client.rs @@ -0,0 +1,82 @@ +use std::path::PathBuf; +use std::thread; + +use mongo_driver::uri::Uri; +use mongo_driver::client::{ClientPool,SslOptions}; + +#[test] +fn test_new_pool_and_pop_client() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + + // Pop a client and get a database and collection + let client = pool.pop(); + pool.pop(); + + let database = client.get_database("rust_test"); + assert_eq!("rust_test", database.get_name().to_mut()); + + let collection = client.get_collection("rust_test", "items"); + assert_eq!("items", collection.get_name().to_mut()); +} + +#[test] +fn test_new_pool_and_pop_client_in_threads() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + + let pool1 = pool.clone(); + let guard1 = thread::spawn(move || { + let client = pool1.pop(); + client.get_collection("test", "items"); + }); + + let pool2 = pool.clone(); + let guard2 = thread::spawn(move || { + let client = pool2.pop(); + client.get_collection("test", "items"); + }); + + guard1.join().unwrap(); + guard2.join().unwrap(); +} + +#[test] +fn test_get_server_status() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + + let status = client.get_server_status(None).unwrap(); + + assert!(status.contains_key("host")); + assert!(status.contains_key("version")); +} + +#[test] +fn test_new_pool_with_ssl_options() { + // We currently have no way to test full operations + let uri = Uri::new("mongodb://localhost:27017/"); + let ssl_options = SslOptions::new( + Some(PathBuf::from("./README.md")), + Some("password".to_string()), + Some(PathBuf::from("./README.md")), + Some(PathBuf::from("./README.md")), + Some(PathBuf::from("./README.md")), + false + ); + assert!(ssl_options.is_ok()); + ClientPool::new(uri, Some(ssl_options.unwrap())); +} + +#[test] +fn test_ssl_options_nonexistent_file() { + assert!(SslOptions::new( + Some(PathBuf::from("/tmp/aaaaa.aa")), + Some("password".to_string()), + Some(PathBuf::from("/tmp/aaaaa.aa")), + Some(PathBuf::from("/tmp/aaaaa.aa")), + Some(PathBuf::from("/tmp/aaaaa.aa")), + false + ).is_err()); +} diff --git a/tests/collection.rs b/tests/collection.rs new file mode 100644 index 0000000..8493c55 --- /dev/null +++ b/tests/collection.rs @@ -0,0 +1,125 @@ +use bson; + +use mongo_driver::CommandAndFindOptions; +use mongo_driver::uri::Uri; +use mongo_driver::client::ClientPool; +use mongo_driver::flags; + +#[test] +fn test_command() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let collection = client.get_collection("rust_driver_test", "items"); + + let command = doc! { "ping" => 1 }; + + let result = collection.command(command, None).unwrap().next().unwrap().unwrap(); + assert!(result.contains_key("ok")); +} + +#[test] +fn test_mutation_and_finding() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let mut collection = client.get_collection("rust_driver_test", "items"); + collection.drop().unwrap_or(()); + + assert_eq!("items", collection.get_name().to_mut()); + + let document = doc! { + "key_1" => "Value 1", + "key_2" => "Value 2" + }; + assert!(collection.insert(&document, None).is_ok()); + + let second_document = doc! { + "key_1" => "Value 3" + }; + assert!(collection.insert(&second_document, None).is_ok()); + + let query = doc!{}; + + // Count the documents in the collection + assert_eq!(2, collection.count(&query, None).unwrap()); + + // Find the documents + assert_eq!( + collection.find(&document, None).unwrap().next().unwrap().unwrap().get("key_1").unwrap().to_json(), + bson::Bson::String("Value 1".to_string()).to_json() + ); + let mut found_document = collection.find(&second_document, None).unwrap().next().unwrap().unwrap(); + assert_eq!( + found_document.get("key_1").unwrap().to_json(), + bson::Bson::String("Value 3".to_string()).to_json() + ); + + // Update the second document + found_document.insert("key_1".to_string(), bson::Bson::String("Value 4".to_string())); + assert!(collection.save(&found_document, None).is_ok()); + + // Reload and check value + let found_document = collection.find(&found_document, None).unwrap().next().unwrap().unwrap(); + assert_eq!( + found_document.get("key_1").unwrap().to_json(), + bson::Bson::String("Value 4".to_string()).to_json() + ); + + // Remove one + assert!(collection.remove(&found_document, None).is_ok()); + + // Count again + assert_eq!(1, collection.count(&query, None).unwrap()); + + // Find the document and see if it has the keys we expect + { + let mut cursor = collection.find(&query, None).unwrap(); + let next_document = cursor.next().unwrap().unwrap(); + assert!(next_document.contains_key("key_1")); + assert!(next_document.contains_key("key_2")); + } + + // Find the document with fields set + { + let options = CommandAndFindOptions { + query_flags: flags::Flags::new(), + skip: 0, + limit: 0, + batch_size: 0, + fields: Some(doc! { "key_1" => true }), + read_prefs: None + }; + + // Query a couple of times to make sure the C driver keeps + // access to the fields bson object. + for _ in 0..5 { + collection.find(&query, Some(&options)).unwrap(); + } + + let mut cursor = collection.find(&query, Some(&options)).unwrap(); + let next_document = cursor.next().unwrap().unwrap(); + assert!(next_document.contains_key("key_1")); + assert!(!next_document.contains_key("key_2")); + } + + // Drop collection + collection.drop().unwrap(); + assert_eq!(0, collection.count(&query, None).unwrap()); +} + +#[test] +fn test_insert_failure() { + let uri = Uri::new("mongodb://localhost:27018/"); // There should be no mongo server here + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let collection = client.get_collection("rust_driver_test", "items"); + let document = doc! {}; + + let result = collection.insert(&document, None); + assert!(result.is_err()); + assert_eq!( + "MongoError (BsoncError: Failed to connect to target host: localhost:27018)", + format!("{:?}", result.err().unwrap()) + ); +} diff --git a/tests/cursor.rs b/tests/cursor.rs new file mode 100644 index 0000000..e818289 --- /dev/null +++ b/tests/cursor.rs @@ -0,0 +1,94 @@ +use std::thread; + +use bson; + +use mongo_driver::uri::Uri; +use mongo_driver::client::ClientPool; +use mongo_driver::Result; + +#[test] +fn test_cursor() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let mut collection = client.get_collection("rust_driver_test", "cursor_items"); + + let document = doc! { "key" => "value" }; + + collection.drop().unwrap_or(()); + for _ in 0..10 { + assert!(collection.insert(&document, None).is_ok()); + } + + let query = doc! {}; + let cursor = collection.find(&query, None).unwrap(); + + assert!(cursor.is_alive()); + + let documents = cursor.into_iter().collect::>>(); + + // See if we got 10 results and the iterator then stopped + assert_eq!(10, documents.len()); +} + +#[test] +fn test_tailing_cursor() { + // See: http://api.mongodb.org/c/1.1.8/cursors.html#tailable + + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let database = client.get_database("rust_test"); + database.get_collection("capped").drop().unwrap_or(()); + database.get_collection("not_capped").drop().unwrap_or(()); + + let options = doc! { + "capped" => true, + "size" => 100000 + }; + let capped_collection = database.create_collection("capped", Some(&options)).unwrap(); + let normal_collection = database.create_collection("not_capped", None).unwrap(); + + // Try to tail on a normal collection + let failing_cursor = normal_collection.tail(doc!{}, None, None); + let failing_result = failing_cursor.into_iter().next().unwrap(); + assert!(failing_result.is_err()); + assert_eq!( + "MongoError (BsoncError: Unable to execute query: error processing query: ns=rust_test.not_capped limit=0 skip=0\nTree: $and\nSort: {}\nProj: {}\n tailable cursor requested on non capped collection)", + format!("{:?}", failing_result.err().unwrap()) + ); + + let document = doc! { "key_1" => "Value 1" }; + // Insert a first document into the collection + capped_collection.insert(&document, None).unwrap(); + + // Start a tailing iterator in a thread + let cloned_pool = pool.clone(); + let guard = thread::spawn(move || { + let client = cloned_pool.pop(); + let collection = client.get_collection("rust_test", "capped"); + let cursor = collection.tail(doc!{}, None, None); + let mut counter = 0usize; + for result in cursor.into_iter() { + assert!(result.is_ok()); + counter += 1; + if counter == 25 { + break; + } + } + counter + }); + + // Wait for the thread to boot up + thread::sleep_ms(250); + + // Insert some more documents into the collection + for _ in 0..25 { + capped_collection.insert(&document, None).unwrap(); + } + + // See if they appeared while iterating the cursor + // The for loop returns whenever we get more than + // 15 results. + assert_eq!(25, guard.join().unwrap()); +} diff --git a/tests/database.rs b/tests/database.rs new file mode 100644 index 0000000..3f81419 --- /dev/null +++ b/tests/database.rs @@ -0,0 +1,44 @@ +use mongo_driver::uri::Uri; +use mongo_driver::client::ClientPool; + +#[test] +fn test_command() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let database = client.get_database("rust_test"); + + let command = doc! { "ping" => 1 }; + + let result = database.command(command, None).unwrap().next().unwrap().unwrap(); + assert!(result.contains_key("ok")); +} + +#[test] +fn test_get_collection_and_name() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let database = client.get_database("rust_test"); + + assert_eq!("rust_test", database.get_name().to_mut()); + + let collection = database.get_collection("items"); + assert_eq!("items", collection.get_name().to_mut()); +} + +#[test] +fn test_create_collection() { + let uri = Uri::new("mongodb://localhost:27017/"); + let pool = ClientPool::new(uri, None); + let client = pool.pop(); + let database = client.get_database("rust_test"); + database.get_collection("created_collection").drop().unwrap_or(()); + + let collection = database.create_collection( + "created_collection", + None + ).unwrap(); + + assert_eq!("created_collection", collection.get_name().to_mut()); +} diff --git a/tests/flags.rs b/tests/flags.rs new file mode 100644 index 0000000..e51af4b --- /dev/null +++ b/tests/flags.rs @@ -0,0 +1,37 @@ +use mongo_driver::FlagsValue; + +#[test] +pub fn test_insert_flags() { + let mut flags = super::Flags::new(); + assert_eq!(0, flags.flags()); + + flags.add(super::InsertFlag::ContinueOnError); + assert_eq!(1, flags.flags()); + + flags.add(super::InsertFlag::NoValidate); + flags.add(super::InsertFlag::NoValidate); + assert_eq!(31, flags.flags()); +} + +#[test] +pub fn test_query_flags() { + let mut flags = super::Flags::new(); + assert_eq!(0, flags.flags()); + + flags.add(super::QueryFlag::TailableCursor); + assert_eq!(2, flags.flags()); + + flags.add(super::QueryFlag::Partial); + flags.add(super::QueryFlag::Partial); + assert_eq!(130, flags.flags()); +} + +#[test] +pub fn test_remove_flags() { + let mut flags = super::Flags::new(); + assert_eq!(0, flags.flags()); + + flags.add(super::RemoveFlag::SingleRemove); + flags.add(super::RemoveFlag::SingleRemove); + assert_eq!(1, flags.flags()); +} diff --git a/tests/read_prefs.rs b/tests/read_prefs.rs new file mode 100644 index 0000000..1f6a769 --- /dev/null +++ b/tests/read_prefs.rs @@ -0,0 +1,7 @@ +use mongo_driver::ReadPrefs; + +#[test] +fn test_read_prefs() { + let read_prefs = ReadPrefs::default(); + assert!(!read_prefs.inner().is_null()); +} diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..a6c938f --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,7 @@ +extern crate mongo_driver; + +#[macro_use] +extern crate bson; + +mod client; +mod collection; diff --git a/tests/uri.rs b/tests/uri.rs new file mode 100644 index 0000000..1b4fbf9 --- /dev/null +++ b/tests/uri.rs @@ -0,0 +1,10 @@ +use mongo_driver::Uri; + +#[cfg(test)] +mod tests { + #[test] + fn test_new_uri() { + let uri = Uri::new("mongodb://localhost:27017/"); + assert_eq!("mongodb://localhost:27017/", uri.as_str()); + } +} diff --git a/tests/write_concern.rs b/tests/write_concern.rs new file mode 100644 index 0000000..e53ef50 --- /dev/null +++ b/tests/write_concern.rs @@ -0,0 +1,10 @@ +use mongo_driver::WriteConcern; + +#[cfg(test)] +mod tests { + #[test] + fn test_write_concern() { + let write_concern = WriteConcern::new(); + assert!(!write_concern.inner().is_null()); + } +}