Commit | Line | Data |
5aa25b93 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
951b7581 |
9 | plan tests => 9; |
5aa25b93 |
10 | |
376b7a93 |
11 | my $schema = DBICTest->init_schema(); |
5aa25b93 |
12 | |
376b7a93 |
13 | lives_ok(sub { |
376b7a93 |
14 | # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch) |
951b7581 |
15 | # only the requested me.name column will be fetched. |
5aa25b93 |
16 | |
376b7a93 |
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 ... |
5aa25b93 |
19 | |
376b7a93 |
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' ], |
951b7581 |
25 | select => [qw/ me.name cds.title / ], |
69ab63d4 |
26 | }, |
376b7a93 |
27 | ); |
5aa25b93 |
28 | |
376b7a93 |
29 | is ($rs->count, 2, 'Correct number of collapsed artists'); |
30 | my $we_are_goth = $rs->first; |
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'); |
951b7581 |
34 | }, 'explicit prefetch on a keyless object works'); |
35 | |
36 | |
69ab63d4 |
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 | is_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; |
56 | for my $tr ($cd->tracks->all) { |
57 | push @{$data->{tracks}}, { $tr->get_columns }; |
58 | } |
59 | push @cds_and_tracks, $data; |
60 | } |
61 | |
62 | my $pref_rs = $rs->search ({}, { columns => ['year'], 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 | push @pref_cds_and_tracks, $data; |
71 | } |
72 | |
73 | is_deeply ( |
74 | \@pref_cds_and_tracks, |
75 | \@cds_and_tracks, |
76 | 'Correct collapsing on non-unique primary object' |
77 | ); |
78 | |
79 | is_deeply ( |
80 | [ $pref_rs->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator' })->all ], |
81 | \@cds_and_tracks, |
82 | 'Correct HRI collapsing on non-unique primary object' |
83 | ); |
84 | |
85 | }, 'weird collapse lives'); |
86 | |
87 | |
951b7581 |
88 | lives_ok(sub { |
89 | # test implicit prefetch as well |
90 | |
91 | my $rs = $schema->resultset('CD')->search( |
92 | { title => 'Generic Manufactured Singles' }, |
93 | { |
94 | join=> 'artist', |
95 | select => [qw/ me.title artist.name / ], |
96 | } |
97 | ); |
98 | |
99 | my $cd = $rs->next; |
100 | is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly'); |
101 | isa_ok ($cd->artist, 'DBICTest::Artist'); |
102 | is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name'); |
5aa25b93 |
103 | |
951b7581 |
104 | }, 'implicit keyless prefetch works'); |