Converted to use the intermediate keyloc so that keys work under transactions
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
index 9af696e..65ea8fc 100644 (file)
@@ -1,6 +1,6 @@
 use strict;
-use Test::More tests => 37;
-use Test::Exception;
+use Test::More tests => 58;
+use Test::Deep;
 use t::common qw( new_fh );
 
 use_ok( 'DBM::Deep' );
@@ -35,6 +35,9 @@ $db1->begin_work;
     is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
     is( $db1->{other_x}, undef, "Since other_x was added after the transaction began, DB1 doesn't see it." );
 
+    cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
+    cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
+
 $db1->rollback;
 
 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
@@ -52,6 +55,13 @@ $db1->begin_work;
     is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
     is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
 
+    $db2->{other_x} = 'bar';
+    is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
+    is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
+
+    cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
+    cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
+
 $db1->commit;
 
 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
@@ -61,37 +71,53 @@ $db1->begin_work;
 
     delete $db2->{other_x};
     ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
-    is( $db1->{other_x}, 'foo', "Since other_x was deleted after the transaction began, DB1 still sees it." );
+    is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
+
+    cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
+    cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
 
     delete $db1->{x};
     ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
     is( $db2->{x}, 'z', "But, DB2 can still see it" );
 
+    cmp_bag( [ keys %$db1 ], [qw( other_x )], "DB1 keys correct" );
+    cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
+
 $db1->rollback;
 
-is( $db2->{other_x}, undef, "It's still deleted for DB2" );
-is( $db1->{other_x}, undef, "And now DB1 sees the deletion" );
+ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
+ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
 
 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
 is( $db2->{x}, 'z', "DB2 can still see it" );
 
+cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
+cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
+
 $db1->begin_work;
 
     delete $db1->{x};
     ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
+#__END__
     is( $db2->{x}, 'z', "But, DB2 can still see it" );
 
+    cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
+    cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
+
 $db1->commit;
 
-is( $db1->{x}, undef, "The transaction was committed, so DB1 still deleted X" );
-is( $db2->{x}, undef, "DB2 can now see the deletion of X" );
+ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
+ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
 
 $db1->{foo} = 'bar';
 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
 
+cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
+cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
+
 TODO: {
-    local $TODO = 'Still need to work on clear()';
+    todo_skip 'Still need to work on clear()', 4;
 
 $db1->begin_work;
 
@@ -99,8 +125,36 @@ $db1->begin_work;
     ok( !exists $db1->{foo}, "Cleared foo" );
     is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
 
+    cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
+    cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
+
 $db1->rollback;
 
 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
+
+cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
+cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
+
 }
+
+$db1->optimize;
+is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
+is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
+
+cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
+cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
+
+$db1->begin_work;
+
+    cmp_ok( $db1->_fileobj->transaction_id, '==', 1, "Transaction ID has been reset after optimize" );
+
+$db1->rollback;
+
+__END__
+
+Tests to add:
+* Two transactions running at the same time
+* Doing a clear on the head while a transaction is running
+# More than just two keys
+* Arrays (in particular, how is length handled?)