30e2fc9a4fd8e9747f0be6bb848f4b6e3dcc3c82
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
1 use strict;
2 use Test::More tests => 29;
3 use Test::Exception;
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 $db1->rollback;
39
40 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
41 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
42
43 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
44 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
45
46 $db1->begin_work;
47
48 $db1->{x} = 'z';
49 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
50 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
51
52 $db1->commit;
53
54 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
55 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
56
57 $db1->begin_work;
58
59 delete $db2->{other_x};
60 is( $db2->{other_x}, undef, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
61 is( $db1->{other_x}, 'foo', "Since other_x was deleted after the transaction began, DB1 still sees it." );
62
63 delete $db1->{x};
64 is( $db1->{x}, undef, "DB1 deleted X in a transaction, so it can't see it anymore" );
65 is( $db2->{x}, 'z', "But, DB2 can still see it" );
66
67 $db1->rollback;
68
69 is( $db2->{other_x}, undef, "It's still deleted for DB2" );
70 is( $db1->{other_x}, undef, "And now DB1 sees the deletion" );
71
72 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
73 is( $db2->{x}, 'z', "DB2 can still see it" );
74
75 $db1->begin_work;
76
77 delete $db1->{x};
78 is( $db1->{x}, undef, "DB1 deleted X in a transaction, so it can't see it anymore" );
79 is( $db2->{x}, 'z', "But, DB2 can still see it" );
80
81 $db1->commit;
82
83 is( $db1->{x}, undef, "The transaction was committed, so DB1 still deleted X" );
84 is( $db2->{x}, undef, "DB2 can now see the deletion of X" );