Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / prefetch / incomplete.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Deep;
8 use Test::Exception;
9
10 use DBICTest ':DiffSQL';
11
12 my $schema = DBICTest->init_schema();
13
14 lives_ok(sub {
15   # while cds.* will be selected anyway (prefetch implies it)
16   # only the requested me.name column will be fetched.
17
18   # reference sql with select => [...]
19   #   SELECT me.name, cds.title, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ...
20
21   my $rs = $schema->resultset('Artist')->search(
22     { 'cds.title' => { '!=', 'Generic Manufactured Singles' } },
23     {
24       prefetch => [ qw/ cds / ],
25       order_by => [ { -desc => 'me.name' }, 'cds.title' ],
26       select => [qw/ me.name cds.title / ],
27     },
28   );
29
30   is ($rs->count, 2, 'Correct number of collapsed artists');
31   my ($we_are_goth) = $rs->all;
32   is ($we_are_goth->name, 'We Are Goth', 'Correct first artist');
33   is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist');
34   is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist');
35 }, 'explicit prefetch on a keyless object works');
36
37 lives_ok ( sub {
38
39   my $rs = $schema->resultset('CD')->search(
40     {},
41     {
42       order_by => [ { -desc => 'me.year' } ],
43     }
44   );
45   my $years = [qw/ 2001 2001 1999 1998 1997/];
46
47   cmp_deeply (
48     [ $rs->search->get_column('me.year')->all ],
49     $years,
50     'Expected years (at least one duplicate)',
51   );
52
53   my @cds_and_tracks;
54   for my $cd ($rs->all) {
55     my $data = { year => $cd->year, cdid => $cd->cdid };
56     for my $tr ($cd->tracks->all) {
57       push @{$data->{tracks}}, { $tr->get_columns };
58     }
59     @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
60     push @cds_and_tracks, $data;
61   }
62
63   my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' });
64
65   my @pref_cds_and_tracks;
66   for my $cd ($pref_rs->all) {
67     my $data = { $cd->get_columns };
68     for my $tr ($cd->tracks->all) {
69       push @{$data->{tracks}}, { $tr->get_columns };
70     }
71     @{$data->{tracks}} = sort { $a->{trackid} <=> $b->{trackid} } @{$data->{tracks}};
72     push @pref_cds_and_tracks, $data;
73   }
74
75   cmp_deeply (
76     \@pref_cds_and_tracks,
77     \@cds_and_tracks,
78     'Correct collapsing on non-unique primary object'
79   );
80
81   cmp_deeply (
82     $pref_rs->search ({}, { order_by => [ { -desc => 'me.year' }, 'trackid' ] })->all_hri,
83     \@cds_and_tracks,
84     'Correct HRI collapsing on non-unique primary object'
85   );
86
87 }, 'weird collapse lives');
88
89
90 lives_ok(sub {
91   # test implicit prefetch as well
92
93   my $rs = $schema->resultset('CD')->search(
94     { title => 'Generic Manufactured Singles' },
95     {
96       join=> 'artist',
97       select => [qw/ me.title artist.name / ],
98     }
99   );
100
101   my $cd = $rs->next;
102   is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
103   isa_ok ($cd->artist, 'DBICTest::Artist');
104   is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
105
106 }, 'implicit keyless prefetch works');
107
108 # sane error
109 throws_ok(
110   sub {
111     $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
112   },
113   qr|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|,
114   'Sensible error message on mis-specified "as"',
115 );
116
117 # check complex limiting prefetch without the join-able columns
118 {
119   my $pref_rs = $schema->resultset('Owners')->search({}, {
120     rows => 3,
121     offset => 1,
122     order_by => 'name',
123     columns => 'name',  # only the owner name, still prefetch all the books
124     prefetch => 'books',
125   });
126
127   is_same_sql_bind(
128     $pref_rs->as_query,
129     '(
130       SELECT me.name, books.id, books.source, books.owner, books.title, books.price
131         FROM (
132           SELECT me.name, me.id
133             FROM owners me
134           ORDER BY name
135           LIMIT ?
136           OFFSET ?
137         ) me
138         LEFT JOIN books books
139           ON books.owner = me.id
140       ORDER BY name
141     )',
142     [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
143     'Expected SQL on complex limited prefetch with non-selected join condition',
144   );
145
146   is_deeply (
147     $pref_rs->all_hri,
148     [ {
149       name => "Waltham",
150       books => [ {
151         id => 3,
152         owner => 2,
153         price => 65,
154         source => "Library",
155         title => "Best Recipe Cookbook",
156       } ],
157     } ],
158     'Expected result on complex limited prefetch with non-selected join condition'
159   );
160
161   my $empty_ordered_pref_rs = $pref_rs->search({}, {
162     columns => [],  # nothing, we only prefetch the book data
163     order_by => 'me.name',
164   });
165   my $empty_ordered_pref_hri = [ {
166     books => [ {
167       id => 3,
168       owner => 2,
169       price => 65,
170       source => "Library",
171       title => "Best Recipe Cookbook",
172     } ],
173   } ];
174
175   is_same_sql_bind(
176     $empty_ordered_pref_rs->as_query,
177     '(
178       SELECT books.id, books.source, books.owner, books.title, books.price
179         FROM (
180           SELECT me.id, me.name
181             FROM owners me
182           ORDER BY me.name
183           LIMIT ?
184           OFFSET ?
185         ) me
186         LEFT JOIN books books
187           ON books.owner = me.id
188       ORDER BY me.name
189     )',
190     [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
191     'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
192   );
193
194   is_deeply (
195     $empty_ordered_pref_rs->all_hri,
196     $empty_ordered_pref_hri,
197     'Expected result on *ordered* complex limited prefetch with non-selected root data'
198   );
199
200   $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
201     order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
202   });
203
204   is_same_sql_bind(
205     $empty_ordered_pref_rs->as_query,
206     '(
207       SELECT books.id, books.source, books.owner, books.title, books.price
208         FROM (
209           SELECT me.id, me.name
210             FROM owners me
211           ORDER BY LENGTH(me.name), RANDOM()
212           LIMIT ?
213           OFFSET ?
214         ) me
215         LEFT JOIN books books
216           ON books.owner = me.id
217       ORDER BY LENGTH(me.name), RANDOM()
218     )',
219     [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
220     'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
221   );
222
223   is_deeply (
224     $empty_ordered_pref_rs->all_hri,
225     $empty_ordered_pref_hri,
226     'Expected result on *function-ordered* complex limited prefetch with non-selected root data'
227   );
228 }
229
230
231 done_testing;