Bring back resolve_condition deleted in 03f6d1f7
[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 ':DiffSQL';
7
8 my $schema = DBICTest->init_schema();
9
10 # test the abstract join => SQL generator
11 my $sa = DBIx::Class::SQLMaker->new;
12
13 my @j = (
14     { child => 'person' },
15     [ { father => 'person' }, { 'father.person_id' => 'child.father_id' }, ],
16     [ { mother => 'person' }, { 'mother.person_id' => 'child.mother_id' } ],
17 );
18 my $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           ;
22 is_same_sql(
23   $sa->_recurse_from(@j),
24   $match,
25   'join 1 ok'
26 );
27
28 my @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        ;
42 is_same_sql(
43   $sa->_recurse_from(@j2),
44   $match,
45   'join 2 ok'
46 );
47
48
49 my @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
59 is_same_sql(
60   $sa->_recurse_from(@j3),
61   $match,
62   'join 3 (inner join) ok'
63 );
64
65 my @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        ;
79 is_same_sql(
80   $sa->_recurse_from(@j4),
81   $match,
82   'join 4 (nested joins + join types) ok'
83 );
84
85 my @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           ;
94 is_same_sql(
95   $sa->_recurse_from(@j5),
96   $match,
97   'join 5 (SCALAR reference for ON statement) ok'
98 );
99
100 my $rs = $schema->resultset("CD")->search(
101            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
102            { from => [ { 'me' => 'cd' },
103                          [
104                            { artist => 'artist' },
105                            { 'me.artist' => 'artist.artistid' }
106                          ] ] }
107          );
108
109 is( $rs + 0, 1, "Single record in resultset");
110
111 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
112
113 $rs = $schema->resultset("CD")->search(
114            { 'year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
115            { join => 'artist' });
116
117 is( $rs + 0, 1, "Single record in resultset");
118
119 is($rs->first->title, 'Forkful of bees', 'Correct record returned');
120
121 $rs = $schema->resultset("CD")->search(
122            { 'artist.name' => 'We Are Goth',
123              'liner_notes.notes' => 'Kill Yourself!' },
124            { join => [ qw/artist liner_notes/ ] });
125
126 is( $rs + 0, 1, "Single record in resultset");
127
128 is($rs->first->title, 'Come Be Depressed With Us', 'Correct record returned');
129
130 # when using join attribute, make sure slice()ing all objects has same count as all()
131 $rs = $schema->resultset("CD")->search(
132     { 'artist' => 1 },
133     { join => [qw/artist/], order_by => 'artist.name' }
134 );
135 is( scalar $rs->all, scalar $rs->slice(0, $rs->count - 1), 'slice() with join has same count as all()' );
136
137 ok(!$rs->slice($rs->count+1000, $rs->count+1002)->count,
138   'Slicing beyond end of rs returns a zero count');
139
140 $rs = $schema->resultset("Artist")->search(
141         { 'liner_notes.notes' => 'Kill Yourself!' },
142         { join => { 'cds' => 'liner_notes' } });
143
144 is( $rs->count, 1, "Single record in resultset");
145
146 is($rs->first->name, 'We Are Goth', 'Correct record returned');
147
148
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     ]);
167
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' );
191
192     # test update on multi-column-pk
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');
214 }
215
216 done_testing;