6 my ($artist, @cd_titles) = @_;
8 $artist->create_related('cds', {
11 }) foreach (@cd_titles);
13 return $artist->cds->all;
16 # Test successful txn_do() - scalar context
18 my @titles = map {'txn_do test CD ' . $_} (1..5);
19 my $artist = $schema->resultset('Artist')->find(1);
20 my $count_before = $artist->cds->count;
21 my $count_after = $schema->txn_do($code, $artist, @titles);
22 is($count_after, $count_before+5, 'successful txn added 5 cds');
24 title => "txn_do test CD $_",
25 })->first->year, 2006, "new CD $_ year correct") for (1..5);
28 # Test successful txn_do() - list context
30 my @titles = map {'txn_do test CD ' . $_} (6..10);
31 my $artist = $schema->resultset('Artist')->find(1);
32 my $count_before = $artist->cds->count;
33 my @cds = $schema->txn_do($code, $artist, @titles);
34 is(scalar @cds, $count_before+5, 'added 5 CDs and returned in list context');
36 title => "txn_do test CD $_",
37 })->first->year, 2006, "new CD $_ year correct") for (6..10);
40 # Test nested successful txn_do()
42 my $nested_code = sub {
43 my ($schema, $artist, $code) = @_;
45 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
46 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
48 $schema->txn_do($code, $artist, @titles1);
49 $schema->txn_do($code, $artist, @titles2);
52 my $artist = $schema->resultset('Artist')->find(2);
53 my $count_before = $artist->cds->count;
56 $schema->txn_do($nested_code, $schema, $artist, $code);
61 ok(!$error, 'nested txn_do succeeded');
63 title => 'nested txn_do test CD '.$_,
64 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
65 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
70 $artist->create_related('cds', {
71 title => 'this should not exist',
74 die "the sky is falling";
77 # Test failed txn_do()
79 my $artist = $schema->resultset('Artist')->find(3);
82 $schema->txn_do($fail_code, $artist);
87 like($error, qr/the sky is falling/, 'failed txn_do threw an exception');
88 my $cd = $artist->cds({
89 title => 'this should not exist',
92 ok(!defined($cd), q{failed txn_do didn't change the cds table});
95 # Test failed txn_do() with failed rollback
97 my $artist = $schema->resultset('Artist')->find(3);
99 # Force txn_rollback() to throw an exception
100 no warnings 'redefine';
101 local *{"DBIx::Class::Schema::txn_rollback"} = sub{die 'FAILED'};
104 $schema->txn_do($fail_code, $artist);
109 like($error, qr/Rollback failed/, 'failed txn_do with a failed '.
110 'txn_rollback threw a rollback exception');
111 like($error, qr/the sky is falling/, 'failed txn_do with a failed '.
112 'txn_rollback included the original exception');
114 my $cd = $artist->cds({
115 title => 'this should not exist',
118 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
119 q{changed the cds table});
120 $cd->delete; # Rollback failed
122 title => 'this should not exist',
125 ok(!defined($cd), q{deleted the failed txn's cd});
126 $schema->storage->{transaction_depth} = 0; # Must reset this or further tests
130 # Test nested failed txn_do()
132 my $nested_fail_code = sub {
133 my ($schema, $artist, $code1, $code2) = @_;
135 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
137 $schema->txn_do($code1, $artist, @titles); # successful txn
138 $schema->txn_do($code2, $artist); # failed txn
141 my $artist = $schema->resultset('Artist')->find(3);
144 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
149 like($error, qr/the sky is falling/, 'nested failed txn_do threw exception');
150 ok(!defined($artist->cds({
151 title => 'nested txn_do test CD '.$_,
153 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
154 my $cd = $artist->cds({
155 title => 'this should not exist',
158 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});