fix attrs on find
[dbsrgits/DBIx-Class.git] / t / run / 16joins.tl
1 sub run_tests {
2 my $schema = shift;
3
4 use IO::File;
5
6 BEGIN {
7     eval "use DBD::SQLite";
8     plan $@
9         ? ( skip_all => 'needs DBD::SQLite for testing' )
10         : ( tests => 33 );
11 }
12
13 # test the abstract join => SQL generator
14 my $sa = new DBIC::SQL::Abstract;
15
16 my @j = (
17     { child => 'person' },
18     [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
19     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
20 );
21 my $match = 'person child JOIN person father ON ( father.person_id = '
22           . 'child.father_id ) JOIN person mother ON ( mother.person_id '
23           . '= child.mother_id )'
24           ;
25 is( $sa->_recurse_from(@j), $match, 'join 1 ok' );
26
27 my @j2 = (
28     { mother => 'person' },
29     [   [   { child => 'person' },
30             [   { father             => 'person' },
31                 { 'father.person_id' => 'child.father_id' }
32             ]
33         ],
34         { 'mother.person_id' => 'child.mother_id' }
35     ],
36 );
37 $match = 'person mother JOIN (person child JOIN person father ON ('
38        . ' father.person_id = child.father_id )) ON ( mother.person_id = '
39        . 'child.mother_id )'
40        ;
41 is( $sa->_recurse_from(@j2), $match, 'join 2 ok' );
42
43 my @j3 = (
44     { child => 'person' },
45     [ { father => 'person', -join_type => 'inner' }, { 'father.person_id' => 'child.father_id' }, ],
46     [ { mother => 'person', -join_type => 'inner'  }, { 'mother.person_id' => 'child.mother_id' } ],
47 );
48 $match = 'person child INNER JOIN person father ON ( father.person_id = '
49           . 'child.father_id ) INNER JOIN person mother ON ( mother.person_id '
50           . '= child.mother_id )'
51           ;
52
53 is( $sa->_recurse_from(@j3), $match, 'join 3 (inner join) ok');
54
55 my $rs = $schema->resultset("CD")->search(
56            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
57            { from => [ { 'me' => 'cd' },
58                          [
59                            { artist => 'artist' },
60                            { 'me.artist' => 'artist.artistid' }
61                          ] ] }
62          );
63
64 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
65
66 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
67
68 $rs = $schema->resultset("CD")->search(
69            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
70            { join => 'artist' });
71
72 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
73
74 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
75
76 $rs = $schema->resultset("CD")->search(
77            { 'artist.name' => 'We Are Goth',
78              'liner_notes.notes' => 'Kill Yourself!' },
79            { join => [ qw/artist liner_notes/ ] });
80
81 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
82
83 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
84
85 # when using join attribute, make sure slice()ing all objects has same count as all()
86 $rs = $schema->resultset("CD")->search(
87     { 'artist' => 1 },
88     { join => [qw/artist/], order_by => 'artist.name' }
89 );
90 cmp_ok( scalar $rs->all, '==', scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
91
92 $rs = $schema->resultset("Artist")->search(
93         { 'liner_notes.notes' => 'Kill Yourself!' },
94         { join => { 'cds' => 'liner_notes' } });
95
96 cmp_ok( $rs->count, '==', 1, "Single record in resultset");
97
98 is($rs->first->name, 'We Are Goth', 'Correct record returned');
99
100 $rs = $schema->resultset("CD")->search(
101            { 'artist.name' => 'Caterwauler McCrae' },
102            { prefetch => [ qw/artist liner_notes/ ],
103              order_by => 'me.cdid' });
104
105 cmp_ok($rs->count, '==', 3, 'Correct number of records returned');
106
107 # start test for prefetch SELECT count
108 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
109 DBI->trace(1, 't/var/dbic.trace');
110
111 my @cd = $rs->all;
112
113 is($cd[0]->title, 'Spoonful of bees', 'First record returned ok');
114
115 ok(!defined $cd[0]->liner_notes, 'No prefetch for NULL LEFT join');
116
117 is($cd[1]->{_relationship_data}{liner_notes}->notes, 'Buy Whiskey!', 'Prefetch for present LEFT JOIN');
118
119 is(ref $cd[1]->liner_notes, 'DBICTest::LinerNotes', 'Prefetch returns correct class');
120
121 is($cd[2]->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'Prefetch on parent object ok');
122
123 # count the SELECTs
124 DBI->trace(0, undef);
125 my $selects = 0;
126 my $trace = IO::File->new('t/var/dbic.trace', '<') 
127     or die "Unable to read trace file";
128 while (<$trace>) {
129     $selects++ if /SELECT/;
130 }
131 $trace->close;
132 unlink 't/var/dbic.trace';
133 is($selects, 1, 'prefetch ran only 1 select statement');
134
135 # start test for nested prefetch SELECT count
136 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
137 DBI->trace(1, 't/var/dbic.trace');
138
139 $rs = $schema->resultset('Tag')->search(
140   {},
141   {
142     prefetch => { cd => 'artist' }
143   }
144 );
145
146 my $tag = $rs->first;
147
148 is( $tag->cd->title, 'Spoonful of bees', 'step 1 ok for nested prefetch' );
149
150 is( $tag->cd->artist->name, 'Caterwauler McCrae', 'step 2 ok for nested prefetch');
151
152 # count the SELECTs
153 DBI->trace(0, undef);
154 $selects = 0;
155 $trace = IO::File->new('t/var/dbic.trace', '<') 
156     or die "Unable to read trace file";
157 while (<$trace>) {
158     $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
159 }
160 $trace->close;
161 unlink 't/var/dbic.trace';
162 is($selects, 1, 'nested prefetch ran exactly 1 select statement (excluding column_info)');
163
164 # start test for prefetch on find SELECT count
165 unlink 't/var/dbic.trace' if -e 't/var/dbic.trace';
166 DBI->trace(1, 't/var/dbic.trace');
167
168 my $cd = $schema->resultset('CD')->find(1, { prefetch => 'artist' });
169
170 is($cd->{_inflated_column}{artist}->name, 'Caterwauler McCrae', 'artist prefetched correctly on find');
171
172 # count the SELECTs
173 DBI->trace(0, undef);
174 $selects = 0;
175 $trace = IO::File->new('t/var/dbic.trace', '<') 
176     or die "Unable to read trace file";
177 while (<$trace>) {
178     $selects++ if /SELECT(?!.*WHERE 1=0.*)/;
179 }
180 $trace->close;
181 unlink 't/var/dbic.trace';
182 is($selects, 1, 'find with prefetch ran exactly 1 select statement (excluding column_info)');
183
184 $rs = $schema->resultset('Tag')->search(
185   {},
186   {
187     join => { cd => 'artist' },
188     prefetch => { cd => 'artist' }
189   }
190 );
191
192 cmp_ok( $rs->count, '>=', 0, 'nested prefetch does not duplicate joins' );
193
194 my ($artist) = $schema->resultset("Artist")->search({ 'cds.year' => 2001 },
195                  { order_by => 'artistid DESC', join => 'cds' });
196
197 is($artist->name, 'Random Boy Band', "Join search by object ok");
198
199 my @cds = $schema->resultset("CD")->search({ 'liner_notes.notes' => 'Buy Merch!' },
200                                { join => 'liner_notes' });
201
202 cmp_ok(scalar @cds, '==', 1, "Single CD retrieved via might_have");
203
204 is($cds[0]->title, "Generic Manufactured Singles", "Correct CD retrieved");
205
206 my @artists = $schema->resultset("Artist")->search({ 'tags.tag' => 'Shiny' },
207                                        { join => { 'cds' => 'tags' } });
208
209 cmp_ok( @artists, '==', 2, "two-join search ok" );
210
211 $rs = $schema->resultset("CD")->search(
212   {},
213   { group_by => [qw/ title me.cdid /] }
214 );
215
216 cmp_ok( $rs->count, '==', 5, "count() ok after group_by on main pk" );
217
218 cmp_ok( scalar $rs->all, '==', 5, "all() returns same count as count() after group_by on main pk" );
219
220 $rs = $schema->resultset("CD")->search(
221   {},
222   { join => [qw/ artist /], group_by => [qw/ artist.name /] }
223 );
224
225 cmp_ok( $rs->count, '==', 3, "count() ok after group_by on related column" );
226
227 cmp_ok( scalar $rs->all, '==', 3, "all() returns same count as count() after group_by on related column" );
228 }
229
230 1;