6 my ($artist, @cd_titles) = @_;
8 $artist->create_related('cds', {
11 }) foreach (@cd_titles);
13 return $artist->cds->all;
16 # Test checking of parameters
20 (ref $schema)->txn_do(sub{});
22 like($@, qr/class method/, '$coderef parameter check ok');
26 like($@, qr/must be a CODE reference/, '$coderef parameter check ok');
29 # Test successful txn_do() - scalar context
31 my @titles = map {'txn_do test CD ' . $_} (1..5);
32 my $artist = $schema->resultset('Artist')->find(1);
33 my $count_before = $artist->cds->count;
34 my $count_after = $schema->txn_do($code, $artist, @titles);
35 is($count_after, $count_before+5, 'successful txn added 5 cds');
37 title => "txn_do test CD $_",
38 })->first->year, 2006, "new CD $_ year correct") for (1..5);
41 # Test successful txn_do() - list context
43 my @titles = map {'txn_do test CD ' . $_} (6..10);
44 my $artist = $schema->resultset('Artist')->find(1);
45 my $count_before = $artist->cds->count;
46 my @cds = $schema->txn_do($code, $artist, @titles);
47 is(scalar @cds, $count_before+5, 'added 5 CDs and returned in list context');
49 title => "txn_do test CD $_",
50 })->first->year, 2006, "new CD $_ year correct") for (6..10);
53 # Test nested successful txn_do()
55 my $nested_code = sub {
56 my ($schema, $artist, $code) = @_;
58 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
59 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
61 $schema->txn_do($code, $artist, @titles1);
62 $schema->txn_do($code, $artist, @titles2);
65 my $artist = $schema->resultset('Artist')->find(2);
66 my $count_before = $artist->cds->count;
69 $schema->txn_do($nested_code, $schema, $artist, $code);
74 ok(!$error, 'nested txn_do succeeded');
76 title => 'nested txn_do test CD '.$_,
77 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
78 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
83 $artist->create_related('cds', {
84 title => 'this should not exist',
87 die "the sky is falling";
90 # Test failed txn_do()
92 my $artist = $schema->resultset('Artist')->find(3);
95 $schema->txn_do($fail_code, $artist);
100 like($error, qr/the sky is falling/, 'failed txn_do threw an exception');
101 my $cd = $artist->cds({
102 title => 'this should not exist',
105 ok(!defined($cd), q{failed txn_do didn't change the cds table});
108 # Test failed txn_do() with failed rollback
110 my $artist = $schema->resultset('Artist')->find(3);
112 # Force txn_rollback() to throw an exception
113 no warnings 'redefine';
114 local *{"DBIx::Class::Schema::txn_rollback"} = sub{die 'FAILED'};
117 $schema->txn_do($fail_code, $artist);
122 like($error, qr/Rollback failed/, 'failed txn_do with a failed '.
123 'txn_rollback threw a rollback exception');
124 like($error, qr/the sky is falling/, 'failed txn_do with a failed '.
125 'txn_rollback included the original exception');
127 my $cd = $artist->cds({
128 title => 'this should not exist',
131 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
132 q{changed the cds table});
133 $cd->delete; # Rollback failed
135 title => 'this should not exist',
138 ok(!defined($cd), q{deleted the failed txn's cd});
139 $schema->storage->{transaction_depth} = 0; # Must reset this or further tests
143 # Test nested failed txn_do()
145 my $nested_fail_code = sub {
146 my ($schema, $artist, $code1, $code2) = @_;
148 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
150 $schema->txn_do($code1, $artist, @titles); # successful txn
151 $schema->txn_do($code2, $artist); # failed txn
154 my $artist = $schema->resultset('Artist')->find(3);
157 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
162 like($error, qr/the sky is falling/, 'nested failed txn_do threw exception');
163 ok(!defined($artist->cds({
164 title => 'nested txn_do test CD '.$_,
166 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
167 my $cd = $artist->cds({
168 title => 'this should not exist',
171 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});