@@ -216,6 +216,44 @@ mod test {
216216 #[ derive( Debug ) ]
217217 struct TestTracker ;
218218
219+ /// Check behavior of [`Store::create_new`] and [`Store::open`].
220+ #[ test]
221+ fn construct_store ( ) {
222+ let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
223+ let file_path = temp_dir. path ( ) . join ( "db_file" ) ;
224+ let _ = Store :: < TestChangeSet > :: open ( & TEST_MAGIC_BYTES , & file_path)
225+ . expect_err ( "must not open as file does not exist yet" ) ;
226+ let _ = Store :: < TestChangeSet > :: create_new ( & TEST_MAGIC_BYTES , & file_path)
227+ . expect ( "must create file" ) ;
228+ // cannot create new as file already exists
229+ let _ = Store :: < TestChangeSet > :: create_new ( & TEST_MAGIC_BYTES , & file_path)
230+ . expect_err ( "must fail as file already exists now" ) ;
231+ let _ = Store :: < TestChangeSet > :: open ( & TEST_MAGIC_BYTES , & file_path)
232+ . expect ( "must open as file exists now" ) ;
233+ }
234+
235+ #[ test]
236+ fn open_or_create_new ( ) {
237+ let temp_dir = tempfile:: tempdir ( ) . unwrap ( ) ;
238+ let file_path = temp_dir. path ( ) . join ( "db_file" ) ;
239+ let changeset = vec ! [ "hello" . to_string( ) , "world" . to_string( ) ] ;
240+
241+ {
242+ let mut db = Store :: < TestChangeSet > :: open_or_create_new ( & TEST_MAGIC_BYTES , & file_path)
243+ . expect ( "must create" ) ;
244+ assert ! ( file_path. exists( ) ) ;
245+ db. append_changeset ( & changeset) . expect ( "must succeed" ) ;
246+ }
247+
248+ {
249+ let mut db = Store :: < TestChangeSet > :: open_or_create_new ( & TEST_MAGIC_BYTES , & file_path)
250+ . expect ( "must recover" ) ;
251+ let ( recovered_changeset, r) = db. aggregate_changesets ( ) ;
252+ r. expect ( "must succeed" ) ;
253+ assert_eq ! ( recovered_changeset, changeset) ;
254+ }
255+ }
256+
219257 #[ test]
220258 fn is_empty ( ) {
221259 let mut file = NamedTempFile :: new ( ) . unwrap ( ) ;
0 commit comments