Commit | Line | Data |
5aa25b93 |
1 | use strict; |
8273e845 |
2 | use warnings; |
5aa25b93 |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
376b7a93 |
9 | my $schema = DBICTest->init_schema(); |
5aa25b93 |
10 | |
376b7a93 |
11 | lives_ok(sub { |
376b7a93 |
12 | # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch) |
4e9fc3f3 |
13 | # only the requested me.name/me.artistid columns will be fetched. |
5aa25b93 |
14 | |
376b7a93 |
15 | # reference sql with select => [...] |
908aa1bb |
16 | # SELECT me.name, cds.title, me.artistid, cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track FROM ... |
5aa25b93 |
17 | |
376b7a93 |
18 | my $rs = $schema->resultset('Artist')->search( |
19 | { 'cds.title' => { '!=', 'Generic Manufactured Singles' } }, |
20 | { |
21 | prefetch => [ qw/ cds / ], |
22 | order_by => [ { -desc => 'me.name' }, 'cds.title' ], |
908aa1bb |
23 | select => [qw/ me.name cds.title me.artistid / ], |
69ab63d4 |
24 | }, |
376b7a93 |
25 | ); |
5aa25b93 |
26 | |
376b7a93 |
27 | is ($rs->count, 2, 'Correct number of collapsed artists'); |
28 | my $we_are_goth = $rs->first; |
29 | is ($we_are_goth->name, 'We Are Goth', 'Correct first artist'); |
30 | is ($we_are_goth->cds->count, 1, 'Correct number of CDs for first artist'); |
31 | is ($we_are_goth->cds->first->title, 'Come Be Depressed With Us', 'Correct cd for artist'); |
951b7581 |
32 | }, 'explicit prefetch on a keyless object works'); |
33 | |
69ab63d4 |
34 | lives_ok ( sub { |
35 | |
36 | my $rs = $schema->resultset('CD')->search( |
37 | {}, |
38 | { |
39 | order_by => [ { -desc => 'me.year' } ], |
40 | } |
41 | ); |
42 | my $years = [qw/ 2001 2001 1999 1998 1997/]; |
43 | |
44 | is_deeply ( |
45 | [ $rs->search->get_column('me.year')->all ], |
46 | $years, |
47 | 'Expected years (at least one duplicate)', |
48 | ); |
49 | |
50 | my @cds_and_tracks; |
51 | for my $cd ($rs->all) { |
908aa1bb |
52 | my $data = { year => $cd->year, cdid => $cd->cdid }; |
69ab63d4 |
53 | for my $tr ($cd->tracks->all) { |
54 | push @{$data->{tracks}}, { $tr->get_columns }; |
55 | } |
56 | push @cds_and_tracks, $data; |
57 | } |
58 | |
908aa1bb |
59 | my $pref_rs = $rs->search ({}, { columns => [qw/year cdid/], prefetch => 'tracks' }); |
69ab63d4 |
60 | |
61 | my @pref_cds_and_tracks; |
62 | for my $cd ($pref_rs->all) { |
63 | my $data = { $cd->get_columns }; |
64 | for my $tr ($cd->tracks->all) { |
65 | push @{$data->{tracks}}, { $tr->get_columns }; |
66 | } |
67 | push @pref_cds_and_tracks, $data; |
68 | } |
69 | |
70 | is_deeply ( |
71 | \@pref_cds_and_tracks, |
72 | \@cds_and_tracks, |
73 | 'Correct collapsing on non-unique primary object' |
74 | ); |
75 | |
76 | is_deeply ( |
77 | [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ], |
78 | \@cds_and_tracks, |
79 | 'Correct HRI collapsing on non-unique primary object' |
80 | ); |
81 | |
82 | }, 'weird collapse lives'); |
83 | |
84 | |
951b7581 |
85 | lives_ok(sub { |
86 | # test implicit prefetch as well |
87 | |
88 | my $rs = $schema->resultset('CD')->search( |
89 | { title => 'Generic Manufactured Singles' }, |
90 | { |
91 | join=> 'artist', |
92 | select => [qw/ me.title artist.name / ], |
93 | } |
94 | ); |
95 | |
96 | my $cd = $rs->next; |
97 | is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly'); |
98 | isa_ok ($cd->artist, 'DBICTest::Artist'); |
99 | is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name'); |
5aa25b93 |
100 | |
951b7581 |
101 | }, 'implicit keyless prefetch works'); |
ce9f555e |
102 | |
103 | # sane error |
104 | throws_ok( |
105 | sub { |
106 | $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next; |
107 | }, |
908aa1bb |
108 | qr|\QCan't inflate prefetch into non-existent relationship 'artist' from 'Track', check the inflation specification (columns/as) ending in '...artist.name'|, |
ce9f555e |
109 | 'Sensible error message on mis-specified "as"', |
110 | ); |
111 | |
27e0370d |
112 | # check complex limiting prefetch without the join-able columns |
113 | { |
114 | my $pref_rs = $schema->resultset('Owners')->search({}, { |
115 | rows => 3, |
116 | offset => 1, |
117 | columns => 'name', # only the owner name, still prefetch all the books |
118 | prefetch => 'books', |
119 | }); |
120 | |
121 | lives_ok { |
122 | is ($pref_rs->all, 1, 'Expected count of objects on limtied prefetch') |
123 | } "Complex limited prefetch works with non-selected join condition"; |
124 | } |
125 | |
126 | |
ce9f555e |
127 | done_testing; |