Switch most remaining debug-hooks to $dbictest_schema->is_executed_querycount()
[dbsrgits/DBIx-Class.git] / t / 76joins.t
CommitLineData
70350518 1use strict;
8273e845 2use warnings;
70350518 3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
949172b0 7use DBIC::SqlMakerTest;
70350518 8
a47e1233 9my $schema = DBICTest->init_schema();
0567538f 10
0567538f 11# test the abstract join => SQL generator
49eeb48d 12my $sa = DBIx::Class::SQLMaker->new;
0567538f 13
14my @j = (
15 { child => 'person' },
16 [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
17 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
18);
19my $match = 'person child JOIN person father ON ( father.person_id = '
20 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
21 . '= child.mother_id )'
22 ;
af6aac2d 23is_same_sql(
24 $sa->_recurse_from(@j),
25 $match,
9b459129 26 'join 1 ok'
27);
0567538f 28
29my @j2 = (
30 { mother => 'person' },
31 [ [ { child => 'person' },
32 [ { father => 'person' },
33 { 'father.person_id' => 'child.father_id' }
34 ]
35 ],
36 { 'mother.person_id' => 'child.mother_id' }
37 ],
38);
39$match = 'person mother JOIN (person child JOIN person father ON ('
40 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
41 . 'child.mother_id )'
42 ;
af6aac2d 43is_same_sql(
44 $sa->_recurse_from(@j2),
45 $match,
9b459129 46 'join 2 ok'
47);
48
0567538f 49
50my @j3 = (
51 { child => 'person' },
52 [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
53 [ { mother => 'person', -join_type => 'inner' }, { 'mother.person_id' => 'child.mother_id' } ],
54);
55$match = 'person child INNER JOIN person father ON ( father.person_id = '
56 . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
57 . '= child.mother_id )'
58 ;
59
af6aac2d 60is_same_sql(
61 $sa->_recurse_from(@j3),
62 $match,
9b459129 63 'join 3 (inner join) ok'
64);
0567538f 65
ca7b9fdf 66my @j4 = (
67 { mother => 'person' },
68 [ [ { child => 'person', -join_type => 'left' },
69 [ { father => 'person', -join_type => 'right' },
70 { 'father.person_id' => 'child.father_id' }
71 ]
72 ],
73 { 'mother.person_id' => 'child.mother_id' }
74 ],
75);
76$match = 'person mother LEFT JOIN (person child RIGHT JOIN person father ON ('
77 . ' father.person_id = child.father_id )) ON ( mother.person_id = '
78 . 'child.mother_id )'
79 ;
af6aac2d 80is_same_sql(
81 $sa->_recurse_from(@j4),
82 $match,
9b459129 83 'join 4 (nested joins + join types) ok'
84);
ca7b9fdf 85
635b9634 86my @j5 = (
87 { child => 'person' },
88 [ { father => 'person' }, { 'father.person_id' => \'!= child.father_id' }, ],
89 [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
90);
91$match = 'person child JOIN person father ON ( father.person_id != '
92 . 'child.father_id ) JOIN person mother ON ( mother.person_id '
93 . '= child.mother_id )'
94 ;
af6aac2d 95is_same_sql(
96 $sa->_recurse_from(@j5),
97 $match,
9b459129 98 'join 5 (SCALAR reference for ON statement) ok'
99);
635b9634 100
f9db5527 101my $rs = $schema->resultset("CD")->search(
0567538f 102 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
103 { from => [ { 'me' => 'cd' },
104 [
105 { artist => 'artist' },
106 { 'me.artist' => 'artist.artistid' }
107 ] ] }
108 );
109
cb9b5b23 110is( $rs + 0, 1, "Single record in resultset");
0567538f 111
112is($rs->first->title, 'Forkful of bees', 'Correct record returned');
113
f9db5527 114$rs = $schema->resultset("CD")->search(
0567538f 115 { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
116 { join => 'artist' });
117
cb9b5b23 118is( $rs + 0, 1, "Single record in resultset");
0567538f 119
120is($rs->first->title, 'Forkful of bees', 'Correct record returned');
121
f9db5527 122$rs = $schema->resultset("CD")->search(
0567538f 123 { 'artist.name' => 'We Are Goth',
124 'liner_notes.notes' => 'Kill Yourself!' },
125 { join => [ qw/artist liner_notes/ ] });
126
cb9b5b23 127is( $rs + 0, 1, "Single record in resultset");
0567538f 128
129is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
130
8fe164b9 131# when using join attribute, make sure slice()ing all objects has same count as all()
f9db5527 132$rs = $schema->resultset("CD")->search(
8fe164b9 133 { 'artist' => 1 },
134 { join => [qw/artist/], order_by => 'artist.name' }
135);
cb9b5b23 136is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
8fe164b9 137
2bd9c7c0 138ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
139 'Slicing beyond end of rs returns a zero count');
140
f9db5527 141$rs = $schema->resultset("Artist")->search(
0567538f 142 { 'liner_notes.notes' => 'Kill Yourself!' },
143 { join => { 'cds' => 'liner_notes' } });
144
cb9b5b23 145is( $rs->count, 1, "Single record in resultset");
0567538f 146
147is($rs->first->name, 'We Are Goth', 'Correct record returned');
148
9f2d17e9 149
0f6fc705 150{
151 $schema->populate('Artist', [
152 [ qw/artistid name/ ],
153 [ 4, 'Another Boy Band' ],
154 ]);
155 $schema->populate('CD', [
156 [ qw/cdid artist title year/ ],
157 [ 6, 2, "Greatest Hits", 2001 ],
158 [ 7, 4, "Greatest Hits", 2005 ],
159 [ 8, 4, "BoyBandBlues", 2008 ],
160 ]);
161 $schema->populate('TwoKeys', [
162 [ qw/artist cd/ ],
163 [ 2, 4 ],
164 [ 2, 6 ],
165 [ 4, 7 ],
166 [ 4, 8 ],
167 ]);
8273e845 168
65d35121 169 my $cd_count = sub { $schema->resultset("CD")->count };
170 my $tk_count = sub { $schema->resultset("TwoKeys")->count };
171
172 is($cd_count->(), 8, '8 rows in table cd');
173 is($tk_count->(), 7, '7 rows in table twokeys');
174
175 my $artist1_rs = $schema->resultset("CD")->search(
176 { 'artist.name' => 'Caterwauler McCrae' },
177 { join => [qw/artist/]}
178 );
179
180 my $artist2_rs = $schema->resultset("CD")->search(
181 { 'artist.name' => 'Random Boy Band' },
182 { join => [qw/artist/]}
183 );
184
185 is( $artist1_rs->count, 3, '3 Caterwauler McCrae CDs' );
186 ok( $artist1_rs->delete, 'Successfully deleted 3 CDs' );
187 is( $artist1_rs->count, 0, '0 Caterwauler McCrae CDs' );
188 is( $artist2_rs->count, 2, '3 Random Boy Band CDs' );
189 ok( $artist2_rs->update( { 'artist' => 1 } ) );
190 is( $artist2_rs->count, 0, '0 Random Boy Band CDs' );
191 is( $artist1_rs->count, 2, '2 Caterwauler McCrae CDs' );
0f6fc705 192
193 # test update on multi-column-pk
65d35121 194 my $tk1_rs = $schema->resultset("TwoKeys")->search(
195 {
196 'artist.name' => { like => '%Boy Band' },
197 'cd.title' => 'Greatest Hits',
198 },
199 { join => [qw/artist cd/] }
200 );
201
202 my $tk2_rs = $schema->resultset("TwoKeys")->search(
203 { 'artist.name' => 'Caterwauler McCrae' },
204 { join => [qw/artist/]}
205 );
206
207 is( $tk2_rs->count, 2, 'TwoKeys count == 2' );
208 is( $tk1_rs->count, 2, 'TwoKeys count == 2' );
209 ok( $tk1_rs->update( { artist => 1 } ) );
210 is( $tk1_rs->count, 0, 'TwoKeys count == 0' );
211 is( $tk2_rs->count, 4, '2 Caterwauler McCrae CDs' );
212 ok( $tk2_rs->delete, 'Successfully deleted 4 CDs' );
213 is($cd_count->(), 5, '5 rows in table cd');
214 is($tk_count->(), 3, '3 rows in table twokeys');
0f6fc705 215}
56166f36 216
217done_testing;