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/class method/, '$self parameter check ok');
32 like($@, qr/must be a CODE reference/, '$coderef parameter check ok');
35 # Test successful txn_do() - scalar context
37 my @titles = map {'txn_do test CD ' . $_} (1..5);
38 my $artist = $schema->resultset('Artist')->find(1);
39 my $count_before = $artist->cds->count;
40 my $count_after = $schema->txn_do($code, $artist, @titles);
41 is($count_after, $count_before+5, 'successful txn added 5 cds');
43 title => "txn_do test CD $_",
44 })->first->year, 2006, "new CD $_ year correct") for (1..5);
47 # Test successful txn_do() - list context
49 my @titles = map {'txn_do test CD ' . $_} (6..10);
50 my $artist = $schema->resultset('Artist')->find(1);
51 my $count_before = $artist->cds->count;
52 my @cds = $schema->txn_do($code, $artist, @titles);
53 is(scalar @cds, $count_before+5, 'added 5 CDs and returned in list context');
55 title => "txn_do test CD $_",
56 })->first->year, 2006, "new CD $_ year correct") for (6..10);
59 # Test nested successful txn_do()
61 my $nested_code = sub {
62 my ($schema, $artist, $code) = @_;
64 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
65 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
67 $schema->txn_do($code, $artist, @titles1);
68 $schema->txn_do($code, $artist, @titles2);
71 my $artist = $schema->resultset('Artist')->find(2);
72 my $count_before = $artist->cds->count;
75 $schema->txn_do($nested_code, $schema, $artist, $code);
80 ok(!$error, 'nested txn_do succeeded');
82 title => 'nested txn_do test CD '.$_,
83 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
84 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
89 $artist->create_related('cds', {
90 title => 'this should not exist',
93 die "the sky is falling";
96 # Test failed txn_do()
98 my $artist = $schema->resultset('Artist')->find(3);
101 $schema->txn_do($fail_code, $artist);
106 like($error, qr/the sky is falling/, 'failed txn_do threw an exception');
107 my $cd = $artist->cds({
108 title => 'this should not exist',
111 ok(!defined($cd), q{failed txn_do didn't change the cds table});
114 # Test failed txn_do() with failed rollback
116 my $artist = $schema->resultset('Artist')->find(3);
118 # Force txn_rollback() to throw an exception
119 no warnings 'redefine';
121 local *{"DBIx::Class::Schema::txn_rollback"} = sub{die 'FAILED'};
124 $schema->txn_do($fail_code, $artist);
129 like($error, qr/Rollback failed/, 'failed txn_do with a failed '.
130 'txn_rollback threw a rollback exception');
131 like($error, qr/the sky is falling/, 'failed txn_do with a failed '.
132 'txn_rollback included the original exception');
134 my $cd = $artist->cds({
135 title => 'this should not exist',
138 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
139 q{changed the cds table});
140 $cd->delete; # Rollback failed
142 title => 'this should not exist',
145 ok(!defined($cd), q{deleted the failed txn's cd});
146 $schema->storage->{transaction_depth} = 0; # Must reset this or further tests
150 # Test nested failed txn_do()
152 my $nested_fail_code = sub {
153 my ($schema, $artist, $code1, $code2) = @_;
155 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
157 $schema->txn_do($code1, $artist, @titles); # successful txn
158 $schema->txn_do($code2, $artist); # failed txn
161 my $artist = $schema->resultset('Artist')->find(3);
164 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
169 like($error, qr/the sky is falling/, 'nested failed txn_do threw exception');
170 ok(!defined($artist->cds({
171 title => 'nested txn_do test CD '.$_,
173 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
174 my $cd = $artist->cds({
175 title => 'this should not exist',
178 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});