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