Merge 'reorganize_tests' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class.git] / t / run / 21transactions.tl
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my $schema = DBICTest::init_schema();
9
10 plan tests => 39;
11
12 my $code = sub {
13   my ($artist, @cd_titles) = @_;
14   
15   $artist->create_related('cds', {
16     title => $_,
17     year => 2006,
18   }) foreach (@cd_titles);
19   
20   return $artist->cds->all;
21 };
22
23 # Test checking of parameters
24 {
25   eval {
26     (ref $schema)->txn_do(sub{});
27   };
28   like($@, qr/class method/, '$self parameter check ok');
29   eval {
30     $schema->txn_do('');
31   };
32   like($@, qr/must be a CODE reference/, '$coderef parameter check ok');
33 }
34
35 # Test successful txn_do() - scalar context
36 {
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');
42   is($artist->cds({
43     title => "txn_do test CD $_",
44   })->first->year, 2006, "new CD $_ year correct") for (1..5);
45 }
46
47 # Test successful txn_do() - list context
48 {
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');
54   is($artist->cds({
55     title => "txn_do test CD $_",
56   })->first->year, 2006, "new CD $_ year correct") for (6..10);
57 }
58
59 # Test nested successful txn_do()
60 {
61   my $nested_code = sub {
62     my ($schema, $artist, $code) = @_;
63
64     my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
65     my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
66
67     $schema->txn_do($code, $artist, @titles1);
68     $schema->txn_do($code, $artist, @titles2);
69   };
70
71   my $artist = $schema->resultset('Artist')->find(2);
72   my $count_before = $artist->cds->count;
73
74   eval {
75     $schema->txn_do($nested_code, $schema, $artist, $code);
76   };
77
78   my $error = $@;
79
80   ok(!$error, 'nested txn_do succeeded');
81   is($artist->cds({
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');
85 }
86
87 my $fail_code = sub {
88   my ($artist) = @_;
89   $artist->create_related('cds', {
90     title => 'this should not exist',
91     year => 2005,
92   });
93   die "the sky is falling";
94 };
95
96 # Test failed txn_do()
97 {
98   my $artist = $schema->resultset('Artist')->find(3);
99
100   eval {
101     $schema->txn_do($fail_code, $artist);
102   };
103
104   my $error = $@;
105
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',
109     year => 2005,
110   })->first;
111   ok(!defined($cd), q{failed txn_do didn't change the cds table});
112 }
113
114 # Test failed txn_do() with failed rollback
115 {
116   my $artist = $schema->resultset('Artist')->find(3);
117
118   # Force txn_rollback() to throw an exception
119   no warnings 'redefine';
120   local *{"DBIx::Class::Schema::txn_rollback"} = sub{die 'FAILED'};
121
122   eval {
123     $schema->txn_do($fail_code, $artist);
124   };
125
126   my $error = $@;
127
128   like($error, qr/Rollback failed/, 'failed txn_do with a failed '.
129        'txn_rollback threw a rollback exception');
130   like($error, qr/the sky is falling/, 'failed txn_do with a failed '.
131        'txn_rollback included the original exception');
132
133   my $cd = $artist->cds({
134     title => 'this should not exist',
135     year => 2005,
136   })->first;
137   isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
138          q{changed the cds table});
139   $cd->delete; # Rollback failed
140   $cd = $artist->cds({
141     title => 'this should not exist',
142     year => 2005,
143   })->first;
144   ok(!defined($cd), q{deleted the failed txn's cd});
145   $schema->storage->{transaction_depth} = 0; # Must reset this or further tests
146                                              # will fail
147 }
148
149 # Test nested failed txn_do()
150 {
151   my $nested_fail_code = sub {
152     my ($schema, $artist, $code1, $code2) = @_;
153
154     my @titles = map {'nested txn_do test CD ' . $_} (1..5);
155
156     $schema->txn_do($code1, $artist, @titles); # successful txn
157     $schema->txn_do($code2, $artist);          # failed txn
158   };
159
160   my $artist = $schema->resultset('Artist')->find(3);
161
162   eval {
163     $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
164   };
165
166   my $error = $@;
167
168   like($error, qr/the sky is falling/, 'nested failed txn_do threw exception');
169   ok(!defined($artist->cds({
170     title => 'nested txn_do test CD '.$_,
171     year => 2006,
172   })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
173   my $cd = $artist->cds({
174     title => 'this should not exist',
175     year => 2005,
176   })->first;
177   ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
178 }
179