|
|
@ -1,6 +1,8 @@
|
|
|
|
extern crate bson;
|
|
|
|
extern crate bson;
|
|
|
|
extern crate mongo_driver;
|
|
|
|
extern crate mongo_driver;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mod helpers;
|
|
|
|
|
|
|
|
|
|
|
|
use bson::doc;
|
|
|
|
use bson::doc;
|
|
|
|
|
|
|
|
|
|
|
|
use mongo_driver::CommandAndFindOptions;
|
|
|
|
use mongo_driver::CommandAndFindOptions;
|
|
|
@ -10,14 +12,14 @@ use mongo_driver::flags;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_aggregate() {
|
|
|
|
fn test_aggregate() {
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let pool = ClientPool::new(uri, None);
|
|
|
|
let pool = ClientPool::new(uri, None);
|
|
|
|
let client = pool.pop();
|
|
|
|
let client = pool.pop();
|
|
|
|
let mut collection = client.get_collection("rust_driver_test", "aggregate");
|
|
|
|
let mut collection = client.get_collection("rust_driver_test", "aggregate");
|
|
|
|
collection.drop().unwrap_or(());
|
|
|
|
collection.drop().unwrap_or(());
|
|
|
|
|
|
|
|
|
|
|
|
for _ in 0..5 {
|
|
|
|
for _ in 0..5 {
|
|
|
|
assert!(collection.insert(&doc!{"key": 1}, None).is_ok());
|
|
|
|
collection.insert(&doc!{"key": 1}, None).expect("Could not insert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let pipeline = doc!{
|
|
|
|
let pipeline = doc!{
|
|
|
@ -36,10 +38,9 @@ fn test_aggregate() {
|
|
|
|
assert_eq!(Ok(5), total.get_i32("total"));
|
|
|
|
assert_eq!(Ok(5), total.get_i32("total"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(target_os = "windows", ignore)]
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_command() {
|
|
|
|
fn test_command() {
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
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");
|
|
|
@ -52,7 +53,7 @@ fn test_command() {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_command_simple() {
|
|
|
|
fn test_command_simple() {
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
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");
|
|
|
@ -65,7 +66,7 @@ fn test_command_simple() {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_mutation_and_finding() {
|
|
|
|
fn test_mutation_and_finding() {
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let pool = ClientPool::new(uri, None);
|
|
|
|
let pool = ClientPool::new(uri, None);
|
|
|
|
let client = pool.pop();
|
|
|
|
let client = pool.pop();
|
|
|
|
let _ = client.get_collection("rust_driver_test".to_string(), "items");
|
|
|
|
let _ = client.get_collection("rust_driver_test".to_string(), "items");
|
|
|
@ -94,7 +95,7 @@ fn test_mutation_and_finding() {
|
|
|
|
let second_document = doc! {
|
|
|
|
let second_document = doc! {
|
|
|
|
"key_1": "Value 3"
|
|
|
|
"key_1": "Value 3"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
assert!(collection.insert(&second_document, None).is_ok());
|
|
|
|
collection.insert(&second_document, None).expect("Could not insert");
|
|
|
|
|
|
|
|
|
|
|
|
let query = doc!{};
|
|
|
|
let query = doc!{};
|
|
|
|
|
|
|
|
|
|
|
@ -119,7 +120,7 @@ fn test_mutation_and_finding() {
|
|
|
|
|
|
|
|
|
|
|
|
// Update the second document
|
|
|
|
// Update the second document
|
|
|
|
let update = doc!{"$set": {"key_1": "Value 4"}};
|
|
|
|
let update = doc!{"$set": {"key_1": "Value 4"}};
|
|
|
|
assert!(collection.update(&second_document, &update, None).is_ok());
|
|
|
|
collection.update(&second_document, &update, None).expect("Could not update");
|
|
|
|
|
|
|
|
|
|
|
|
// Reload and check value
|
|
|
|
// Reload and check value
|
|
|
|
let query_after_update = doc! {
|
|
|
|
let query_after_update = doc! {
|
|
|
@ -133,7 +134,7 @@ fn test_mutation_and_finding() {
|
|
|
|
|
|
|
|
|
|
|
|
// Save the second document
|
|
|
|
// Save the second document
|
|
|
|
found_document.insert("key_1".to_string(), bson::Bson::String("Value 5".to_string()));
|
|
|
|
found_document.insert("key_1".to_string(), bson::Bson::String("Value 5".to_string()));
|
|
|
|
assert!(collection.save(&found_document, None).is_ok());
|
|
|
|
collection.save(&found_document, None).expect("Could not save");
|
|
|
|
|
|
|
|
|
|
|
|
// Reload and check value
|
|
|
|
// Reload and check value
|
|
|
|
let found_document = collection.find(&found_document, None).unwrap().next().unwrap().unwrap();
|
|
|
|
let found_document = collection.find(&found_document, None).unwrap().next().unwrap().unwrap();
|
|
|
@ -143,7 +144,7 @@ fn test_mutation_and_finding() {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Remove one
|
|
|
|
// Remove one
|
|
|
|
assert!(collection.remove(&found_document, None).is_ok());
|
|
|
|
collection.remove(&found_document, None).expect("Could not remove");
|
|
|
|
|
|
|
|
|
|
|
|
// Count again
|
|
|
|
// Count again
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
@ -186,7 +187,7 @@ fn test_mutation_and_finding() {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_find_and_modify() {
|
|
|
|
fn test_find_and_modify() {
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let pool = ClientPool::new(uri, None);
|
|
|
|
let pool = ClientPool::new(uri, None);
|
|
|
|
let client = pool.pop();
|
|
|
|
let client = pool.pop();
|
|
|
|
let mut collection = client.get_collection("rust_driver_test", "find_and_modify");
|
|
|
|
let mut collection = client.get_collection("rust_driver_test", "find_and_modify");
|
|
|
@ -199,12 +200,11 @@ fn test_find_and_modify() {
|
|
|
|
let update = doc! {
|
|
|
|
let update = doc! {
|
|
|
|
"$set": {"content": 1i32}
|
|
|
|
"$set": {"content": 1i32}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let result = collection.find_and_modify(
|
|
|
|
collection.find_and_modify(
|
|
|
|
&query,
|
|
|
|
&query,
|
|
|
|
FindAndModifyOperation::Upsert(&update),
|
|
|
|
FindAndModifyOperation::Upsert(&update),
|
|
|
|
None
|
|
|
|
None
|
|
|
|
);
|
|
|
|
).expect("Could not find and modify");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
|
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
|
|
|
|
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
|
|
|
|
assert_eq!(found_document.get_i32("content"), Ok(1));
|
|
|
|
assert_eq!(found_document.get_i32("content"), Ok(1));
|
|
|
@ -213,23 +213,21 @@ fn test_find_and_modify() {
|
|
|
|
let update2 = doc! {
|
|
|
|
let update2 = doc! {
|
|
|
|
"$set": {"content": 2i32}
|
|
|
|
"$set": {"content": 2i32}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
let result = collection.find_and_modify(
|
|
|
|
collection.find_and_modify(
|
|
|
|
&query,
|
|
|
|
&query,
|
|
|
|
FindAndModifyOperation::Update(&update2),
|
|
|
|
FindAndModifyOperation::Update(&update2),
|
|
|
|
None
|
|
|
|
None
|
|
|
|
);
|
|
|
|
).expect("Could not find and modify");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
|
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
|
|
|
|
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
|
|
|
|
assert_eq!(found_document.get_i32("content"), Ok(2));
|
|
|
|
assert_eq!(found_document.get_i32("content"), Ok(2));
|
|
|
|
|
|
|
|
|
|
|
|
// Remove it
|
|
|
|
// Remove it
|
|
|
|
let result = collection.find_and_modify(
|
|
|
|
collection.find_and_modify(
|
|
|
|
&query,
|
|
|
|
&query,
|
|
|
|
FindAndModifyOperation::Remove,
|
|
|
|
FindAndModifyOperation::Remove,
|
|
|
|
None
|
|
|
|
None
|
|
|
|
);
|
|
|
|
).expect("Could not find and modify");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
|
|
|
assert_eq!(0, collection.count(&query, None).unwrap());
|
|
|
|
assert_eq!(0, collection.count(&query, None).unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|