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