a710fbbb9eb860d1c8ac4014937431eb69783b55
[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     push @cds_and_tracks, $data;
59   }
60
61   my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' });
62
63   my @pref_cds_and_tracks;
64   for my $cd ($pref_rs->all) {
65     my $data = { $cd->get_columns };
66     for my $tr ($cd->tracks->all) {
67       push @{$data->{tracks}}, { $tr->get_columns };
68     }
69     push @pref_cds_and_tracks, $data;
70   }
71
72   cmp_deeply (
73     \@pref_cds_and_tracks,
74     \@cds_and_tracks,
75     'Correct collapsing on non-unique primary object'
76   );
77
78   cmp_deeply (
79     [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ],
80     \@cds_and_tracks,
81     'Correct HRI collapsing on non-unique primary object'
82   );
83
84 }, 'weird collapse lives');
85
86
87 lives_ok(sub {
88   # test implicit prefetch as well
89
90   my $rs = $schema->resultset('CD')->search(
91     { title => 'Generic Manufactured Singles' },
92     {
93       join=> 'artist',
94       select => [qw/ me.title artist.name / ],
95     }
96   );
97
98   my $cd = $rs->next;
99   is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
100   isa_ok ($cd->artist, 'DBICTest::Artist');
101   is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
102
103 }, 'implicit keyless prefetch works');
104
105 # sane error
106 throws_ok(
107   sub {
108     $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
109   },
110   qr|\QInflation into non-existent relationship 'artist' of 'Track' requested, check the inflation specification (columns/as) ending in '...artist.name'|,
111   'Sensible error message on mis-specified "as"',
112 );
113
114 # check complex limiting prefetch without the join-able columns
115 {
116   my $pref_rs = $schema->resultset('Owners')->search({}, {
117     rows => 3,
118     offset => 1,
119     columns => 'name',  # only the owner name, still prefetch all the books
120     prefetch => 'books',
121   });
122
123   is_same_sql_bind(
124     $pref_rs->as_query,
125     '(
126       SELECT me.name, books.id, books.source, books.owner, books.title, books.price
127         FROM (
128           SELECT me.name, me.id
129             FROM owners me
130           LIMIT ?
131           OFFSET ?
132         ) me
133         LEFT JOIN books books
134           ON books.owner = me.id
135     )',
136     [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
137     'Expected SQL on complex limited prefetch with non-selected join condition',
138   );
139
140   is_deeply (
141     $pref_rs->all_hri,
142     [ {
143       name => "Waltham",
144       books => [ {
145         id => 3,
146         owner => 2,
147         price => 65,
148         source => "Library",
149         title => "Best Recipe Cookbook",
150       } ],
151     } ],
152     'Expected result on complex limited prefetch with non-selected join condition'
153   );
154
155   my $empty_ordered_pref_rs = $pref_rs->search({}, {
156     columns => [],  # nothing, we only prefetch the book data
157     order_by => 'me.name',
158   });
159   my $empty_ordered_pref_hri = [ {
160     books => [ {
161       id => 3,
162       owner => 2,
163       price => 65,
164       source => "Library",
165       title => "Best Recipe Cookbook",
166     } ],
167   } ];
168
169   is_same_sql_bind(
170     $empty_ordered_pref_rs->as_query,
171     '(
172       SELECT books.id, books.source, books.owner, books.title, books.price
173         FROM (
174           SELECT me.id, me.name
175             FROM owners me
176           ORDER BY me.name
177           LIMIT ?
178           OFFSET ?
179         ) me
180         LEFT JOIN books books
181           ON books.owner = me.id
182       ORDER BY me.name
183     )',
184     [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
185     'Expected SQL on *ordered* complex limited prefetch with non-selected root data',
186   );
187
188   is_deeply (
189     $empty_ordered_pref_rs->all_hri,
190     $empty_ordered_pref_hri,
191     'Expected result on *ordered* complex limited prefetch with non-selected root data'
192   );
193
194   $empty_ordered_pref_rs = $empty_ordered_pref_rs->search({}, {
195     order_by => [ \ 'LENGTH(me.name)', \ 'RANDOM()' ],
196   });
197
198   is_same_sql_bind(
199     $empty_ordered_pref_rs->as_query,
200     '(
201       SELECT books.id, books.source, books.owner, books.title, books.price
202         FROM (
203           SELECT me.id, me.name
204             FROM owners me
205           ORDER BY LENGTH(me.name), RANDOM()
206           LIMIT ?
207           OFFSET ?
208         ) me
209         LEFT JOIN books books
210           ON books.owner = me.id
211       ORDER BY LENGTH(me.name), RANDOM()
212     )',
213     [ [ { sqlt_datatype => "integer" } => 3 ], [ { sqlt_datatype => "integer" } => 1 ] ],
214     'Expected SQL on *function-ordered* complex limited prefetch with non-selected root data',
215   );
216
217   is_deeply (
218     $empty_ordered_pref_rs->all_hri,
219     $empty_ordered_pref_hri,
220     'Expected result on *function-ordered* complex limited prefetch with non-selected root data'
221   );
222 }
223
224
225 done_testing;