Final count tests
[dbsrgits/DBIx-Class-Historic.git] / t / 76joins.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
cb3e35d2 7use Data::Dumper;
949172b0 8use DBIC::SqlMakerTest;
70350518 9
a47e1233 10my $schema = DBICTest->init_schema();
0567538f 11
4d91ad3f 12my $orig_debug = $schema->storage->debug;
13
0567538f 14use IO::File;
15
16BEGIN {
17 eval "use DBD::SQLite";
18 plan $@
19 ? ( skip_all => 'needs DBD::SQLite for testing' )
0f6fc705 20 : ( tests => 33 );
0567538f 21}
22
58ff4acf 23# figure out if we've got a version of sqlite that is older than 3.2.6, in
24# which case COUNT(DISTINCT()) doesn't work
25my $is_broken_sqlite = 0;
26my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
27 split /\./, $schema->storage->dbh->get_info(18);
28if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
29 ( ($sqlite_major_ver < 3) ||
30 ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
31 ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
32 $is_broken_sqlite = 1;
33}
485f6e10 34
0567538f 35# test the abstract join => SQL generator
36my $sa = new DBIC::SQL::Abstract;
37
38my @j = (
39 { child => 'person' },
40 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
41 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
42);
43my $match = 'person child JOIN person father ON ( father.person_id = '
44 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
45 . '= child.mother_id )'
46 ;
9b459129 47is_same_sql_bind(
48 $sa->_recurse_from(@j), [],
49 $match, [],
50 'join 1 ok'
51);
0567538f 52
53my @j2 = (
54 { mother => 'person' },
55 [ [ { child => 'person' },
56 [ { father => 'person' },
57 { 'father.person_id' => 'child.father_id' }
58 ]
59 ],
60 { 'mother.person_id' => 'child.mother_id' }
61 ],
62);
63$match = 'person mother JOIN (person child JOIN person father ON ('
64 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
65 . 'child.mother_id )'
66 ;
9b459129 67is_same_sql_bind(
68 $sa->_recurse_from(@j2), [],
69 $match, [],
70 'join 2 ok'
71);
72
0567538f 73
74my @j3 = (
75 { child => 'person' },
76 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
77 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
78);
79$match = 'person child INNER JOIN person father ON ( father.person_id = '
80 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
81 . '= child.mother_id )'
82 ;
83
9b459129 84is_same_sql_bind(
85 $sa->_recurse_from(@j3), [],
86 $match, [],
87 'join 3 (inner join) ok'
88);
0567538f 89
ca7b9fdf 90my @j4 = (
91 { mother => 'person' },
92 [ [ { child => 'person', -join_type => 'left' },
93 [ { father => 'person', -join_type => 'right' },
94 { 'father.person_id' => 'child.father_id' }
95 ]
96 ],
97 { 'mother.person_id' => 'child.mother_id' }
98 ],
99);
100$match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
101 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
102 . 'child.mother_id )'
103 ;
9b459129 104is_same_sql_bind(
105 $sa->_recurse_from(@j4), [],
106 $match, [],
107 'join 4 (nested joins + join types) ok'
108);
ca7b9fdf 109
635b9634 110my @j5 = (
111 { child => 'person' },
112 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
113 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
114);
115$match = 'person child JOIN person father ON ( father.person_id != '
116 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
117 . '= child.mother_id )'
118 ;
9b459129 119is_same_sql_bind(
120 $sa->_recurse_from(@j5), [],
121 $match, [],
122 'join 5 (SCALAR reference for ON statement) ok'
123);
635b9634 124
125my @j6 = (
126 { child => 'person' },
127 [ { father => 'person' }, { 'father.person_id' => { '!=', '42' } }, ],
128 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
129);
11aada86 130$match = qr/^HASH reference arguments are not supported in JOINS - try using "\.\.\." instead/;
635b9634 131eval { $sa->_recurse_from(@j6) };
132like( $@, $match, 'join 6 (HASH reference for ON statement dies) ok' );
133
f9db5527 134my $rs = $schema->resultset("CD")->search(
0567538f 135 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
136 { from => [ { 'me' => 'cd' },
137 [
138 { artist => 'artist' },
139 { 'me.artist' => 'artist.artistid' }
140 ] ] }
141 );
142
cb9b5b23 143is( $rs + 0, 1, "Single record in resultset");
0567538f 144
145is($rs->first->title, 'Forkful of bees', 'Correct record returned');
146
f9db5527 147$rs = $schema->resultset("CD")->search(
0567538f 148 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
149 { join => 'artist' });
150
cb9b5b23 151is( $rs + 0, 1, "Single record in resultset");
0567538f 152
153is($rs->first->title, 'Forkful of bees', 'Correct record returned');
154
f9db5527 155$rs = $schema->resultset("CD")->search(
0567538f 156 { 'artist.name' => 'We Are Goth',
157 'liner_notes.notes' => 'Kill Yourself!' },
158 { join => [ qw/artist liner_notes/ ] });
159
cb9b5b23 160is( $rs + 0, 1, "Single record in resultset");
0567538f 161
162is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
163
8fe164b9 164# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 165$rs = $schema->resultset("CD")->search(
8fe164b9 166 { 'artist' => 1 },
167 { join => [qw/artist/], order_by => 'artist.name' }
168);
cb9b5b23 169is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
8fe164b9 170
2bd9c7c0 171ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
172 'Slicing beyond end of rs returns a zero count');
173
f9db5527 174$rs = $schema->resultset("Artist")->search(
0567538f 175 { 'liner_notes.notes' => 'Kill Yourself!' },
176 { join => { 'cds' => 'liner_notes' } });
177
cb9b5b23 178is( $rs->count, 1, "Single record in resultset");
0567538f 179
180is($rs->first->name, 'We Are Goth', 'Correct record returned');
181
9f2d17e9 182
0f6fc705 183{
184 $schema->populate('Artist', [
185 [ qw/artistid name/ ],
186 [ 4, 'Another Boy Band' ],
187 ]);
188 $schema->populate('CD', [
189 [ qw/cdid artist title year/ ],
190 [ 6, 2, "Greatest Hits", 2001 ],
191 [ 7, 4, "Greatest Hits", 2005 ],
192 [ 8, 4, "BoyBandBlues", 2008 ],
193 ]);
194 $schema->populate('TwoKeys', [
195 [ qw/artist cd/ ],
196 [ 2, 4 ],
197 [ 2, 6 ],
198 [ 4, 7 ],
199 [ 4, 8 ],
200 ]);
201
202 sub cd_count {
203 return $schema->resultset("CD")->count;
204 }
205 sub tk_count {
206 return $schema->resultset("TwoKeys")->count;
207 }
208
cb9b5b23 209 is(cd_count(), 8, '8 rows in table cd');
210 is(tk_count(), 7, '7 rows in table twokeys');
0f6fc705 211
212 sub artist1 {
213 return $schema->resultset("CD")->search(
214 { 'artist.name' => 'Caterwauler McCrae' },
215 { join => [qw/artist/]}
216 );
217 }
218 sub artist2 {
219 return $schema->resultset("CD")->search(
220 { 'artist.name' => 'Random Boy Band' },
221 { join => [qw/artist/]}
222 );
223 }
224
cb9b5b23 225 is( artist1()->count, 3, '3 Caterwauler McCrae CDs' );
0f6fc705 226 ok( artist1()->delete, 'Successfully deleted 3 CDs' );
cb9b5b23 227 is( artist1()->count, 0, '0 Caterwauler McCrae CDs' );
228 is( artist2()->count, 2, '3 Random Boy Band CDs' );
0f6fc705 229 ok( artist2()->update( { 'artist' => 1 } ) );
cb9b5b23 230 is( artist2()->count, 0, '0 Random Boy Band CDs' );
231 is( artist1()->count, 2, '2 Caterwauler McCrae CDs' );
0f6fc705 232
233 # test update on multi-column-pk
234 sub tk1 {
235 return $schema->resultset("TwoKeys")->search(
236 {
237 'artist.name' => { like => '%Boy Band' },
238 'cd.title' => 'Greatest Hits',
239 },
240 { join => [qw/artist cd/] }
241 );
242 }
243 sub tk2 {
244 return $schema->resultset("TwoKeys")->search(
245 { 'artist.name' => 'Caterwauler McCrae' },
246 { join => [qw/artist/]}
247 );
248 }
cb9b5b23 249 is( tk2()->count, 2, 'TwoKeys count == 2' );
250 is( tk1()->count, 2, 'TwoKeys count == 2' );
0f6fc705 251 ok( tk1()->update( { artist => 1 } ) );
cb9b5b23 252 is( tk1()->count, 0, 'TwoKeys count == 0' );
253 is( tk2()->count, 4, '2 Caterwauler McCrae CDs' );
0f6fc705 254 ok( tk2()->delete, 'Successfully deleted 4 CDs' );
cb9b5b23 255 is(cd_count(), 5, '5 rows in table cd');
256 is(tk_count(), 3, '3 rows in table twokeys');
0f6fc705 257}