More failing tests, particularly for keys() and transactions.
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
1 use strict;
2 use Test::More tests => 56;
3 use Test::Deep;
4 use t::common qw( new_fh );
5
6 use_ok( 'DBM::Deep' );
7
8 my ($fh, $filename) = new_fh();
9 my $db1 = DBM::Deep->new(
10     file => $filename,
11     locking => 1,
12     autoflush => 1,
13 );
14
15 my $db2 = DBM::Deep->new(
16     file => $filename,
17     locking => 1,
18     autoflush => 1,
19 );
20
21 $db1->{x} = 'y';
22 is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
23 is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
24
25 $db1->begin_work;
26
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" );
29
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" );
33
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." );
37
38 TODO: {
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
44 $db1->rollback;
45
46 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
47 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
48
49 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
50 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
51
52 $db1->begin_work;
53
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" );
60
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
64 $db1->commit;
65
66 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
67 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
68
69 $db1->begin_work;
70
71     delete $db2->{other_x};
72     ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
73     is( $db1->{other_x}, 'foo', "Since other_x was deleted after the transaction began, DB1 still sees it." );
74
75     cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
76     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
77
78     delete $db1->{x};
79     ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
80     is( $db2->{x}, 'z', "But, DB2 can still see it" );
81
82 TODO: {
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
88 $db1->rollback;
89
90 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
91 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
92
93 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
94 is( $db2->{x}, 'z', "DB2 can still see it" );
95
96 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
97 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
98
99 $db1->begin_work;
100
101     delete $db1->{x};
102     ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
103     is( $db2->{x}, 'z', "But, DB2 can still see it" );
104
105 TODO: {
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
111 $db1->commit;
112
113 ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
114 ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
115
116 $db1->{foo} = 'bar';
117 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
118 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
119
120 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
121 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
122
123 TODO: {
124     todo_skip 'Still need to work on clear()', 4;
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
132     cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
133     cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
134
135 $db1->rollback;
136
137 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
138 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
139
140 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
141 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
142
143 }
144
145 $db1->optimize;
146 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
147 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
148
149 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
150 cmp_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
160 Tests 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?)