Switch most remaining debug-hooks to $dbictest_schema->is_executed_querycount()
[dbsrgits/DBIx-Class.git] / t / prefetch / via_search_related.t
CommitLineData
5fc1d686 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6
7use lib qw(t/lib);
8use DBICTest;
9
10my $schema = DBICTest->init_schema();
11
e711f316 12lives_ok ( sub {
13 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
14 {
15 'cd.year' => "2000",
16 },
17 {
18 join => 'tags',
19 order_by => 'me.trackid',
20 rows => 1,
21 }
22 );
23
24 my $use_prefetch = $no_prefetch->search(
25 {},
26 {
27 prefetch => 'tags',
28 }
29 );
30
31 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
32 is(
33 scalar ($use_prefetch->all),
34 scalar ($no_prefetch->all),
35 "Amount of returned rows is right"
36 );
37
38}, 'search_related prefetch with order_by works');
39
340926fd 40lives_ok ( sub {
41 my $no_prefetch = $schema->resultset('Track')->search_related(cd =>
42 {
43 'cd.year' => "2000",
44 'tagid' => 1,
45 },
46 {
47 join => 'tags',
48 rows => 1,
49 }
50 );
51
52 my $use_prefetch = $no_prefetch->search(
53 undef,
54 {
55 prefetch => 'tags',
56 }
57 );
58
340926fd 59 is(
60 scalar ($use_prefetch->all),
61 scalar ($no_prefetch->all),
62 "Amount of returned rows is right"
63 );
36b59369 64 is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match');
340926fd 65
66}, 'search_related prefetch with condition referencing unqualified column of a joined table works');
340926fd 67
bdb4d940 68# make sure chains off prefetched results still work
69{
70 my $cd = $schema->resultset('CD')->search({}, { prefetch => 'cd_to_producer' })->find(1);
71
49eeb48d 72 $schema->is_executed_querycount( sub {
73 is( $cd->cd_to_producer->count, 3 ,'Count of prefetched m2m links via accessor' );
74 is( scalar $cd->cd_to_producer->all, 3, 'Amount of prefetched m2m link objects via accessor' );
75 is( $cd->search_related('cd_to_producer')->count, 3, 'Count of prefetched m2m links via search_related' );
76 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Amount of prefetched m2m links via search_related' );
77 }, 0, 'No queries ran so far');
bdb4d940 78
79 is( scalar $cd->cd_to_producer->search_related('producer')->all, 3,
80 'Amount of objects via search_related off prefetched linker' );
81 is( $cd->cd_to_producer->search_related('producer')->count, 3,
82 'Count via search_related off prefetched linker' );
83 is( scalar $cd->search_related('cd_to_producer')->search_related('producer')->all, 3,
84 'Amount of objects via chained search_related off prefetched linker' );
85 is( $cd->search_related('cd_to_producer')->search_related('producer')->count, 3,
86 'Count via chained search_related off prefetched linker' );
87 is( scalar $cd->producers->all, 3,
88 'Amount of objects via m2m accessor' );
89 is( $cd->producers->count, 3,
90 'Count via m2m accessor' );
91
49eeb48d 92 $schema->is_executed_querycount( sub {
93 is( $cd->cd_to_producer->count, 3 ,'Review count of prefetched m2m links via accessor' );
94 is( scalar $cd->cd_to_producer->all, 3, 'Review amount of prefetched m2m link objects via accessor' );
95 is( $cd->search_related('cd_to_producer')->count, 3, 'Review count of prefetched m2m links via search_related' );
96 is( scalar $cd->search_related('cd_to_producer')->all, 3, 'Rreview amount of prefetched m2m links via search_related' );
97 }, 0, 'Still no queries on prefetched linker');
bdb4d940 98}
99
100# tests with distinct => 1
5fc1d686 101lives_ok (sub {
102 my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
103 ->search_related('artwork_to_artist')->search_related('artist',
104 undef,
105 { prefetch => 'cds' },
106 );
107 is($rs->all, 0, 'prefetch without WHERE (objects)');
108 is($rs->count, 0, 'prefetch without WHERE (count)');
109
110 $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
111 ->search_related('artwork_to_artist')->search_related('artist',
112 { 'cds.title' => 'foo' },
113 { prefetch => 'cds' },
114 );
115 is($rs->all, 0, 'prefetch with WHERE (objects)');
116 is($rs->count, 0, 'prefetch with WHERE (count)');
117
118
119# test where conditions at the root of the related chain
5e8cb53c 120 my $artist_rs = $schema->resultset("Artist")->search({artistid => 2});
121 my $artist = $artist_rs->next;
122 $artist->create_related ('cds', $_) for (
123 {
124 year => 1999, title => 'vague cd', genre => { name => 'vague genre' }
125 },
126 {
127 year => 1999, title => 'vague cd2', genre => { name => 'vague genre' }
128 },
129 );
5fc1d686 130
131 $rs = $artist_rs->search_related('cds')->search_related('genre',
5e8cb53c 132 { 'genre.name' => 'vague genre' },
5fc1d686 133 { prefetch => 'cds' },
134 );
5e8cb53c 135 is($rs->all, 1, 'base without distinct (objects)');
136 is($rs->count, 1, 'base without distinct (count)');
137 # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4
138 is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)');
139 is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)');
5fc1d686 140
141
d992eae8 142 $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre',
5e8cb53c 143 { 'genre.name' => 'vague genre' },
5fc1d686 144 );
d992eae8 145 is($rs->all, 2, 'distinct does not propagate over search_related (objects)');
146 is($rs->count, 2, 'distinct does not propagate over search_related (count)');
147
148 $rs = $rs->search ({}, { distinct => 1} );
5e8cb53c 149 is($rs->all, 1, 'distinct without prefetch (objects)');
150 is($rs->count, 1, 'distinct without prefetch (count)');
5fc1d686 151
152
d992eae8 153 $rs = $artist_rs->search_related('cds')->search_related('genre',
5e8cb53c 154 { 'genre.name' => 'vague genre' },
d992eae8 155 { prefetch => 'cds', distinct => 1 },
5fc1d686 156 );
5e8cb53c 157 is($rs->all, 1, 'distinct with prefetch (objects)');
158 is($rs->count, 1, 'distinct with prefetch (count)');
d992eae8 159
49eeb48d 160 local $TODO = "This makes another 2 trips to the database, it can't be right";
161 $schema->is_executed_querycount( sub {
b9df8e39 162
49eeb48d 163 # the is() calls are not todoified
164 local $TODO;
b9df8e39 165
49eeb48d 166 # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2
167 is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)');
168 is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)');
169
170 }, 0, 'No extra queries fired (prefetch survives search_related)');
b9df8e39 171
e711f316 172}, 'distinct generally works with prefetch on deep search_related chains');
5fc1d686 173
90f10b5a 174# pathological "user knows what they're doing" case
175# lifted from production somewhere
176{
177 $schema->resultset('CD')
178 ->search({ cdid => [1,2] })
179 ->search_related('tracks', { position => [3,1] })
180 ->delete_all;
181
182 my $rs = $schema->resultset('CD')->search_related('tracks', {}, {
183 group_by => 'me.title',
184 columns => { title => 'me.title', max_trk => \ 'MAX(tracks.position)' },
185 });
186
187 is_deeply(
188 $rs->all_hri,
189 [
190 { title => "Caterwaulin' Blues", max_trk => 3 },
191 { title => "Come Be Depressed With Us", max_trk => 3 },
192 { title => "Forkful of bees", max_trk => 1 },
193 { title => "Generic Manufactured Singles", max_trk => 3 },
194 { title => "Spoonful of bees", max_trk => 1 },
195 ],
196 'Expected nonsense',
197 );
198}
199
5fc1d686 200done_testing;