From 97c78e77637a02181d9c735c17e39a9365b55e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Mon, 30 Oct 2023 18:02:44 +0800 Subject: [PATCH] test(file_store): add construction method tests --- crates/file_store/src/store.rs | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/file_store/src/store.rs b/crates/file_store/src/store.rs index 22e8b39d68..8af10cbd0e 100644 --- a/crates/file_store/src/store.rs +++ b/crates/file_store/src/store.rs @@ -216,6 +216,44 @@ mod test { #[derive(Debug)] struct TestTracker; + /// Check behavior of [`Store::create_new`] and [`Store::open`]. + #[test] + fn construct_store() { + let temp_dir = tempfile::tempdir().unwrap(); + let file_path = temp_dir.path().join("db_file"); + let _ = Store::::open(&TEST_MAGIC_BYTES, &file_path) + .expect_err("must not open as file does not exist yet"); + let _ = Store::::create_new(&TEST_MAGIC_BYTES, &file_path) + .expect("must create file"); + // cannot create new as file already exists + let _ = Store::::create_new(&TEST_MAGIC_BYTES, &file_path) + .expect_err("must fail as file already exists now"); + let _ = Store::::open(&TEST_MAGIC_BYTES, &file_path) + .expect("must open as file exists now"); + } + + #[test] + fn open_or_create_new() { + let temp_dir = tempfile::tempdir().unwrap(); + let file_path = temp_dir.path().join("db_file"); + let changeset = vec!["hello".to_string(), "world".to_string()]; + + { + let mut db = Store::::open_or_create_new(&TEST_MAGIC_BYTES, &file_path) + .expect("must create"); + assert!(file_path.exists()); + db.append_changeset(&changeset).expect("must succeed"); + } + + { + let mut db = Store::::open_or_create_new(&TEST_MAGIC_BYTES, &file_path) + .expect("must recover"); + let (recovered_changeset, r) = db.aggregate_changesets(); + r.expect("must succeed"); + assert_eq!(recovered_changeset, changeset); + } + } + #[test] fn is_empty() { let mut file = NamedTempFile::new().unwrap();