|
|
@ -1,8 +1,6 @@
|
|
|
|
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;
|
|
|
@ -12,14 +10,14 @@ use mongo_driver::flags;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_aggregate() {
|
|
|
|
fn test_aggregate() {
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").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 {
|
|
|
|
collection.insert(&doc!{"key": 1}, None).expect("Could not insert");
|
|
|
|
assert!(collection.insert(&doc!{"key": 1}, None).is_ok());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let pipeline = doc!{
|
|
|
|
let pipeline = doc!{
|
|
|
@ -38,9 +36,10 @@ 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(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").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");
|
|
|
@ -53,7 +52,7 @@ fn test_command() {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_command_simple() {
|
|
|
|
fn test_command_simple() {
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").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");
|
|
|
@ -66,7 +65,7 @@ fn test_command_simple() {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_mutation_and_finding() {
|
|
|
|
fn test_mutation_and_finding() {
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").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");
|
|
|
@ -95,7 +94,7 @@ fn test_mutation_and_finding() {
|
|
|
|
let second_document = doc! {
|
|
|
|
let second_document = doc! {
|
|
|
|
"key_1": "Value 3"
|
|
|
|
"key_1": "Value 3"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
collection.insert(&second_document, None).expect("Could not insert");
|
|
|
|
assert!(collection.insert(&second_document, None).is_ok());
|
|
|
|
|
|
|
|
|
|
|
|
let query = doc!{};
|
|
|
|
let query = doc!{};
|
|
|
|
|
|
|
|
|
|
|
@ -120,7 +119,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"}};
|
|
|
|
collection.update(&second_document, &update, None).expect("Could not update");
|
|
|
|
assert!(collection.update(&second_document, &update, None).is_ok());
|
|
|
|
|
|
|
|
|
|
|
|
// Reload and check value
|
|
|
|
// Reload and check value
|
|
|
|
let query_after_update = doc! {
|
|
|
|
let query_after_update = doc! {
|
|
|
@ -134,7 +133,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()));
|
|
|
|
collection.save(&found_document, None).expect("Could not save");
|
|
|
|
assert!(collection.save(&found_document, None).is_ok());
|
|
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
@ -144,7 +143,7 @@ fn test_mutation_and_finding() {
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Remove one
|
|
|
|
// Remove one
|
|
|
|
collection.remove(&found_document, None).expect("Could not remove");
|
|
|
|
assert!(collection.remove(&found_document, None).is_ok());
|
|
|
|
|
|
|
|
|
|
|
|
// Count again
|
|
|
|
// Count again
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
|
assert_eq!(1, collection.count(&query, None).unwrap());
|
|
|
@ -187,7 +186,7 @@ fn test_mutation_and_finding() {
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_find_and_modify() {
|
|
|
|
fn test_find_and_modify() {
|
|
|
|
let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
|
|
|
|
let uri = Uri::new("mongodb://localhost:27017/").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");
|
|
|
@ -200,11 +199,12 @@ fn test_find_and_modify() {
|
|
|
|
let update = doc! {
|
|
|
|
let update = doc! {
|
|
|
|
"$set": {"content": 1i32}
|
|
|
|
"$set": {"content": 1i32}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
collection.find_and_modify(
|
|
|
|
let result = 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,21 +213,23 @@ fn test_find_and_modify() {
|
|
|
|
let update2 = doc! {
|
|
|
|
let update2 = doc! {
|
|
|
|
"$set": {"content": 2i32}
|
|
|
|
"$set": {"content": 2i32}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
collection.find_and_modify(
|
|
|
|
let result = 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
|
|
|
|
collection.find_and_modify(
|
|
|
|
let result = 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|