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