2 use Test::More tests => 99;
5 use t::common qw( new_fh );
9 my ($fh, $filename) = new_fh();
10 my $db1 = DBM::Deep->new(
17 my $db2 = DBM::Deep->new(
25 is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
26 is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
28 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
29 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
33 } qr/Cannot rollback without an active transaction/, "Attempting to rollback without a transaction throws an error";
37 } qr/Cannot commit without an active transaction/, "Attempting to commit without a transaction throws an error";
43 } qr/Cannot begin_work within an active transaction/, "Attempting to begin_work within a transaction throws an error";
47 } "Rolling back an empty transaction is ok.";
49 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
52 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
59 } "Committing an empty transaction is ok.";
61 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
62 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
66 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
67 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
69 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
70 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
73 is( $db1->{x}, 'y', "Within DB1 transaction, DB1's X is still Y" );
74 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is now A" );
77 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
78 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is still A" );
81 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
82 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
84 $db2->{other_x} = 'foo';
85 is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
86 ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." );
88 # Reset to an expected value
90 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is istill Z" );
91 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is now Y" );
93 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
94 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
98 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
99 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
101 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
102 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
106 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
107 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
109 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
110 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
113 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
114 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
116 $db2->{other_x} = 'bar';
117 is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
118 is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
121 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
122 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
124 cmp_bag( [ keys %$db1 ], [qw( x other_x z )], "DB1 keys correct" );
125 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
129 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
130 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
132 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
133 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
135 is( $db1->{other_x}, 'bar', "After commit, DB1's other_x is bar" );
136 is( $db2->{other_x}, 'bar', "After commit, DB2's other_x is bar" );
140 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
141 cmp_bag( [ keys %$db2 ], [qw( x z other_x )], "DB2 keys correct" );
143 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
144 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
146 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
147 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
149 is( $db1->{other_x}, 'bar', "After begin_work, DB1's other_x is still bar" );
150 is( $db2->{other_x}, 'bar', "After begin_work, DB2's other_x is still bar" );
152 delete $db2->{other_x};
153 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
154 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
156 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
157 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
160 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
161 is( $db2->{x}, 'z', "But, DB2 can still see it" );
163 cmp_bag( [ keys %$db1 ], [qw( other_x z )], "DB1 keys correct" );
164 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
168 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
169 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
171 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
172 is( $db2->{x}, 'z', "DB2 can still see it" );
174 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
175 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
180 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
182 is( $db2->{x}, 'z', "But, DB2 can still see it" );
184 cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" );
185 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
189 ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
190 ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
193 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
194 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
196 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
197 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
201 %$db1 = (); # clear()
202 ok( !exists $db1->{foo}, "Cleared foo" );
203 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
205 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
206 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
210 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
211 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
213 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
214 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
217 skip "Optimize tests skipped on Win32", 7
218 if $^O eq 'MSWin32' || $^O eq 'cygwin';
222 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
223 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
225 is( $db1->{z}, 'a', 'After optimize, everything is ok' );
226 is( $db2->{z}, 'a', 'After optimize, everything is ok' );
228 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
229 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
233 cmp_ok( $db1->_engine->trans_id, '==', 1, "Transaction ID has been reset after optimize" );