r11683@rob-kinyons-powerbook58: rob | 2006-04-28 20:54:09 -0400
[dbsrgits/DBM-Deep.git] / t / 34_transaction_arrays.t
CommitLineData
504185fb 1use strict;
f9a320bb 2use Test::More tests => 47;
504185fb 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,
11 locking => 1,
12 autoflush => 1,
13 type => DBM::Deep->TYPE_ARRAY,
14);
15
16my $db2 = DBM::Deep->new(
17 file => $filename,
18 locking => 1,
19 autoflush => 1,
20 type => DBM::Deep->TYPE_ARRAY,
21);
22
23$db1->[0] = 'y';
24is( $db1->[0], 'y', "Before transaction, DB1's 0 is Y" );
25is( $db2->[0], 'y', "Before transaction, DB2's 0 is Y" );
26
27$db1->begin_work;
28
29 is( $db1->[0], 'y', "DB1 transaction started, no actions - DB1's 0 is Y" );
30 is( $db2->[0], 'y', "DB1 transaction started, no actions - DB2's 0 is Y" );
31
32 $db1->[0] = 'z';
33 is( $db1->[0], 'z', "Within DB1 transaction, DB1's 0 is Z" );
34 is( $db2->[0], 'y', "Within DB1 transaction, DB2's 0 is still Y" );
35
36 $db2->[1] = 'foo';
37 is( $db2->[1], 'foo', "DB2 set 1 within DB1's transaction, so DB2 can see it" );
38 ok( !exists $db1->[1], "Since 1 was added after the transaction began, DB1 doesn't see it." );
39
40 cmp_ok( scalar(@$db1), '==', 1, "DB1 has 1 element" );
41 cmp_ok( scalar(@$db2), '==', 2, "DB2 has 2 elements" );
42
43$db1->rollback;
44
45is( $db1->[0], 'y', "After rollback, DB1's 0 is Y" );
46is( $db2->[0], 'y', "After rollback, DB2's 0 is Y" );
47
48is( $db1->[1], 'foo', "After DB1 transaction is over, DB1 can see 1" );
49is( $db2->[1], 'foo', "After DB1 transaction is over, DB2 can still see 1" );
50
51cmp_ok( scalar(@$db1), '==', 2, "DB1 now has 2 elements" );
52cmp_ok( scalar(@$db2), '==', 2, "DB2 still has 2 elements" );
53
c0780c5e 54$db1->begin_work;
55
56 is( $db1->[0], 'y', "DB1 transaction started, no actions - DB1's 0 is Y" );
57 is( $db2->[0], 'y', "DB1 transaction started, no actions - DB2's 0 is Y" );
58
59 $db1->[2] = 'z';
60 is( $db1->[2], 'z', "Within DB1 transaction, DB1's 2 is Z" );
61 ok( !exists $db2->[2], "Within DB1 transaction, DB2 cannot see 2" );
62
63 cmp_ok( scalar(@$db1), '==', 3, "DB1 has 3 elements" );
64 cmp_ok( scalar(@$db2), '==', 2, "DB2 has 2 elements" );
65
66$db1->commit;
67
68is( $db1->[0], 'y', "After rollback, DB1's 0 is Y" );
69is( $db2->[0], 'y', "After rollback, DB2's 0 is Y" );
70
71is( $db1->[2], 'z', "After DB1 transaction is over, DB1 can still see 2" );
72is( $db2->[2], 'z', "After DB1 transaction is over, DB2 can now see 2" );
73
74cmp_ok( scalar(@$db1), '==', 3, "DB1 now has 2 elements" );
75cmp_ok( scalar(@$db2), '==', 3, "DB2 still has 2 elements" );
76
51458ec9 77$db1->begin_work;
78
79 push @$db1, 'foo';
80 unshift @$db1, 'bar';
81
82 cmp_ok( scalar(@$db1), '==', 5, "DB1 now has 5 elements" );
83 cmp_ok( scalar(@$db2), '==', 3, "DB2 still has 3 elements" );
84
85 is( $db1->[0], 'bar' );
86 is( $db1->[-1], 'foo' );
87
88$db1->rollback;
89
90cmp_ok( scalar(@$db1), '==', 3, "DB1 is back to 3 elements" );
91cmp_ok( scalar(@$db2), '==', 3, "DB2 still has 3 elements" );
92
93$db1->begin_work;
94
95 push @$db1, 'foo';
96 unshift @$db1, 'bar';
97
98 cmp_ok( scalar(@$db1), '==', 5, "DB1 now has 5 elements" );
99 cmp_ok( scalar(@$db2), '==', 3, "DB2 still has 3 elements" );
100
101$db1->commit;
102
103cmp_ok( scalar(@$db1), '==', 5, "DB1 is still at 5 elements" );
104cmp_ok( scalar(@$db2), '==', 5, "DB2 now has 5 elements" );
105
106is( $db1->[0], 'bar' );
107is( $db1->[-1], 'foo' );
108
109is( $db2->[0], 'bar' );
110is( $db2->[-1], 'foo' );
f9a320bb 111
112$db1->begin_work;
113
114 @$db1 = (); # clear()
115
116 cmp_ok( scalar(@$db1), '==', 0, "DB1 now has 0 elements" );
117 cmp_ok( scalar(@$db2), '==', 5, "DB2 still has 5 elements" );
118
119$db1->rollback;
120
121cmp_ok( scalar(@$db1), '==', 5, "DB1 now has 5 elements" );
122cmp_ok( scalar(@$db2), '==', 5, "DB2 still has 5 elements" );
123