Extend test
[dbsrgits/DBIx-Class.git] / t / prefetch / incomplete.t
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
9 plan tests => 9;
10
11 my $schema = DBICTest->init_schema();
12
13 lives_ok(sub {
14   # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch)
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->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');
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');
52
53 }, 'implicit keyless prefetch works');