Fixed immediate dependence on DBI
[dbsrgits/DBM-Deep.git] / t / 41_transaction_multilevel.t
CommitLineData
2120a181 1use strict;
350896ee 2use Test::More;
2120a181 3use Test::Deep;
0e3e3555 4use t::common qw( new_dbm );
2120a181 5
6use_ok( 'DBM::Deep' );
7
0e3e3555 8my $dbm_factory = new_dbm(
9 locking => 1,
2120a181 10 autoflush => 1,
c57b19c6 11 num_txns => 2,
2120a181 12);
0e3e3555 13while ( my $dbm_maker = $dbm_factory->() ) {
14 my $db1 = $dbm_maker->();
580e5ee2 15 next unless $db1->supports('transactions');
0e3e3555 16 my $db2 = $dbm_maker->();
2120a181 17
0e3e3555 18 $db1->{x} = { xy => { foo => 'y' } };
19 is( $db1->{x}{xy}{foo}, 'y', "Before transaction, DB1's X is Y" );
20 is( $db2->{x}{xy}{foo}, 'y', "Before transaction, DB2's X is Y" );
21
22 $db1->begin_work;
23
24 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
25 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
26
27 cmp_bag( [ keys %{$db1->{x}} ], [qw( xy )], "DB1->X keys correct" );
28 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
2120a181 29
0e3e3555 30 cmp_bag( [ keys %{$db1->{x}{xy}} ], [qw( foo )], "DB1->X->XY keys correct" );
31 cmp_bag( [ keys %{$db2->{x}{xy}} ], [qw( foo )], "DB2->X->XY keys correct" );
2120a181 32
0e3e3555 33 is( $db1->{x}{xy}{foo}, 'y', "After transaction, DB1's X is Y" );
34 is( $db2->{x}{xy}{foo}, 'y', "After transaction, DB2's X is Y" );
35
36 $db1->{x} = { yz => { bar => 30 } };
37 ok( !exists $db1->{x}{xy}, "DB1: After reassignment of X, X->XY is gone" );
38 is( $db2->{x}{xy}{foo}, 'y', "DB2: After reassignment of DB1 X, X is Y" );
39
40 cmp_bag( [ keys %{$db1->{x}} ], [qw( yz )], "DB1->X keys correct" );
41 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
42
43 $db1->rollback;
2120a181 44
45 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
46 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
47
45f047f8 48 cmp_bag( [ keys %{$db1->{x}} ], [qw( xy )], "DB1->X keys correct" );
49 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
2120a181 50
45f047f8 51 cmp_bag( [ keys %{$db1->{x}{xy}} ], [qw( foo )], "DB1->X->XY keys correct" );
52 cmp_bag( [ keys %{$db2->{x}{xy}} ], [qw( foo )], "DB2->X->XY keys correct" );
2120a181 53
0e3e3555 54 is( $db1->{x}{xy}{foo}, 'y', "Before transaction, DB1's X is Y" );
55 is( $db2->{x}{xy}{foo}, 'y', "Before transaction, DB2's X is Y" );
2120a181 56
0e3e3555 57 $db1->begin_work;
45f047f8 58
0e3e3555 59 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
60 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
2120a181 61
0e3e3555 62 cmp_bag( [ keys %{$db1->{x}} ], [qw( xy )], "DB1->X keys correct" );
63 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
9c87a079 64
0e3e3555 65 cmp_bag( [ keys %{$db1->{x}{xy}} ], [qw( foo )], "DB1->X->XY keys correct" );
66 cmp_bag( [ keys %{$db2->{x}{xy}} ], [qw( foo )], "DB2->X->XY keys correct" );
2120a181 67
0e3e3555 68 is( $db1->{x}{xy}{foo}, 'y', "After transaction, DB1's X is Y" );
69 is( $db2->{x}{xy}{foo}, 'y', "After transaction, DB2's X is Y" );
45f047f8 70
0e3e3555 71 $db1->{x} = { yz => { bar => 30 } };
72 ok( !exists $db1->{x}{xy}, "DB1: After reassignment of X, X->XY is gone" );
73 is( $db2->{x}{xy}{foo}, 'y', "DB2: After reassignment of DB1 X, X->YZ is Y" );
2120a181 74
0e3e3555 75 cmp_bag( [ keys %{$db1->{x}} ], [qw( yz )], "DB1->X keys correct" );
76 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
2120a181 77
0e3e3555 78 $db1->commit;
2120a181 79
80 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
81 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
82
45f047f8 83 cmp_bag( [ keys %{$db1->{x}} ], [qw( yz )], "DB1->X keys correct" );
0e3e3555 84 cmp_bag( [ keys %{$db2->{x}} ], [qw( yz )], "DB2->X keys correct" );
45f047f8 85
0e3e3555 86 cmp_bag( [ keys %{$db1->{x}{yz}} ], [qw( bar )], "DB1->X->XY keys correct" );
87 cmp_bag( [ keys %{$db2->{x}{yz}} ], [qw( bar )], "DB2->X->XY keys correct" );
88}
45f047f8 89
0e3e3555 90done_testing;