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