6 my ($artist, @cd_titles) = @_;
8 $artist->create_related('cds', {
11 }) foreach (@cd_titles);
13 return $artist->cds->all;
16 # Test checking of parameters
19 (ref $schema)->txn_do(sub{});
21 like($@, qr/class method/, '$self parameter check ok');
25 like($@, qr/must be a CODE reference/, '$coderef parameter check ok');
28 # Test successful txn_do() - scalar context
30 my @titles = map {'txn_do test CD ' . $_} (1..5);
31 my $artist = $schema->resultset('Artist')->find(1);
32 my $count_before = $artist->cds->count;
33 my $count_after = $schema->txn_do($code, $artist, @titles);
34 is($count_after, $count_before+5, 'successful txn added 5 cds');
36 title => "txn_do test CD $_",
37 })->first->year, 2006, "new CD $_ year correct") for (1..5);
40 # Test successful txn_do() - list context
42 my @titles = map {'txn_do test CD ' . $_} (6..10);
43 my $artist = $schema->resultset('Artist')->find(1);
44 my $count_before = $artist->cds->count;
45 my @cds = $schema->txn_do($code, $artist, @titles);
46 is(scalar @cds, $count_before+5, 'added 5 CDs and returned in list context');
48 title => "txn_do test CD $_",
49 })->first->year, 2006, "new CD $_ year correct") for (6..10);
52 # Test nested successful txn_do()
54 my $nested_code = sub {
55 my ($schema, $artist, $code) = @_;
57 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
58 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
60 $schema->txn_do($code, $artist, @titles1);
61 $schema->txn_do($code, $artist, @titles2);
64 my $artist = $schema->resultset('Artist')->find(2);
65 my $count_before = $artist->cds->count;
68 $schema->txn_do($nested_code, $schema, $artist, $code);
73 ok(!$error, 'nested txn_do succeeded');
75 title => 'nested txn_do test CD '.$_,
76 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
77 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
82 $artist->create_related('cds', {
83 title => 'this should not exist',
86 die "the sky is falling";
89 # Test failed txn_do()
91 my $artist = $schema->resultset('Artist')->find(3);
94 $schema->txn_do($fail_code, $artist);
99 like($error, qr/the sky is falling/, 'failed txn_do threw an exception');
100 my $cd = $artist->cds({
101 title => 'this should not exist',
104 ok(!defined($cd), q{failed txn_do didn't change the cds table});
107 # Test failed txn_do() with failed rollback
109 my $artist = $schema->resultset('Artist')->find(3);
111 # Force txn_rollback() to throw an exception
112 no warnings 'redefine';
113 local *{"DBIx::Class::Schema::txn_rollback"} = sub{die 'FAILED'};
116 $schema->txn_do($fail_code, $artist);
121 like($error, qr/Rollback failed/, 'failed txn_do with a failed '.
122 'txn_rollback threw a rollback exception');
123 like($error, qr/the sky is falling/, 'failed txn_do with a failed '.
124 'txn_rollback included the original exception');
126 my $cd = $artist->cds({
127 title => 'this should not exist',
130 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
131 q{changed the cds table});
132 $cd->delete; # Rollback failed
134 title => 'this should not exist',
137 ok(!defined($cd), q{deleted the failed txn's cd});
138 $schema->storage->{transaction_depth} = 0; # Must reset this or further tests
142 # Test nested failed txn_do()
144 my $nested_fail_code = sub {
145 my ($schema, $artist, $code1, $code2) = @_;
147 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
149 $schema->txn_do($code1, $artist, @titles); # successful txn
150 $schema->txn_do($code2, $artist); # failed txn
153 my $artist = $schema->resultset('Artist')->find(3);
156 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
161 like($error, qr/the sky is falling/, 'nested failed txn_do threw exception');
162 ok(!defined($artist->cds({
163 title => 'nested txn_do test CD '.$_,
165 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
166 my $cd = $artist->cds({
167 title => 'this should not exist',
170 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});