Added supports() and rewrote the tests so that Engine::DBI doesn't run the transactio...
[dbsrgits/DBM-Deep.git] / t / 35_transaction_multiple.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5 use Test::Deep;
6 use t::common qw( new_dbm );
7
8 use_ok( 'DBM::Deep' );
9
10 my $dbm_factory = new_dbm(
11     locking => 1,
12     autoflush => 1,
13     num_txns  => 16,
14 );
15 while ( my $dbm_maker = $dbm_factory->() ) {
16     my $db1 = $dbm_maker->();
17     next unless $db1->supports( 'transactions' );
18     my $db2 = $dbm_maker->();
19     my $db3 = $dbm_maker->();
20
21     $db1->{foo} = 'bar';
22     is( $db1->{foo}, 'bar', "Before transaction, DB1's foo is bar" );
23     is( $db2->{foo}, 'bar', "Before transaction, DB2's foo is bar" );
24     is( $db3->{foo}, 'bar', "Before transaction, DB3's foo is bar" );
25
26     $db1->begin_work;
27
28     is( $db1->{foo}, 'bar', "Before transaction work, DB1's foo is bar" );
29     is( $db2->{foo}, 'bar', "Before transaction work, DB2's foo is bar" );
30     is( $db3->{foo}, 'bar', "Before transaction work, DB3's foo is bar" );
31
32     $db1->{foo} = 'bar2';
33
34     is( $db1->{foo}, 'bar2', "After DB1 foo to bar2, DB1's foo is bar2" );
35     is( $db2->{foo}, 'bar', "After DB1 foo to bar2, DB2's foo is bar" );
36     is( $db3->{foo}, 'bar', "After DB1 foo to bar2, DB3's foo is bar" );
37
38     $db1->{bar} = 'foo';
39
40     ok(  exists $db1->{bar}, "After DB1 set bar to foo, DB1's bar exists" );
41     ok( !exists $db2->{bar}, "After DB1 set bar to foo, DB2's bar doesn't exist" );
42     ok( !exists $db3->{bar}, "After DB1 set bar to foo, DB3's bar doesn't exist" );
43      
44     $db2->begin_work;
45
46     is( $db1->{foo}, 'bar2', "After DB2 transaction begin, DB1's foo is still bar2" );
47     is( $db2->{foo}, 'bar', "After DB2 transaction begin, DB2's foo is still bar" );
48     is( $db3->{foo}, 'bar', "After DB2 transaction begin, DB3's foo is still bar" );
49
50     ok(  exists $db1->{bar}, "After DB2 transaction begin, DB1's bar exists" );
51     ok( !exists $db2->{bar}, "After DB2 transaction begin, DB2's bar doesn't exist" );
52     ok( !exists $db3->{bar}, "After DB2 transaction begin, DB3's bar doesn't exist" );
53
54     $db2->{foo} = 'bar333';
55
56     is( $db1->{foo}, 'bar2', "After DB2 foo to bar2, DB1's foo is bar2" );
57     is( $db2->{foo}, 'bar333', "After DB2 foo to bar2, DB2's foo is bar333" );
58     is( $db3->{foo}, 'bar', "After DB2 foo to bar2, DB3's foo is bar" );
59
60     $db2->{bar} = 'mybar';
61
62     ok(  exists $db1->{bar}, "After DB2 set bar to mybar, DB1's bar exists" );
63     ok(  exists $db2->{bar}, "After DB2 set bar to mybar, DB2's bar exists" );
64     ok( !exists $db3->{bar}, "After DB2 set bar to mybar, DB3's bar doesn't exist" );
65
66     is( $db1->{bar}, 'foo', "DB1's bar is still foo" );
67     is( $db2->{bar}, 'mybar', "DB2's bar is now mybar" );
68
69     $db2->{mykey} = 'myval';
70
71     ok( !exists $db1->{mykey}, "After DB2 set mykey to myval, DB1's mykey doesn't exist" );
72     ok(  exists $db2->{mykey}, "After DB2 set mykey to myval, DB2's mykey exists" );
73     ok( !exists $db3->{mykey}, "After DB2 set mykey to myval, DB3's mykey doesn't exist" );
74
75     cmp_bag( [ keys %$db1 ], [qw( foo bar )], "DB1 keys correct" );
76     cmp_bag( [ keys %$db2 ], [qw( foo bar mykey )], "DB2 keys correct" );
77     cmp_bag( [ keys %$db3 ], [qw( foo )], "DB3 keys correct" );
78
79     $db1->commit;
80
81     is( $db1->{foo}, 'bar2', "After DB1 commit, DB1's foo is bar2" );
82     is( $db2->{foo}, 'bar333', "After DB1 commit, DB2's foo is bar333" );
83     is( $db3->{foo}, 'bar2', "After DB1 commit, DB3's foo is bar2" );
84
85     is( $db1->{bar}, 'foo', "DB1's bar is still foo" );
86     is( $db2->{bar}, 'mybar', "DB2's bar is still mybar" );
87     is( $db3->{bar}, 'foo', "DB3's bar is now foo" );
88
89     cmp_bag( [ keys %$db1 ], [qw( foo bar )], "DB1 keys correct" );
90     cmp_bag( [ keys %$db2 ], [qw( foo bar mykey )], "DB2 keys correct" );
91     cmp_bag( [ keys %$db3 ], [qw( foo bar )], "DB3 keys correct" );
92
93     $db2->commit;
94
95     is( $db1->{foo}, 'bar333', "After DB2 commit, DB1's foo is bar333" );
96     is( $db2->{foo}, 'bar333', "After DB2 commit, DB2's foo is bar333" );
97     is( $db3->{foo}, 'bar333', "After DB2 commit, DB3's foo is bar333" );
98
99     is( $db1->{bar}, 'mybar', "DB1's bar is now mybar" );
100     is( $db2->{bar}, 'mybar', "DB2's bar is still mybar" );
101     is( $db3->{bar}, 'mybar', "DB3's bar is now mybar" );
102
103     cmp_bag( [ keys %$db1 ], [qw( foo bar mykey )], "DB1 keys correct" );
104     cmp_bag( [ keys %$db2 ], [qw( foo bar mykey )], "DB2 keys correct" );
105     cmp_bag( [ keys %$db3 ], [qw( foo bar mykey )], "DB3 keys correct" );
106 }
107
108 done_testing;