Added tests for clear() and keys()/length() in the big hash/array tests
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
CommitLineData
fee0243f 1use strict;
42717e46 2use Test::More tests => 56;
3use Test::Deep;
fee0243f 4use t::common qw( new_fh );
5
6use_ok( 'DBM::Deep' );
7
8my ($fh, $filename) = new_fh();
21838116 9my $db1 = DBM::Deep->new(
fee0243f 10 file => $filename,
21838116 11 locking => 1,
c9b6d0d8 12 autoflush => 1,
fee0243f 13);
14
21838116 15my $db2 = DBM::Deep->new(
16 file => $filename,
17 locking => 1,
c9b6d0d8 18 autoflush => 1,
21838116 19);
20
21$db1->{x} = 'y';
22is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
23is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
24
25$db1->begin_work;
26
633df1fd 27 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
28 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
21838116 29
633df1fd 30 $db1->{x} = 'z';
31 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
32 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
21838116 33
633df1fd 34 $db2->{other_x} = 'foo';
35 is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
36 is( $db1->{other_x}, undef, "Since other_x was added after the transaction began, DB1 doesn't see it." );
21838116 37
42717e46 38TODO: {
39 local $TODO = "keys aren't working yet";
40 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
41}
42 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
43
21838116 44$db1->rollback;
45
46is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
47is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
48
49is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
50is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
460b1067 51
6677de37 52$db1->begin_work;
53
633df1fd 54 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
55 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
56
57 $db1->{x} = 'z';
58 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
59 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
6677de37 60
42717e46 61 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
62 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
63
6677de37 64$db1->commit;
65
66is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
67is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
68
69$db1->begin_work;
70
633df1fd 71 delete $db2->{other_x};
94e8af14 72 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
633df1fd 73 is( $db1->{other_x}, 'foo', "Since other_x was deleted after the transaction began, DB1 still sees it." );
6677de37 74
42717e46 75 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
76 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
77
633df1fd 78 delete $db1->{x};
94e8af14 79 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
22e20cce 80 is( $db2->{x}, 'z', "But, DB2 can still see it" );
94e8af14 81
42717e46 82TODO: {
83 local $TODO = "keys aren't working yet";
84 cmp_bag( [ keys %$db1 ], [qw( other_x )], "DB1 keys correct" );
85}
86 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
87
6677de37 88$db1->rollback;
89
42717e46 90ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
91ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
6677de37 92
93is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
94is( $db2->{x}, 'z', "DB2 can still see it" );
95
42717e46 96cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
97cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
98
6677de37 99$db1->begin_work;
100
633df1fd 101 delete $db1->{x};
94e8af14 102 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
633df1fd 103 is( $db2->{x}, 'z', "But, DB2 can still see it" );
6677de37 104
42717e46 105TODO: {
106 local $TODO = "keys aren't working yet";
107 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
108}
109 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
110
6677de37 111$db1->commit;
112
42717e46 113ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
114ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
94e8af14 115
116$db1->{foo} = 'bar';
117is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
118is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
119
42717e46 120cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
121cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
122
94e8af14 123TODO: {
42717e46 124 todo_skip 'Still need to work on clear()', 4;
94e8af14 125
126$db1->begin_work;
127
128 %$db1 = (); # clear()
129 ok( !exists $db1->{foo}, "Cleared foo" );
130 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
131
42717e46 132 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
133 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
134
94e8af14 135$db1->rollback;
136
137is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
138is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
42717e46 139
140cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
141cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
142
94e8af14 143}
42717e46 144
145$db1->optimize;
146is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
147is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
148
149cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
150cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
151
152$db1->begin_work;
153
154 cmp_ok( $db1->_fileobj->transaction_id, '==', 1, "Transaction ID has been reset after optimize" );
155
156$db1->rollback;
157
158__END__
159
160Tests to add:
161* Two transactions running at the same time
162* Doing a clear on the head while a transaction is running
163# More than just two keys
164* Arrays (in particular, how is length handled?)