Back out constructor/prefetch rewrite introduced mainly by 43245ada4a
[dbsrgits/DBIx-Class.git] / t / prefetch / incomplete.t
CommitLineData
5aa25b93 1use strict;
8273e845 2use warnings;
5aa25b93 3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
376b7a93 9my $schema = DBICTest->init_schema();
5aa25b93 10
376b7a93 11lives_ok(sub {
376b7a93 12 # while cds.* will be selected anyway (prefetch currently forces the result of _resolve_prefetch)
c9733800 13 # only the requested me.name column will be fetched.
5aa25b93 14
376b7a93 15 # reference sql with select => [...]
c9733800 16 # SELECT me.name, cds.title, 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' ],
c9733800 23 select => [qw/ me.name cds.title / ],
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
951b7581 35lives_ok(sub {
36 # test implicit prefetch as well
37
38 my $rs = $schema->resultset('CD')->search(
39 { title => 'Generic Manufactured Singles' },
40 {
41 join=> 'artist',
42 select => [qw/ me.title artist.name / ],
43 }
44 );
45
46 my $cd = $rs->next;
47 is ($cd->title, 'Generic Manufactured Singles', 'CD title prefetched correctly');
48 isa_ok ($cd->artist, 'DBICTest::Artist');
49 is ($cd->artist->name, 'Random Boy Band', 'Artist object has correct name');
5aa25b93 50
951b7581 51}, 'implicit keyless prefetch works');
ce9f555e 52
53# sane error
54throws_ok(
55 sub {
56 $schema->resultset('Track')->search({}, { join => { cd => 'artist' }, '+columns' => 'artist.name' } )->next;
57 },
c9733800 58 qr|\QCan't inflate manual prefetch into non-existent relationship 'artist' from 'Track', check the inflation specification (columns/as) ending in 'artist.name'|,
ce9f555e 59 'Sensible error message on mis-specified "as"',
60);
61
27e0370d 62# check complex limiting prefetch without the join-able columns
63{
64 my $pref_rs = $schema->resultset('Owners')->search({}, {
65 rows => 3,
66 offset => 1,
67 columns => 'name', # only the owner name, still prefetch all the books
68 prefetch => 'books',
69 });
70
71 lives_ok {
72 is ($pref_rs->all, 1, 'Expected count of objects on limtied prefetch')
73 } "Complex limited prefetch works with non-selected join condition";
74}
75
76
ce9f555e 77done_testing;