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