8 my $schema = DBICTest->init_schema();
13 my ($artist, @cd_titles) = @_;
15 $artist->create_related('cds', {
18 }) foreach (@cd_titles);
20 return $artist->cds->all;
23 # Test checking of parameters
26 (ref $schema)->txn_do(sub{});
28 like($@, qr/storage/, "can't call txn_do without storage");
32 like($@, qr/must be a CODE reference/, '$coderef parameter check ok');
35 # Test successful txn_do() - scalar context
37 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
39 my @titles = map {'txn_do test CD ' . $_} (1..5);
40 my $artist = $schema->resultset('Artist')->find(1);
41 my $count_before = $artist->cds->count;
42 my $count_after = $schema->txn_do($code, $artist, @titles);
43 is($count_after, $count_before+5, 'successful txn added 5 cds');
45 title => "txn_do test CD $_",
46 })->first->year, 2006, "new CD $_ year correct") for (1..5);
48 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
51 # Test successful txn_do() - list context
53 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
55 my @titles = map {'txn_do test CD ' . $_} (6..10);
56 my $artist = $schema->resultset('Artist')->find(1);
57 my $count_before = $artist->cds->count;
58 my @cds = $schema->txn_do($code, $artist, @titles);
59 is(scalar @cds, $count_before+5, 'added 5 CDs and returned in list context');
61 title => "txn_do test CD $_",
62 })->first->year, 2006, "new CD $_ year correct") for (6..10);
64 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
67 # Test nested successful txn_do()
69 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
71 my $nested_code = sub {
72 my ($schema, $artist, $code) = @_;
74 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
75 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
77 $schema->txn_do($code, $artist, @titles1);
78 $schema->txn_do($code, $artist, @titles2);
81 my $artist = $schema->resultset('Artist')->find(2);
82 my $count_before = $artist->cds->count;
85 $schema->txn_do($nested_code, $schema, $artist, $code);
90 ok(!$error, 'nested txn_do succeeded');
92 title => 'nested txn_do test CD '.$_,
93 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
94 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
96 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
101 $artist->create_related('cds', {
102 title => 'this should not exist',
105 die "the sky is falling";
108 # Test failed txn_do()
111 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
113 my $artist = $schema->resultset('Artist')->find(3);
116 $schema->txn_do($fail_code, $artist);
121 like($error, qr/the sky is falling/, 'failed txn_do threw an exception');
122 my $cd = $artist->cds({
123 title => 'this should not exist',
126 ok(!defined($cd), q{failed txn_do didn't change the cds table});
128 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
131 # do the same transaction again
133 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
135 my $artist = $schema->resultset('Artist')->find(3);
138 $schema->txn_do($fail_code, $artist);
143 like($error, qr/the sky is falling/, 'failed txn_do threw an exception');
144 my $cd = $artist->cds({
145 title => 'this should not exist',
148 ok(!defined($cd), q{failed txn_do didn't change the cds table});
150 is( $schema->storage->{transaction_depth}, 0, 'txn depth has been reset');
153 # Test failed txn_do() with failed rollback
155 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
157 my $artist = $schema->resultset('Artist')->find(3);
159 # Force txn_rollback() to throw an exception
160 no warnings 'redefine';
163 # die in rollback, but maintain sanity for further tests ...
164 local *{"DBIx::Class::Storage::DBI::SQLite::txn_rollback"} = sub{
166 $storage->{transaction_depth}--;
171 $schema->txn_do($fail_code, $artist);
176 like($error, qr/Rollback failed/, 'failed txn_do with a failed '.
177 'txn_rollback threw a rollback exception');
178 like($error, qr/the sky is falling/, 'failed txn_do with a failed '.
179 'txn_rollback included the original exception');
181 my $cd = $artist->cds({
182 title => 'this should not exist',
185 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
186 q{changed the cds table});
187 $cd->delete; # Rollback failed
189 title => 'this should not exist',
192 ok(!defined($cd), q{deleted the failed txn's cd});
193 $schema->storage->_dbh->rollback;
196 # Test nested failed txn_do()
198 is( $schema->storage->{transaction_depth}, 0, 'txn depth starts at 0');
200 my $nested_fail_code = sub {
201 my ($schema, $artist, $code1, $code2) = @_;
203 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
205 $schema->txn_do($code1, $artist, @titles); # successful txn
206 $schema->txn_do($code2, $artist); # failed txn
209 my $artist = $schema->resultset('Artist')->find(3);
212 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
217 like($error, qr/the sky is falling/, 'nested failed txn_do threw exception');
218 ok(!defined($artist->cds({
219 title => 'nested txn_do test CD '.$_,
221 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
222 my $cd = $artist->cds({
223 title => 'this should not exist',
226 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
229 # Grab a new schema to test txn before connect
231 my $schema2 = DBICTest->init_schema(no_deploy => 1);
233 $schema2->txn_begin();
234 $schema2->txn_begin();
237 ok(($err eq ''), 'Pre-connection nested transactions.');