Added description to a test
[dbsrgits/DBM-Deep.git] / t / 41_transaction_multilevel.t
CommitLineData
2120a181 1use strict;
45f047f8 2use Test::More tests => 41;
2120a181 3use Test::Deep;
4use t::common qw( new_fh );
5
6use_ok( 'DBM::Deep' );
7
8my ($fh, $filename) = new_fh();
9my $db1 = DBM::Deep->new(
10 file => $filename,
45f047f8 11 fh => $fh,
2120a181 12 locking => 1,
13 autoflush => 1,
c57b19c6 14 num_txns => 2,
2120a181 15);
f1879fdc 16seek $db1->_get_self->_engine->storage->{fh}, 0, 0;
2120a181 17
18my $db2 = DBM::Deep->new(
19 file => $filename,
45f047f8 20 fh => $fh,
2120a181 21 locking => 1,
22 autoflush => 1,
c57b19c6 23 num_txns => 2,
2120a181 24);
25
45f047f8 26$db1->{x} = { xy => { foo => 'y' } };
27is( $db1->{x}{xy}{foo}, 'y', "Before transaction, DB1's X is Y" );
28is( $db2->{x}{xy}{foo}, 'y', "Before transaction, DB2's X is Y" );
2120a181 29
30$db1->begin_work;
31
32 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
33 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
34
45f047f8 35 cmp_bag( [ keys %{$db1->{x}} ], [qw( xy )], "DB1->X keys correct" );
36 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
2120a181 37
45f047f8 38 cmp_bag( [ keys %{$db1->{x}{xy}} ], [qw( foo )], "DB1->X->XY keys correct" );
39 cmp_bag( [ keys %{$db2->{x}{xy}} ], [qw( foo )], "DB2->X->XY keys correct" );
2120a181 40
45f047f8 41 is( $db1->{x}{xy}{foo}, 'y', "After transaction, DB1's X is Y" );
42 is( $db2->{x}{xy}{foo}, 'y', "After transaction, DB2's X is Y" );
2120a181 43
45f047f8 44 $db1->{x} = { yz => { bar => 30 } };
45 ok( !exists $db1->{x}{xy}, "DB1: After reassignment of X, X->XY is gone" );
46 is( $db2->{x}{xy}{foo}, 'y', "DB2: After reassignment of DB1 X, X is Y" );
47
48 cmp_bag( [ keys %{$db1->{x}} ], [qw( yz )], "DB1->X keys correct" );
49 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
2120a181 50
51$db1->rollback;
9c87a079 52
2120a181 53cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
54cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
55
45f047f8 56cmp_bag( [ keys %{$db1->{x}} ], [qw( xy )], "DB1->X keys correct" );
57cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
58
59cmp_bag( [ keys %{$db1->{x}{xy}} ], [qw( foo )], "DB1->X->XY keys correct" );
60cmp_bag( [ keys %{$db2->{x}{xy}} ], [qw( foo )], "DB2->X->XY keys correct" );
2120a181 61
45f047f8 62is( $db1->{x}{xy}{foo}, 'y', "Before transaction, DB1's X is Y" );
63is( $db2->{x}{xy}{foo}, 'y', "Before transaction, DB2's X is Y" );
2120a181 64
65$db1->begin_work;
66
67 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
68 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
69
45f047f8 70 cmp_bag( [ keys %{$db1->{x}} ], [qw( xy )], "DB1->X keys correct" );
71 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
72
73 cmp_bag( [ keys %{$db1->{x}{xy}} ], [qw( foo )], "DB1->X->XY keys correct" );
74 cmp_bag( [ keys %{$db2->{x}{xy}} ], [qw( foo )], "DB2->X->XY keys correct" );
2120a181 75
45f047f8 76 is( $db1->{x}{xy}{foo}, 'y', "After transaction, DB1's X is Y" );
77 is( $db2->{x}{xy}{foo}, 'y', "After transaction, DB2's X is Y" );
2120a181 78
45f047f8 79 $db1->{x} = { yz => { bar => 30 } };
80 ok( !exists $db1->{x}{xy}, "DB1: After reassignment of X, X->XY is gone" );
81 is( $db2->{x}{xy}{foo}, 'y', "DB2: After reassignment of DB1 X, X->YZ is Y" );
2120a181 82
45f047f8 83 cmp_bag( [ keys %{$db1->{x}} ], [qw( yz )], "DB1->X keys correct" );
84 cmp_bag( [ keys %{$db2->{x}} ], [qw( xy )], "DB2->X keys correct" );
2120a181 85
86$db1->commit;
87
88cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
89cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
90
45f047f8 91cmp_bag( [ keys %{$db1->{x}} ], [qw( yz )], "DB1->X keys correct" );
92cmp_bag( [ keys %{$db2->{x}} ], [qw( yz )], "DB2->X keys correct" );
93
94cmp_bag( [ keys %{$db1->{x}{yz}} ], [qw( bar )], "DB1->X->XY keys correct" );
95cmp_bag( [ keys %{$db2->{x}{yz}} ], [qw( bar )], "DB2->X->XY keys correct" );
96
f1879fdc 97$db1->_get_self->_engine->storage->close( $db1->_get_self );
98$db2->_get_self->_engine->storage->close( $db2->_get_self );