10 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, 'Pre-connection nested transactions.');
239 # although not connected DBI would still warn about rolling back at disconnect
240 $schema2->txn_rollback;
241 $schema2->txn_rollback;
242 $schema2->storage->disconnect;
244 $schema->storage->disconnect;
246 # Test txn_scope_guard
248 my $schema = DBICTest->init_schema();
250 is($schema->storage->transaction_depth, 0, "Correct transaction depth");
251 my $artist_rs = $schema->resultset('Artist');
253 my $guard = $schema->txn_scope_guard;
257 name => 'Death Cab for Cutie',
262 } qr/No such column made_up_column .*? at .*?81transactions.t line \d+/s, "Error propogated okay";
264 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
266 my $inner_exception; # set in inner() below
270 is($@, $inner_exception, "Nested exceptions propogated");
272 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
275 warnings_exist ( sub {
276 # The 0 arg says don't die, just let the scope guard go out of scope
277 # forcing a txn_rollback to happen
279 }, qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./, 'Out of scope warning detected');
280 ok(!$artist_rs->find({name => 'Death Cab for Cutie'}), "Artist not created");
281 }, 'rollback successful withot exception');
286 my $guard = $schema->txn_scope_guard;
287 $schema->resultset('Artist')->create({
288 name => 'Death Cab for Cutie',
294 my ($schema, $fatal) = @_;
296 my $inner_guard = $schema->txn_scope_guard;
297 is($schema->storage->transaction_depth, 2, "Correct transaction depth");
299 my $artist = $artist_rs->find({ name => 'Death Cab for Cutie' });
302 $artist->cds->create({
305 $fatal ? ( foo => 'bar' ) : ()
309 # Record what got thrown so we can test it propgates out properly.
310 $inner_exception = $@;
314 # inner guard should commit without consequences
315 $inner_guard->commit;
319 # make sure the guard does not eat exceptions
321 my $schema = DBICTest->init_schema();
323 my $guard = $schema->txn_scope_guard;
324 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
326 $schema->storage->disconnect; # this should freak out the guard rollback
328 die 'Deliberate exception';
329 }, qr/Deliberate exception.+Rollback failed/s);
332 # make sure it warns *big* on failed rollbacks
334 my $schema = DBICTest->init_schema();
336 # something is really confusing Test::Warn here, no time to debug
340 my $guard = $schema->txn_scope_guard;
341 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
343 $schema->storage->disconnect; # this should freak out the guard rollback
346 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
347 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
349 'proper warnings generated on out-of-scope+rollback failure'
354 qr/A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back./,
355 qr/\*+ ROLLBACK FAILED\!\!\! \*+/,
359 local $SIG{__WARN__} = sub {
360 if (grep {$_[0] =~ $_} (@want)) {
368 my $guard = $schema->txn_scope_guard;
369 $schema->resultset ('Artist')->create ({ name => 'bohhoo'});
371 $schema->storage->disconnect; # this should freak out the guard rollback
374 is (@w, 2, 'Both expected warnings found');