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