All the tests now pass with the broken out classes
[dbsrgits/DBM-Deep.git] / t / 35_transaction_multiple.t
1 use strict;
2 use Test::More tests => 51;
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     num_txns  => 16,
14 );
15
16 my $db2 = DBM::Deep->new(
17     file => $filename,
18     locking => 1,
19     autoflush => 1,
20     num_txns  => 16,
21 );
22
23 my $db3 = DBM::Deep->new(
24     file => $filename,
25     locking => 1,
26     autoflush => 1,
27     num_txns  => 16,
28 );
29
30 $db1->{foo} = 'bar';
31 is( $db1->{foo}, 'bar', "Before transaction, DB1's foo is bar" );
32 is( $db2->{foo}, 'bar', "Before transaction, DB2's foo is bar" );
33 is( $db3->{foo}, 'bar', "Before transaction, DB3's foo is bar" );
34
35 $db1->begin_work;
36
37 is( $db1->{foo}, 'bar', "Before transaction work, DB1's foo is bar" );
38 is( $db2->{foo}, 'bar', "Before transaction work, DB2's foo is bar" );
39 is( $db3->{foo}, 'bar', "Before transaction work, DB3's foo is bar" );
40
41 $db1->{foo} = 'bar2';
42
43 is( $db1->{foo}, 'bar2', "After DB1 foo to bar2, DB1's foo is bar2" );
44 is( $db2->{foo}, 'bar', "After DB1 foo to bar2, DB2's foo is bar" );
45 is( $db3->{foo}, 'bar', "After DB1 foo to bar2, DB3's foo is bar" );
46
47 $db1->{bar} = 'foo';
48
49 ok(  exists $db1->{bar}, "After DB1 set bar to foo, DB1's bar exists" );
50 ok( !exists $db2->{bar}, "After DB1 set bar to foo, DB2's bar doesn't exist" );
51 ok( !exists $db3->{bar}, "After DB1 set bar to foo, DB3's bar doesn't exist" );
52  
53 $db2->begin_work;
54
55 is( $db1->{foo}, 'bar2', "After DB2 transaction begin, DB1's foo is still bar2" );
56 is( $db2->{foo}, 'bar', "After DB2 transaction begin, DB2's foo is still bar" );
57 is( $db3->{foo}, 'bar', "After DB2 transaction begin, DB3's foo is still bar" );
58
59 ok(  exists $db1->{bar}, "After DB2 transaction begin, DB1's bar exists" );
60 ok( !exists $db2->{bar}, "After DB2 transaction begin, DB2's bar doesn't exist" );
61 ok( !exists $db3->{bar}, "After DB2 transaction begin, DB3's bar doesn't exist" );
62
63 $db2->{foo} = 'bar333';
64
65 is( $db1->{foo}, 'bar2', "After DB2 foo to bar2, DB1's foo is bar2" );
66 is( $db2->{foo}, 'bar333', "After DB2 foo to bar2, DB2's foo is bar333" );
67 is( $db3->{foo}, 'bar', "After DB2 foo to bar2, DB3's foo is bar" );
68
69 $db2->{bar} = 'mybar';
70
71 ok(  exists $db1->{bar}, "After DB2 set bar to mybar, DB1's bar exists" );
72 ok(  exists $db2->{bar}, "After DB2 set bar to mybar, DB2's bar exists" );
73 ok( !exists $db3->{bar}, "After DB2 set bar to mybar, DB3's bar doesn't exist" );
74
75 is( $db1->{bar}, 'foo', "DB1's bar is still foo" );
76 is( $db2->{bar}, 'mybar', "DB2's bar is now mybar" );
77
78 $db2->{mykey} = 'myval';
79
80 ok( !exists $db1->{mykey}, "After DB2 set mykey to myval, DB1's mykey doesn't exist" );
81 ok(  exists $db2->{mykey}, "After DB2 set mykey to myval, DB2's mykey exists" );
82 ok( !exists $db3->{mykey}, "After DB2 set mykey to myval, DB3's mykey doesn't exist" );
83
84 cmp_bag( [ keys %$db1 ], [qw( foo bar )], "DB1 keys correct" );
85 cmp_bag( [ keys %$db2 ], [qw( foo bar mykey )], "DB2 keys correct" );
86 cmp_bag( [ keys %$db3 ], [qw( foo )], "DB3 keys correct" );
87
88 $db1->commit;
89
90 is( $db1->{foo}, 'bar2', "After DB1 commit, DB1's foo is bar2" );
91 is( $db2->{foo}, 'bar333', "After DB1 commit, DB2's foo is bar333" );
92 is( $db3->{foo}, 'bar2', "After DB1 commit, DB3's foo is bar2" );
93
94 is( $db1->{bar}, 'foo', "DB1's bar is still foo" );
95 is( $db2->{bar}, 'mybar', "DB2's bar is still mybar" );
96 is( $db3->{bar}, 'foo', "DB3's bar is now foo" );
97
98 cmp_bag( [ keys %$db1 ], [qw( foo bar )], "DB1 keys correct" );
99 cmp_bag( [ keys %$db2 ], [qw( foo bar mykey )], "DB2 keys correct" );
100 cmp_bag( [ keys %$db3 ], [qw( foo bar )], "DB3 keys correct" );
101
102 $db2->commit;
103
104 is( $db1->{foo}, 'bar333', "After DB2 commit, DB1's foo is bar333" );
105 is( $db2->{foo}, 'bar333', "After DB2 commit, DB2's foo is bar333" );
106 is( $db3->{foo}, 'bar333', "After DB2 commit, DB3's foo is bar333" );
107
108 is( $db1->{bar}, 'mybar', "DB1's bar is now mybar" );
109 is( $db2->{bar}, 'mybar', "DB2's bar is still mybar" );
110 is( $db3->{bar}, 'mybar', "DB3's bar is now mybar" );
111
112 cmp_bag( [ keys %$db1 ], [qw( foo bar mykey )], "DB1 keys correct" );
113 cmp_bag( [ keys %$db2 ], [qw( foo bar mykey )], "DB2 keys correct" );
114 cmp_bag( [ keys %$db3 ], [qw( foo bar mykey )], "DB3 keys correct" );