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 / ], |
376b7a93 |
26 | } |
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 | |
37 | lives_ok(sub { |
38 | # test implicit prefetch as well |
39 | |
40 | my $rs = $schema->resultset('CD')->search( |
41 | { title => 'Generic Manufactured Singles' }, |
42 | { |
43 | join=> 'artist', |
44 | select => [qw/ me.title artist.name / ], |
45 | } |
46 | ); |
47 | |
48 | my $cd = $rs->next; |
49 | is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly'); |
50 | isa_ok ($cd->artist, 'DBICTest::Artist'); |
51 | is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name'); |
5aa25b93 |
52 | |
951b7581 |
53 | }, 'implicit keyless prefetch works'); |