Added DBIx::Class::Schema::txn_do()
[dbsrgits/DBIx-Class.git] / t / run / 21transactions.tl
CommitLineData
a62cf8d4 1sub run_tests {
2my $schema = shift;
3plan tests => 37;
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
16# Test successful txn_do() - scalar context
17{
18 my @titles = map {'txn_do test CD ' . $_} (1..5);
19 my $artist = $schema->resultset('Artist')->find(1);
20 my $count_before = $artist->cds->count;
21 my $count_after = $schema->txn_do($code, $artist, @titles);
22 is($count_after, $count_before+5, 'successful txn added 5 cds');
23 is($artist->cds({
24 title => "txn_do test CD $_",
25 })->first->year, 2006, "new CD $_ year correct") for (1..5);
26}
27
28# Test successful txn_do() - list context
29{
30 my @titles = map {'txn_do test CD ' . $_} (6..10);
31 my $artist = $schema->resultset('Artist')->find(1);
32 my $count_before = $artist->cds->count;
33 my @cds = $schema->txn_do($code, $artist, @titles);
34 is(scalar @cds, $count_before+5, 'added 5 CDs and returned in list context');
35 is($artist->cds({
36 title => "txn_do test CD $_",
37 })->first->year, 2006, "new CD $_ year correct") for (6..10);
38}
39
40# Test nested successful txn_do()
41{
42 my $nested_code = sub {
43 my ($schema, $artist, $code) = @_;
44
45 my @titles1 = map {'nested txn_do test CD ' . $_} (1..5);
46 my @titles2 = map {'nested txn_do test CD ' . $_} (6..10);
47
48 $schema->txn_do($code, $artist, @titles1);
49 $schema->txn_do($code, $artist, @titles2);
50 };
51
52 my $artist = $schema->resultset('Artist')->find(2);
53 my $count_before = $artist->cds->count;
54
55 eval {
56 $schema->txn_do($nested_code, $schema, $artist, $code);
57 };
58
59 my $error = $@;
60
61 ok(!$error, 'nested txn_do succeeded');
62 is($artist->cds({
63 title => 'nested txn_do test CD '.$_,
64 })->first->year, 2006, qq{nested txn_do CD$_ year ok}) for (1..10);
65 is($artist->cds->count, $count_before+10, 'nested txn_do added all CDs');
66}
67
68my $fail_code = sub {
69 my ($artist) = @_;
70 $artist->create_related('cds', {
71 title => 'this should not exist',
72 year => 2005,
73 });
74 die "the sky is falling";
75};
76
77# Test failed txn_do()
78{
79 my $artist = $schema->resultset('Artist')->find(3);
80
81 eval {
82 $schema->txn_do($fail_code, $artist);
83 };
84
85 my $error = $@;
86
87 like($error, qr/the sky is falling/, 'failed txn_do threw an exception');
88 my $cd = $artist->cds({
89 title => 'this should not exist',
90 year => 2005,
91 })->first;
92 ok(!defined($cd), q{failed txn_do didn't change the cds table});
93}
94
95# Test failed txn_do() with failed rollback
96{
97 my $artist = $schema->resultset('Artist')->find(3);
98
99 # Force txn_rollback() to throw an exception
100 no warnings 'redefine';
101 local *{"DBIx::Class::Schema::txn_rollback"} = sub{die 'FAILED'};
102
103 eval {
104 $schema->txn_do($fail_code, $artist);
105 };
106
107 my $error = $@;
108
109 like($error, qr/Rollback failed/, 'failed txn_do with a failed '.
110 'txn_rollback threw a rollback exception');
111 like($error, qr/the sky is falling/, 'failed txn_do with a failed '.
112 'txn_rollback included the original exception');
113
114 my $cd = $artist->cds({
115 title => 'this should not exist',
116 year => 2005,
117 })->first;
118 isa_ok($cd, 'DBICTest::CD', q{failed txn_do with a failed txn_rollback }.
119 q{changed the cds table});
120 $cd->delete; # Rollback failed
121 $cd = $artist->cds({
122 title => 'this should not exist',
123 year => 2005,
124 })->first;
125 ok(!defined($cd), q{deleted the failed txn's cd});
126 $schema->storage->{transaction_depth} = 0; # Must reset this or further tests
127 # will fail
128}
129
130# Test nested failed txn_do()
131{
132 my $nested_fail_code = sub {
133 my ($schema, $artist, $code1, $code2) = @_;
134
135 my @titles = map {'nested txn_do test CD ' . $_} (1..5);
136
137 $schema->txn_do($code1, $artist, @titles); # successful txn
138 $schema->txn_do($code2, $artist); # failed txn
139 };
140
141 my $artist = $schema->resultset('Artist')->find(3);
142
143 eval {
144 $schema->txn_do($nested_fail_code, $schema, $artist, $code, $fail_code);
145 };
146
147 my $error = $@;
148
149 like($error, qr/the sky is falling/, 'nested failed txn_do threw exception');
150 ok(!defined($artist->cds({
151 title => 'nested txn_do test CD '.$_,
152 year => 2006,
153 })->first), qq{failed txn_do didn't add first txn's cd $_}) for (1..5);
154 my $cd = $artist->cds({
155 title => 'this should not exist',
156 year => 2005,
157 })->first;
158 ok(!defined($cd), q{failed txn_do didn't add failed txn's cd});
159}
160}
161
1621;