Commit | Line | Data |
5fc1d686 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
9 | |
10 | my $schema = DBICTest->init_schema(); |
11 | |
b9df8e39 |
12 | my $queries; |
13 | my $debugcb = sub { $queries++; }; |
14 | my $orig_debug = $schema->storage->debug; |
15 | |
e711f316 |
16 | lives_ok ( sub { |
17 | my $no_prefetch = $schema->resultset('Track')->search_related(cd => |
18 | { |
19 | 'cd.year' => "2000", |
20 | }, |
21 | { |
22 | join => 'tags', |
23 | order_by => 'me.trackid', |
24 | rows => 1, |
25 | } |
26 | ); |
27 | |
28 | my $use_prefetch = $no_prefetch->search( |
29 | {}, |
30 | { |
31 | prefetch => 'tags', |
32 | } |
33 | ); |
34 | |
35 | is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match'); |
36 | is( |
37 | scalar ($use_prefetch->all), |
38 | scalar ($no_prefetch->all), |
39 | "Amount of returned rows is right" |
40 | ); |
41 | |
42 | }, 'search_related prefetch with order_by works'); |
43 | |
340926fd |
44 | lives_ok ( sub { |
45 | my $no_prefetch = $schema->resultset('Track')->search_related(cd => |
46 | { |
47 | 'cd.year' => "2000", |
48 | 'tagid' => 1, |
49 | }, |
50 | { |
51 | join => 'tags', |
52 | rows => 1, |
53 | } |
54 | ); |
55 | |
56 | my $use_prefetch = $no_prefetch->search( |
57 | undef, |
58 | { |
59 | prefetch => 'tags', |
60 | } |
61 | ); |
62 | |
340926fd |
63 | is( |
64 | scalar ($use_prefetch->all), |
65 | scalar ($no_prefetch->all), |
66 | "Amount of returned rows is right" |
67 | ); |
36b59369 |
68 | is($use_prefetch->count, $no_prefetch->count, 'counts with and without prefetch match'); |
340926fd |
69 | |
70 | }, 'search_related prefetch with condition referencing unqualified column of a joined table works'); |
340926fd |
71 | |
5fc1d686 |
72 | lives_ok (sub { |
73 | my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) |
74 | ->search_related('artwork_to_artist')->search_related('artist', |
75 | undef, |
76 | { prefetch => 'cds' }, |
77 | ); |
78 | is($rs->all, 0, 'prefetch without WHERE (objects)'); |
79 | is($rs->count, 0, 'prefetch without WHERE (count)'); |
80 | |
81 | $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) |
82 | ->search_related('artwork_to_artist')->search_related('artist', |
83 | { 'cds.title' => 'foo' }, |
84 | { prefetch => 'cds' }, |
85 | ); |
86 | is($rs->all, 0, 'prefetch with WHERE (objects)'); |
87 | is($rs->count, 0, 'prefetch with WHERE (count)'); |
88 | |
89 | |
90 | # test where conditions at the root of the related chain |
5e8cb53c |
91 | my $artist_rs = $schema->resultset("Artist")->search({artistid => 2}); |
92 | my $artist = $artist_rs->next; |
93 | $artist->create_related ('cds', $_) for ( |
94 | { |
95 | year => 1999, title => 'vague cd', genre => { name => 'vague genre' } |
96 | }, |
97 | { |
98 | year => 1999, title => 'vague cd2', genre => { name => 'vague genre' } |
99 | }, |
100 | ); |
5fc1d686 |
101 | |
102 | $rs = $artist_rs->search_related('cds')->search_related('genre', |
5e8cb53c |
103 | { 'genre.name' => 'vague genre' }, |
5fc1d686 |
104 | { prefetch => 'cds' }, |
105 | ); |
5e8cb53c |
106 | is($rs->all, 1, 'base without distinct (objects)'); |
107 | is($rs->count, 1, 'base without distinct (count)'); |
108 | # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4 |
109 | is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)'); |
110 | is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)'); |
5fc1d686 |
111 | |
112 | |
d992eae8 |
113 | $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre', |
5e8cb53c |
114 | { 'genre.name' => 'vague genre' }, |
5fc1d686 |
115 | ); |
d992eae8 |
116 | is($rs->all, 2, 'distinct does not propagate over search_related (objects)'); |
117 | is($rs->count, 2, 'distinct does not propagate over search_related (count)'); |
118 | |
119 | $rs = $rs->search ({}, { distinct => 1} ); |
5e8cb53c |
120 | is($rs->all, 1, 'distinct without prefetch (objects)'); |
121 | is($rs->count, 1, 'distinct without prefetch (count)'); |
5fc1d686 |
122 | |
123 | |
d992eae8 |
124 | $rs = $artist_rs->search_related('cds')->search_related('genre', |
5e8cb53c |
125 | { 'genre.name' => 'vague genre' }, |
d992eae8 |
126 | { prefetch => 'cds', distinct => 1 }, |
5fc1d686 |
127 | ); |
5e8cb53c |
128 | is($rs->all, 1, 'distinct with prefetch (objects)'); |
129 | is($rs->count, 1, 'distinct with prefetch (count)'); |
d992eae8 |
130 | |
131 | TODO: { |
b9df8e39 |
132 | $queries = 0; |
133 | $schema->storage->debugcb ($debugcb); |
134 | $schema->storage->debug (1); |
135 | |
5e8cb53c |
136 | # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2 |
137 | is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)'); |
138 | is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)'); |
b9df8e39 |
139 | |
9fd5c112 |
140 | local $TODO = "This makes another 2 trips to the database, it can't be right"; |
b9df8e39 |
141 | is ($queries, 0, 'No extra queries fired (prefetch survives search_related)'); |
142 | |
143 | $schema->storage->debugcb (undef); |
144 | $schema->storage->debug ($orig_debug); |
d992eae8 |
145 | } |
5fc1d686 |
146 | |
e711f316 |
147 | }, 'distinct generally works with prefetch on deep search_related chains'); |
5fc1d686 |
148 | |
149 | done_testing; |