diff --git a/tests/database_tests.rs b/tests/database_tests.rs index 429f41b..510c3f8 100644 --- a/tests/database_tests.rs +++ b/tests/database_tests.rs @@ -19,6 +19,8 @@ extern crate couchbase_lite; extern crate tempdir; extern crate lazy_static; +use crate::utils::default_collection; + use self::couchbase_lite::*; use self::tempdir::TempDir; use lazy_static::lazy_static; @@ -77,14 +79,15 @@ fn copy_file() { encryption_key: None, }; match Database::open(DB_NAME, Some(cfg.clone())) { - Ok(mut db) => { + Ok(db) => { let mut doc = Document::new_with_id("foo"); let mut props = doc.mutable_properties(); props.at("i").put_i64(1); props.at("s").put_string("test"); - db.save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) + default_collection(&db) + .save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) .expect("save"); } Err(_) => panic!("The initial database could not be opened"), @@ -102,7 +105,7 @@ fn copy_file() { // Check document is inside the new DB match Database::open(DB_NAME_BACKUP, Some(cfg)) { Ok(db) => { - let doc = db.get_document("foo").unwrap(); + let doc = default_collection(&db).get_document("foo").unwrap(); assert_eq!(doc.properties().get("i").as_i64().unwrap(), 1); assert_eq!(doc.properties().get("s").as_string().unwrap(), "test"); @@ -116,7 +119,7 @@ fn in_transaction() { utils::with_db(|db| { let result = db.in_transaction(|db| { let mut doc = Document::new_with_id("document"); - let mut collection = db.default_collection_or_error().unwrap(); + let mut collection = default_collection(db); collection .save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) .unwrap(); @@ -128,7 +131,7 @@ fn in_transaction() { let result = db.in_transaction(|db| -> Result { let mut doc = Document::new_with_id("document_error"); - let mut collection = db.default_collection_or_error().unwrap(); + let mut collection = default_collection(db); collection .save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) .unwrap(); @@ -137,7 +140,7 @@ fn in_transaction() { assert!(result.is_err()); - let mut collection = db.default_collection_or_error().unwrap(); + let collection = default_collection(db); assert!(collection.get_document("document").is_ok()); assert!(collection.get_document("document_error").is_err()); }); @@ -146,7 +149,7 @@ fn in_transaction() { #[test] fn manual_transaction_commit() { utils::with_db(|db| { - let initial_count = db.count(); + let initial_count = default_collection(db).count(); { let transaction = db.begin_transaction().unwrap(); @@ -158,8 +161,8 @@ fn manual_transaction_commit() { transaction.commit().unwrap(); } - assert_eq!(db.count(), initial_count + 1); - let mut collection = db.default_collection_or_error().unwrap(); + assert_eq!(default_collection(db).count(), initial_count + 1); + let collection = db.default_collection_or_error().unwrap(); assert!(collection.get_document("manual_commit_doc").is_ok()); }); } @@ -167,7 +170,7 @@ fn manual_transaction_commit() { #[test] fn manual_transaction_rollback() { utils::with_db(|db| { - let initial_count = db.count(); + let initial_count = default_collection(db).count(); { let _transaction = db.begin_transaction().unwrap(); @@ -179,8 +182,8 @@ fn manual_transaction_rollback() { // Transaction dropped here without commit -> automatic rollback } - assert_eq!(db.count(), initial_count); - let mut collection = db.default_collection_or_error().unwrap(); + assert_eq!(default_collection(db).count(), initial_count); + let collection = db.default_collection_or_error().unwrap(); assert!(collection.get_document("rollback_doc").is_err()); }); } @@ -189,7 +192,7 @@ fn manual_transaction_rollback() { fn db_properties() { utils::with_db(|db| { assert_eq!(db.name(), utils::DB_NAME); - assert_eq!(db.count(), 0); + assert_eq!(default_collection(db).count(), 0); }); } @@ -215,10 +218,11 @@ fn db_encryption_key() { // Create database with no encryption & one document { - let mut db = Database::open(utils::DB_NAME, Some(cfg_no_encryption.clone())).unwrap(); + let db = Database::open(utils::DB_NAME, Some(cfg_no_encryption.clone())).unwrap(); let mut doc = Document::new_with_id("foo"); assert!( - db.save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) + default_collection(&db) + .save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) .is_ok() ); } @@ -228,7 +232,7 @@ fn db_encryption_key() { assert!(Database::open(utils::DB_NAME, Some(cfg_encryption1.clone())).is_err()); { let mut db = Database::open(utils::DB_NAME, Some(cfg_no_encryption.clone())).unwrap(); - assert!(db.get_document("foo").is_ok()); + assert!(default_collection(&db).get_document("foo").is_ok()); assert!(db.change_encryption_key(&encryption_key).is_ok()); } @@ -237,22 +241,26 @@ fn db_encryption_key() { assert!(Database::open(utils::DB_NAME, Some(cfg_encryption1.clone())).is_ok()); { let db = Database::open(utils::DB_NAME, Some(cfg_encryption1.clone())).unwrap(); - assert!(db.get_document("foo").is_ok()); + assert!(default_collection(&db).get_document("foo").is_ok()); } } #[test] fn add_listener() { utils::with_db(|db| { + let mut default_collection = default_collection(db); + let (sender, receiver) = std::sync::mpsc::channel(); - let listener_token = db.add_listener(Box::new(move |_, doc_ids| { + let listener_token = default_collection.add_listener(Box::new(move |_, doc_ids| { + println!("\nDoc ids: {:?}\n", doc_ids); if doc_ids.first().unwrap() == "document" { sender.send(true).unwrap(); } })); let mut doc = Document::new_with_id("document"); - db.save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) + default_collection + .save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) .unwrap(); receiver.recv_timeout(Duration::from_secs(1)).unwrap(); @@ -271,14 +279,16 @@ fn buffer_notifications() { utils::set_static(&BUFFER_NOTIFICATIONS, true); }); - let listener_token = db.add_listener(Box::new(move |_, doc_ids| { + let mut default_collection = default_collection(db); + let listener_token = default_collection.add_listener(Box::new(move |_, doc_ids| { if doc_ids.first().unwrap() == "document" { utils::set_static(&DOCUMENT_DETECTED, true); } })); let mut doc = Document::new_with_id("document"); - db.save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) + default_collection + .save_document_with_concurency_control(&mut doc, ConcurrencyControl::LastWriteWins) .unwrap(); assert!(!utils::check_static_with_wait( diff --git a/tests/document_tests.rs b/tests/document_tests.rs index 59ea5a4..54ea332 100644 --- a/tests/document_tests.rs +++ b/tests/document_tests.rs @@ -1,6 +1,8 @@ extern crate core; extern crate couchbase_lite; +use crate::utils::default_collection; + use self::couchbase_lite::*; use std::{thread::sleep, time::Duration}; use utils::{init_logging, LeakChecker}; @@ -37,12 +39,20 @@ fn document_revision_id() { let mut document = Document::new(); assert_eq!(document.revision_id(), None); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) .expect("save_document"); assert!(document.revision_id().is_some()); let first_revision_id = String::from(document.revision_id().unwrap()); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) .expect("save_document"); assert!(document.revision_id().is_some()); let second_revision_id = String::from(document.revision_id().unwrap()); @@ -58,16 +68,18 @@ fn document_sequence() { assert_eq!(document_1.sequence(), 0); assert_eq!(document_2.sequence(), 0); - db.save_document_with_concurency_control( - &mut document_1, - ConcurrencyControl::FailOnConflict, - ) - .expect("save_document"); - db.save_document_with_concurency_control( - &mut document_2, - ConcurrencyControl::FailOnConflict, - ) - .expect("save_document"); + default_collection(db) + .save_document_with_concurency_control( + &mut document_1, + ConcurrencyControl::FailOnConflict, + ) + .expect("save_document"); + default_collection(db) + .save_document_with_concurency_control( + &mut document_2, + ConcurrencyControl::FailOnConflict, + ) + .expect("save_document"); assert_eq!(document_1.sequence(), 1); assert_eq!(document_2.sequence(), 2); }); @@ -115,12 +127,16 @@ fn document_properties_as_json() { fn database_get_document() { utils::with_db(|db| { let mut document = Document::new_with_id("foo"); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) .expect("save_document"); - let document = db.get_document(document.id()); + let document = default_collection(db).get_document(document.id()); assert!(document.is_ok()); assert_eq!(document.unwrap().id(), "foo"); - let document = db.get_document(""); + let document = default_collection(db).get_document(""); assert!(document.is_err()); }); } @@ -129,27 +145,39 @@ fn database_get_document() { fn database_save_document() { utils::with_db(|db| { let mut document = Document::new_with_id("foo"); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) - .expect("save_document"); - let mut document = db.get_document("foo").expect("get_document"); - { - let mut document = db.get_document("foo").expect("get_document"); - document.mutable_properties().at("foo").put_i64(1); - db.save_document_with_concurency_control( + default_collection(db) + .save_document_with_concurency_control( &mut document, ConcurrencyControl::FailOnConflict, ) .expect("save_document"); + let mut document = default_collection(db) + .get_document("foo") + .expect("get_document"); + { + let mut document = default_collection(db) + .get_document("foo") + .expect("get_document"); + document.mutable_properties().at("foo").put_i64(1); + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) + .expect("save_document"); } document.mutable_properties().at("foo").put_i64(2); - let conflict_error = db.save_document_with_concurency_control( + let conflict_error = default_collection(db).save_document_with_concurency_control( &mut document, ConcurrencyControl::FailOnConflict, ); assert!(conflict_error.is_err()); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::LastWriteWins) + default_collection(db) + .save_document_with_concurency_control(&mut document, ConcurrencyControl::LastWriteWins) .expect("save_document"); - let document = db.get_document("foo").expect("get_document"); + let document = default_collection(db) + .get_document("foo") + .expect("get_document"); assert_eq!(document.properties().get("foo").as_i64_or_0(), 2); }); } @@ -158,19 +186,24 @@ fn database_save_document() { fn database_save_document_resolving() { utils::with_db(|db| { let mut document = Document::new_with_id("foo"); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) - .expect("save_document"); - { - let mut document = db.get_document("foo").unwrap(); - document.mutable_properties().at("foo").put_i64(1); - db.save_document_with_concurency_control( + default_collection(db) + .save_document_with_concurency_control( &mut document, ConcurrencyControl::FailOnConflict, ) .expect("save_document"); + { + let mut document = default_collection(db).get_document("foo").unwrap(); + document.mutable_properties().at("foo").put_i64(1); + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) + .expect("save_document"); } document.mutable_properties().at("foo").put_i64(2); - document = db + document = default_collection(db) .save_document_resolving(&mut document, |document_a, document_b| { let property_a = document_a.properties().get("foo").as_i64_or_0(); let property_b = document_b.properties().get("foo").as_i64_or_0(); @@ -182,7 +215,7 @@ fn database_save_document_resolving() { }) .expect("save_document_resolving"); assert_eq!(document.properties().get("foo").as_i64_or_0(), 3); - document = db.get_document("foo").unwrap(); + document = default_collection(db).get_document("foo").unwrap(); assert_eq!(document.properties().get("foo").as_i64_or_0(), 3); }); } @@ -192,24 +225,29 @@ fn database_purge_document() { utils::with_db(|db| { let mut document = Document::new(); { - db.save_document_with_concurency_control( - &mut document, - ConcurrencyControl::FailOnConflict, - ) - .expect("save_document"); + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) + .expect("save_document"); let mut document = Document::new_with_id("foo"); - db.save_document_with_concurency_control( - &mut document, - ConcurrencyControl::FailOnConflict, - ) - .expect("save_document"); + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) + .expect("save_document"); } - db.purge_document(&document).expect("purge_document"); - db.purge_document_by_id("foo") + default_collection(db) + .purge_document(&document) + .expect("purge_document"); + default_collection(db) + .purge_document_by_id("foo") .expect("purge_document_by_id"); - let document = db.get_document(document.id()); + let document = default_collection(db).get_document(document.id()); assert!(document.is_err()); - let document = db.get_document("foo"); + let document = default_collection(db).get_document("foo"); assert!(document.is_err()); }); } @@ -219,9 +257,13 @@ fn database_add_document_change_listener() { utils::with_db(|db| { let (sender, receiver) = std::sync::mpsc::channel(); let mut document = Document::new_with_id("foo"); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) .expect("save_document"); - let listener_token = db.add_document_change_listener( + let listener_token = default_collection(db).add_document_change_listener( &document, Box::new(move |_, document_id| { if let Some(id) = document_id { @@ -231,13 +273,21 @@ fn database_add_document_change_listener() { }), ); document.mutable_properties().at("foo").put_i64(1); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) .expect("save_document"); receiver.recv_timeout(Duration::from_secs(1)).unwrap(); let mut document = Document::new_with_id("bar"); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) .expect("save_document"); assert!(receiver.recv_timeout(Duration::from_secs(10)).is_err()); drop(listener_token); @@ -270,17 +320,17 @@ fn database_delete_document() { // Check 'foo' is replicated to central and DB 2 assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); // Delete document in DB 1 - let document = local_db.get_document("foo").unwrap(); - local_db + let document = default_collection(local_db).get_document("foo").unwrap(); + default_collection(local_db) .delete_document_with_concurency_control(&document, ConcurrencyControl::FailOnConflict) .expect("delete_document"); - let document = local_db.get_document("foo").unwrap(); + let document = default_collection(local_db).get_document("foo").unwrap(); assert!(document.is_deleted()); // Check document is replicated with deleted flag @@ -292,32 +342,41 @@ fn database_delete_document() { fn database_document_expiration() { utils::with_db(|db| { let mut document = Document::new_with_id("foo"); - db.save_document_with_concurency_control(&mut document, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control( + &mut document, + ConcurrencyControl::FailOnConflict, + ) .expect("save_document"); // No expiration by default - let expiration = db.document_expiration("foo").expect("document_expiration"); + let expiration = default_collection(db) + .document_expiration("foo") + .expect("document_expiration"); assert!(expiration.is_none()); // Set expiration in 2 seconds let expiration = Timestamp::now().add(Duration::from_secs(2)); - db.set_document_expiration("foo", Some(expiration)) + default_collection(db) + .set_document_expiration("foo", Some(expiration)) .expect("set_document_expiration"); // Check expiration is set up - let doc_expiration = db.document_expiration("foo").expect("document_expiration"); + let doc_expiration = default_collection(db) + .document_expiration("foo") + .expect("document_expiration"); assert_eq!(doc_expiration.unwrap(), expiration); // Check the document is still present after 1 second sleep(Duration::from_secs(1)); - assert!(db.get_document("foo").is_ok()); + assert!(default_collection(db).get_document("foo").is_ok()); // Move to expiration time sleep(Duration::from_secs(1)); // Check documents disappears for _ in 0..5 { - let doc = db.get_document("foo"); + let doc = default_collection(db).get_document("foo"); if doc.is_err() || doc.unwrap().is_deleted() { return; } diff --git a/tests/query_tests.rs b/tests/query_tests.rs index 7cef9e3..ea23c8e 100644 --- a/tests/query_tests.rs +++ b/tests/query_tests.rs @@ -4,6 +4,8 @@ extern crate regex; use couchbase_lite::index::{ValueIndexConfiguration, ArrayIndexConfiguration}; use regex::Regex; +use crate::utils::default_collection; + use self::couchbase_lite::*; pub mod utils; @@ -74,7 +76,8 @@ fn parameters() { props.at("f64").put_f64(3.1); props.at("i64").put_i64(3); props.at("string").put_string("allo"); - db.save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) + default_collection(db) + .save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) .expect("save"); let query = Query::new( @@ -161,15 +164,21 @@ fn get_index_name_from_explain(explain: &str) -> Option { fn full_index() { utils::with_db(|db| { assert!( - db.create_index( - "new_index", - &ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#, None), - ) - .unwrap() + default_collection(db) + .create_index( + "new_index", + &ValueIndexConfiguration::new(QueryLanguage::JSON, r#"[[".someField"]]"#, None), + ) + .unwrap() ); // Check index creation - let value = db.get_index_names().iter().next().unwrap(); + let value = default_collection(db) + .get_index_names() + .unwrap() + .iter() + .next() + .unwrap(); let name = value.as_string().unwrap(); assert_eq!(name, "new_index"); @@ -200,13 +209,13 @@ fn full_index() { assert!(index.is_none()); // Check index deletion - assert_eq!(db.get_index_names().count(), 1); + assert_eq!(default_collection(db).get_index_names().unwrap().count(), 1); - db.delete_index("idx").unwrap(); - assert_eq!(db.get_index_names().count(), 1); + default_collection(db).delete_index("idx").unwrap(); + assert_eq!(default_collection(db).get_index_names().unwrap().count(), 1); - db.delete_index("new_index").unwrap(); - assert_eq!(db.get_index_names().count(), 0); + default_collection(db).delete_index("new_index").unwrap(); + assert_eq!(default_collection(db).get_index_names().unwrap().count(), 0); }); } @@ -214,19 +223,25 @@ fn full_index() { fn partial_index() { utils::with_db(|db| { assert!( - db.create_index( - "new_index", - &ValueIndexConfiguration::new( - QueryLanguage::JSON, - r#"{"WHAT": [[".id"]], "WHERE": ["=", [".someField"], "someValue"]}"#, - None - ), - ) - .unwrap() + default_collection(db) + .create_index( + "new_index", + &ValueIndexConfiguration::new( + QueryLanguage::JSON, + r#"{"WHAT": [[".id"]], "WHERE": ["=", [".someField"], "someValue"]}"#, + None + ), + ) + .unwrap() ); // Check index creation - let value = db.get_index_names().iter().next().unwrap(); + let value = default_collection(db) + .get_index_names() + .unwrap() + .iter() + .next() + .unwrap(); let name = value.as_string().unwrap(); assert_eq!(name, "new_index"); @@ -262,19 +277,25 @@ fn partial_index() { fn partial_index_with_condition_field() { utils::with_db(|db| { assert!( - db.create_index( - "new_index", - &ValueIndexConfiguration::new( - QueryLanguage::JSON, - r#"[[".id"]]"#, - Some(r#"["=", [".someField"], "someValue"]"#) - ), - ) - .unwrap() + default_collection(db) + .create_index( + "new_index", + &ValueIndexConfiguration::new( + QueryLanguage::JSON, + r#"[[".id"]]"#, + Some(r#"["=", [".someField"], "someValue"]"#) + ), + ) + .unwrap() ); // Check index creation - let value = db.get_index_names().iter().next().unwrap(); + let value = default_collection(db) + .get_index_names() + .unwrap() + .iter() + .next() + .unwrap(); let name = value.as_string().unwrap(); assert_eq!(name, "new_index"); diff --git a/tests/replicator_tests.rs b/tests/replicator_tests.rs index 6e922d7..b50ed62 100644 --- a/tests/replicator_tests.rs +++ b/tests/replicator_tests.rs @@ -25,6 +25,7 @@ use encryptable::Encryptable; use std::{time::Duration, thread}; pub mod utils; +use crate::utils::default_collection; //////// TESTS: @@ -42,7 +43,7 @@ fn push_replication() { // Check document is replicated to central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); }); @@ -62,7 +63,7 @@ fn pull_replication() { // Check document replicated to local assert!(utils::check_callback_with_wait( - || local_db.get_document("foo").is_ok(), + || default_collection(local_db).get_document("foo").is_ok(), None )); }); @@ -84,13 +85,13 @@ fn push_pull_replication() { // Check document replicated to central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); // Check document replicated to local DB 2 assert!(utils::check_callback_with_wait( - || local_db2.get_document("foo").is_ok(), + || default_collection(local_db2).get_document("foo").is_ok(), None )); }); @@ -115,7 +116,7 @@ fn pull_type_not_pushing() { // Check the replication process is not pushing to central assert!(!utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); }); @@ -140,7 +141,7 @@ fn push_type_not_pulling() { // Check document not replicated in local assert!(!utils::check_callback_with_wait( - || local_db.get_document("foo").is_ok(), + || default_collection(local_db).get_document("foo").is_ok(), None )); }); @@ -170,11 +171,11 @@ fn document_ids() { // Check only 'foo' is replicated assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); assert!(!utils::check_callback_with_wait( - || central_db.get_document("foo2").is_ok(), + || default_collection(central_db).get_document("foo2").is_ok(), None )); }); @@ -212,25 +213,25 @@ fn push_and_pull_filter() { // Check only 'foo' and 'foo2' were replicated to central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); assert!(utils::check_callback_with_wait( - || central_db.get_document("foo2").is_ok(), + || default_collection(central_db).get_document("foo2").is_ok(), None )); assert!(!utils::check_callback_with_wait( - || central_db.get_document("foo3").is_ok(), + || default_collection(central_db).get_document("foo3").is_ok(), None )); // Check only foo2' were replicated to DB 2 assert!(!utils::check_callback_with_wait( - || local_db2.get_document("foo").is_ok(), + || default_collection(local_db2).get_document("foo").is_ok(), None )); assert!(utils::check_callback_with_wait( - || local_db2.get_document("foo2").is_ok(), + || default_collection(local_db2).get_document("foo2").is_ok(), None )); }); @@ -269,11 +270,11 @@ fn conflict_resolver() { // Check 'foo' is replicated to central and DB 2 assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); assert!(utils::check_callback_with_wait( - || local_db2.get_document("foo").is_ok(), + || default_collection(local_db2).get_document("foo").is_ok(), None )); @@ -281,22 +282,22 @@ fn conflict_resolver() { repl1.stop(None); // Modify 'foo' in DB 1 - let mut foo = local_db1.get_document("foo").unwrap(); + let mut foo = default_collection(local_db1).get_document("foo").unwrap(); foo.mutable_properties().at("i").put_i64(i1); - local_db1 + default_collection(local_db1) .save_document_with_concurency_control(&mut foo, ConcurrencyControl::FailOnConflict) .expect("save"); // Modify 'foo' in DB 2 - let mut foo = local_db2.get_document("foo").unwrap(); + let mut foo = default_collection(local_db2).get_document("foo").unwrap(); foo.mutable_properties().at("i").put_i64(i2); - local_db2 + default_collection(local_db2) .save_document_with_concurency_control(&mut foo, ConcurrencyControl::FailOnConflict) .expect("save"); // Check DB 2 version is in central assert!(utils::check_callback_with_wait( - || central_db + || default_collection(central_db) .get_document("foo") .unwrap() .properties() @@ -314,7 +315,7 @@ fn conflict_resolver() { // Check DB 2 version is in DB 1 assert!(utils::check_callback_with_wait( - || local_db1 + || default_collection(local_db1) .get_document("foo") .unwrap() .properties() @@ -344,7 +345,7 @@ fn conflict_resolver_save_keep_local() { // Check 'foo' is replicated to central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); @@ -352,21 +353,21 @@ fn conflict_resolver_save_keep_local() { repl.stop(None); // Modify 'foo' in central - let mut foo = central_db.get_document("foo").unwrap(); + let mut foo = default_collection(central_db).get_document("foo").unwrap(); foo.mutable_properties().at("i").put_i64(i2); - central_db + default_collection(central_db) .save_document_with_concurency_control(&mut foo, ConcurrencyControl::FailOnConflict) .expect("save"); // Fetch 'foo' in DB 1 - let mut foo = local_db.get_document("foo").unwrap(); + let mut foo = default_collection(local_db).get_document("foo").unwrap(); // Restart replication repl.start(false); // Check central version of 'foo' is replicated to DB 1 assert!(utils::check_callback_with_wait( - || local_db + || default_collection(local_db) .get_document("foo") .expect("foo exists") .properties() @@ -379,14 +380,14 @@ fn conflict_resolver_save_keep_local() { // Modify 'foo' in DB1 from outdated document foo.mutable_properties().at("i").put_i64(i1); assert!( - local_db + default_collection(local_db) .save_document_resolving(&mut foo, move |_, _| true) .is_ok() ); // Assert conflict was resolved by keeping latest version assert!(utils::check_callback_with_wait( - || local_db + || default_collection(local_db) .get_document("foo") .expect("foo exists") .properties() @@ -398,7 +399,7 @@ fn conflict_resolver_save_keep_local() { // Check 'foo' new version replicated to central assert!(utils::check_callback_with_wait( - || central_db + || default_collection(central_db) .get_document("foo") .expect("foo exists") .properties() @@ -428,7 +429,7 @@ fn conflict_resolver_save_keep_remote() { // Check 'foo' is replicated to central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); @@ -436,21 +437,21 @@ fn conflict_resolver_save_keep_remote() { repl.stop(None); // Modify 'foo' in central - let mut foo = central_db.get_document("foo").unwrap(); + let mut foo = default_collection(central_db).get_document("foo").unwrap(); foo.mutable_properties().at("i").put_i64(i2); - central_db + default_collection(central_db) .save_document_with_concurency_control(&mut foo, ConcurrencyControl::FailOnConflict) .expect("save"); // Fetch 'foo' in DB 1 - let mut foo = local_db.get_document("foo").unwrap(); + let mut foo = default_collection(local_db).get_document("foo").unwrap(); // Restart replication repl.start(false); // Check central version of 'foo' is replicated to DB 1 assert!(utils::check_callback_with_wait( - || local_db + || default_collection(local_db) .get_document("foo") .expect("foo exists") .properties() @@ -463,14 +464,14 @@ fn conflict_resolver_save_keep_remote() { // Modify 'foo' in DB1 from outdated document foo.mutable_properties().at("i").put_i64(i1); assert!( - local_db + default_collection(local_db) .save_document_resolving(&mut foo, move |_, _| false) .is_err() ); // Assert conflict was resolved by keeping central's version assert!(utils::check_callback_with_wait( - || local_db + || default_collection(local_db) .get_document("foo") .expect("foo exists") .properties() @@ -482,7 +483,7 @@ fn conflict_resolver_save_keep_remote() { // Check 'foo' was unchanged in central assert!(utils::check_callback_with_wait( - || central_db + || default_collection(central_db) .get_document("foo") .expect("foo exists") .properties() @@ -599,7 +600,7 @@ fn encryption_ok_decryption_ok() { props .at("s") .put_encrypt(&Encryptable::create_with_string("test_encryption")); - local_db1 + default_collection(local_db1) .save_document_with_concurency_control( &mut doc_db1, ConcurrencyControl::FailOnConflict, @@ -609,22 +610,22 @@ fn encryption_ok_decryption_ok() { // Check document is replicated with data encrypted in central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); { - let doc_central = central_db.get_document("foo").unwrap(); + let doc_central = default_collection(central_db).get_document("foo").unwrap(); let dict = doc_central.properties(); assert!(dict.to_keys_hash_set().get("encrypted$s").is_some()); } // Check document is replicated with data decrypted in DB 2 assert!(utils::check_callback_with_wait( - || local_db2.get_document("foo").is_ok(), + || default_collection(local_db2).get_document("foo").is_ok(), None )); { - let doc_db2 = local_db2.get_document("foo").unwrap(); + let doc_db2 = default_collection(local_db2).get_document("foo").unwrap(); let dict = doc_db2.properties(); let value = dict.get("s"); assert!(value.is_encryptable()); @@ -660,21 +661,21 @@ fn encryption_error_temporary() { props .at("s") .put_encrypt(&Encryptable::create_with_string("test_encryption")); - local_db + default_collection(local_db) .save_document_with_concurency_control( &mut doc_db1, ConcurrencyControl::FailOnConflict, ) .expect("save"); } - assert!(local_db.get_document("foo").is_ok()); + assert!(default_collection(local_db).get_document("foo").is_ok()); // Manually trigger the replication repl.start(false); // Check document is not replicated in central because of the encryption error thread::sleep(Duration::from_secs(5)); - assert!(central_db.get_document("foo").is_err()); + assert!(default_collection(central_db).get_document("foo").is_err()); }); // Change local DB 1 replicator to make the encryption work @@ -692,7 +693,7 @@ fn encryption_error_temporary() { // Check document is replicated in central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); }); @@ -722,7 +723,7 @@ fn decryption_error_temporary() { let doc = r#"{"i":1234,"encrypted$s":{"alg":"CB_MOBILE_CUSTOM","ciphertext":"EkRVQ0RvVV5TQklARFlfXhI="}}"#; doc_db1.set_properties_as_json(&doc).unwrap(); - central_db + default_collection(central_db) .save_document_with_concurency_control( &mut doc_db1, ConcurrencyControl::FailOnConflict, @@ -730,14 +731,14 @@ fn decryption_error_temporary() { .expect("save"); } - assert!(central_db.get_document("foo").is_ok()); + assert!(default_collection(central_db).get_document("foo").is_ok()); // Manually trigger the replication repl.start(false); // Check document is not replicated in local because of the decryption error thread::sleep(Duration::from_secs(5)); - assert!(local_db.get_document("foo").is_err()); + assert!(default_collection(local_db).get_document("foo").is_err()); }); // Change local DB replicator to make the decryption work @@ -755,7 +756,7 @@ fn decryption_error_temporary() { // Check document is replicated in local assert!(utils::check_callback_with_wait( - || local_db.get_document("foo").is_ok(), + || default_collection(local_db).get_document("foo").is_ok(), None )); }); @@ -786,21 +787,21 @@ fn encryption_error_permanent() { props .at("s") .put_encrypt(&Encryptable::create_with_string("test_encryption")); - local_db + default_collection(local_db) .save_document_with_concurency_control( &mut doc_db1, ConcurrencyControl::FailOnConflict, ) .expect("save"); } - assert!(local_db.get_document("foo").is_ok()); + assert!(default_collection(local_db).get_document("foo").is_ok()); // Manually trigger the replication repl.start(false); // Check document is not replicated in central because of the encryption error thread::sleep(Duration::from_secs(5)); - assert!(central_db.get_document("foo").is_err()); + assert!(default_collection(central_db).get_document("foo").is_err()); }); // Change local DB 1 replicator to make the encryption work @@ -818,18 +819,18 @@ fn encryption_error_permanent() { // Check document is not replicated in central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_err(), + || default_collection(central_db).get_document("foo").is_err(), None )); }); tester.test(|local_db, central_db, repl| { // Create new revision for document 'foo' in local - let mut doc = local_db.get_document("foo").unwrap(); + let mut doc = default_collection(local_db).get_document("foo").unwrap(); let mut props = doc.mutable_properties(); props.at("i").put_i64(1235); - local_db + default_collection(local_db) .save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) .expect("save"); @@ -838,7 +839,7 @@ fn encryption_error_permanent() { // Check document is replicated in central assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); }); @@ -868,7 +869,7 @@ fn decryption_error_permanent() { let doc = r#"{"i":1234,"encrypted$s":{"alg":"CB_MOBILE_CUSTOM","ciphertext":"EkRVQ0RvVV5TQklARFlfXhI="}}"#; doc_db1.set_properties_as_json(&doc).unwrap(); - central_db + default_collection(central_db) .save_document_with_concurency_control( &mut doc_db1, ConcurrencyControl::FailOnConflict, @@ -876,14 +877,14 @@ fn decryption_error_permanent() { .expect("save"); } - assert!(central_db.get_document("foo").is_ok()); + assert!(default_collection(central_db).get_document("foo").is_ok()); // Manually trigger the replication repl.start(false); // Check document is not replicated in local because of the decryption error thread::sleep(Duration::from_secs(5)); - assert!(local_db.get_document("foo").is_err()); + assert!(default_collection(local_db).get_document("foo").is_err()); }); // Change local DB replicator to make the decryption work @@ -901,18 +902,18 @@ fn decryption_error_permanent() { // Check document is not replicated in local assert!(utils::check_callback_with_wait( - || local_db.get_document("foo").is_err(), + || default_collection(local_db).get_document("foo").is_err(), None )); }); tester.test(|local_db, central_db, repl| { // Create new revision for document 'foo' in central - let mut doc = central_db.get_document("foo").unwrap(); + let mut doc = default_collection(central_db).get_document("foo").unwrap(); let mut props = doc.mutable_properties(); props.at("i").put_i64(1235); - central_db + default_collection(central_db) .save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) .expect("save"); @@ -921,7 +922,7 @@ fn decryption_error_permanent() { // Check document is replicated in local assert!(utils::check_callback_with_wait( - || local_db.get_document("foo").is_ok(), + || default_collection(local_db).get_document("foo").is_ok(), None )); }); @@ -950,7 +951,7 @@ mod unsafe_test { // Check the replication process is not running automatically assert!(!utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); @@ -959,7 +960,7 @@ mod unsafe_test { // Check the replication was successful assert!(utils::check_callback_with_wait( - || central_db.get_document("foo").is_ok(), + || default_collection(central_db).get_document("foo").is_ok(), None )); }); diff --git a/tests/utils.rs b/tests/utils.rs index c16f877..ba9e045 100644 --- a/tests/utils.rs +++ b/tests/utils.rs @@ -10,7 +10,7 @@ use std::{ }; #[cfg(feature = "enterprise")] use std::collections::HashMap; -use couchbase_lite::logging::CustomLogSink; +use couchbase_lite::{collection::Collection, logging::CustomLogSink}; pub const DB_NAME: &str = "test_db"; @@ -488,10 +488,16 @@ pub fn add_doc(db: &mut Database, id: &str, i: i64, s: &str) { let mut props = doc.mutable_properties(); props.at("i").put_i64(i); props.at("s").put_string(s); - db.save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) + db.default_collection_or_error() + .unwrap() + .save_document_with_concurency_control(&mut doc, ConcurrencyControl::FailOnConflict) .expect("save"); } +pub fn default_collection(db: &Database) -> Collection { + db.default_collection_or_error().unwrap() +} + // Static pub fn get_static(st: &Arc>) -> MutexGuard {