Move integration tests to own dir
parent
b2d927c137
commit
fc71370c0f
@ -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());
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
@ -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())
|
||||
);
|
||||
}
|
@ -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::<Vec<Result<bson::Document>>>();
|
||||
|
||||
// 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());
|
||||
}
|
@ -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());
|
||||
}
|
@ -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());
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
use mongo_driver::ReadPrefs;
|
||||
|
||||
#[test]
|
||||
fn test_read_prefs() {
|
||||
let read_prefs = ReadPrefs::default();
|
||||
assert!(!read_prefs.inner().is_null());
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
extern crate mongo_driver;
|
||||
|
||||
#[macro_use]
|
||||
extern crate bson;
|
||||
|
||||
mod client;
|
||||
mod collection;
|
@ -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());
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue