Fixed txn_do to check that $coderef is actually a code ref, and wrote parameter check...
[dbsrgits/DBIx-Class.git] / t / run / 21transactions.tl
CommitLineData
a62cf8d4 1sub run_tests {
2my $schema = shift;
171dadd7 3plan tests => 39;
a62cf8d4 4
5my $code = sub {
6 my ($artist, @cd_titles) = @_;
7
8 $artist->create_related('cds', {
9 title => $_,
10 year => 2006,
11 }) foreach (@cd_titles);
12
13 return $artist->cds->all;
14};
15
171dadd7 16# Test checking of parameters
17{
18
19 eval {
20 (ref $schema)->txn_do(sub{});
21 };
22 like($@, qr/class method/, '$coderef parameter check ok');
23 eval {
24 $schema->txn_do('');
25 };
26 like($@, qr/must be a CODE reference/, '$coderef parameter check ok');
27}
28
a62cf8d4 29# Test successful txn_do() - scalar context
30{
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');
36 is($artist->cds({
37 title => "txn_do test CD $_",
38 })->first->year, 2006, "new CD $_ year correct") for (1..5);
39}
40
41# Test successful txn_do() - list context
42{
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');
48 is($artist->cds({
49 title => "txn_do test CD $_",
50 })->first->year, 2006, "new CD $_ year correct") for (6..10);
51}
52
53# Test nested successful txn_do()
54{
55 my $nested_code = sub {
56 my ($schema, $artist, $code) = @_;
57
58 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
59 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
60
61 $schema->txn_do($code, $artist, @titles1);
62 $schema->txn_do($code, $artist, @titles2);
63 };
64
65 my $artist = $schema->resultset('Artist')->find(2);
66 my $count_before = $artist->cds->count;
67
68 eval {
69 $schema->txn_do($nested_code, $schema, $artist, $code);
70 };
71
72 my $error = $@;
73
74 ok(!$error, 'nested txn_do succeeded');
75 is($artist->cds({
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');
79}
80
81my $fail_code = sub {
82 my ($artist) = @_;
83 $artist->create_related('cds', {
84 title => 'this should not exist',
85 year => 2005,
86 });
87 die "the sky is falling";
88};
89
90# Test failed txn_do()
91{
92 my $artist = $schema->resultset('Artist')->find(3);
93
94 eval {
95 $schema->txn_do($fail_code, $artist);
96 };
97
98 my $error = $@;
99
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',
103 year => 2005,
104 })->first;
105 ok(!defined($cd), q{failed txn_do didn't change the cds table});
106}
107
108# Test failed txn_do() with failed rollback
109{
110 my $artist = $schema->resultset('Artist')->find(3);
111
112 # Force txn_rollback() to throw an exception
113 no warnings 'redefine';
114 local *{"DBIx::Class::Schema::txn_rollback"} = sub{die 'FAILED'};
115
116 eval {
117 $schema->txn_do($fail_code, $artist);
118 };
119
120 my $error = $@;
121
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');
126
127 my $cd = $artist->cds({
128 title => 'this should not exist',
129 year => 2005,
130 })->first;
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
134 $cd = $artist->cds({
135 title => 'this should not exist',
136 year => 2005,
137 })->first;
138 ok(!defined($cd), q{deleted the failed txn's cd});
139 $schema->storage->{transaction_depth} = 0; # Must reset this or further tests
140 # will fail
141}
142
143# Test nested failed txn_do()
144{
145 my $nested_fail_code = sub {
146 my ($schema, $artist, $code1, $code2) = @_;
147
148 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
149
150 $schema->txn_do($code1, $artist, @titles); # successful txn
151 $schema->txn_do($code2, $artist); # failed txn
152 };
153
154 my $artist = $schema->resultset('Artist')->find(3);
155
156 eval {
157 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
158 };
159
160 my $error = $@;
161
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 '.$_,
165 year => 2006,
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',
169 year => 2005,
170 })->first;
171 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
172}
173}
174
1751;