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 | |
e711f316 |
12 | lives_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 |
40 | lives_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 | |
5fc1d686 |
68 | lives_ok (sub { |
69 | my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) |
70 | ->search_related('artwork_to_artist')->search_related('artist', |
71 | undef, |
72 | { prefetch => 'cds' }, |
73 | ); |
74 | is($rs->all, 0, 'prefetch without WHERE (objects)'); |
75 | is($rs->count, 0, 'prefetch without WHERE (count)'); |
76 | |
77 | $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) |
78 | ->search_related('artwork_to_artist')->search_related('artist', |
79 | { 'cds.title' => 'foo' }, |
80 | { prefetch => 'cds' }, |
81 | ); |
82 | is($rs->all, 0, 'prefetch with WHERE (objects)'); |
83 | is($rs->count, 0, 'prefetch with WHERE (count)'); |
84 | |
85 | |
86 | # test where conditions at the root of the related chain |
5e8cb53c |
87 | my $artist_rs = $schema->resultset("Artist")->search({artistid => 2}); |
88 | my $artist = $artist_rs->next; |
89 | $artist->create_related ('cds', $_) for ( |
90 | { |
91 | year => 1999, title => 'vague cd', genre => { name => 'vague genre' } |
92 | }, |
93 | { |
94 | year => 1999, title => 'vague cd2', genre => { name => 'vague genre' } |
95 | }, |
96 | ); |
5fc1d686 |
97 | |
98 | $rs = $artist_rs->search_related('cds')->search_related('genre', |
5e8cb53c |
99 | { 'genre.name' => 'vague genre' }, |
5fc1d686 |
100 | { prefetch => 'cds' }, |
101 | ); |
5e8cb53c |
102 | is($rs->all, 1, 'base without distinct (objects)'); |
103 | is($rs->count, 1, 'base without distinct (count)'); |
104 | # artist -> 2 cds -> 2 genres -> 2 cds for each genre = 4 |
105 | is($rs->search_related('cds')->all, 4, 'prefetch without distinct (objects)'); |
106 | is($rs->search_related('cds')->count, 4, 'prefetch without distinct (count)'); |
5fc1d686 |
107 | |
108 | |
d992eae8 |
109 | $rs = $artist_rs->search_related('cds', {}, { distinct => 1})->search_related('genre', |
5e8cb53c |
110 | { 'genre.name' => 'vague genre' }, |
5fc1d686 |
111 | ); |
d992eae8 |
112 | is($rs->all, 2, 'distinct does not propagate over search_related (objects)'); |
113 | is($rs->count, 2, 'distinct does not propagate over search_related (count)'); |
114 | |
115 | $rs = $rs->search ({}, { distinct => 1} ); |
5e8cb53c |
116 | is($rs->all, 1, 'distinct without prefetch (objects)'); |
117 | is($rs->count, 1, 'distinct without prefetch (count)'); |
5fc1d686 |
118 | |
119 | |
d992eae8 |
120 | $rs = $artist_rs->search_related('cds')->search_related('genre', |
5e8cb53c |
121 | { 'genre.name' => 'vague genre' }, |
d992eae8 |
122 | { prefetch => 'cds', distinct => 1 }, |
5fc1d686 |
123 | ); |
5e8cb53c |
124 | is($rs->all, 1, 'distinct with prefetch (objects)'); |
125 | is($rs->count, 1, 'distinct with prefetch (count)'); |
d992eae8 |
126 | |
127 | TODO: { |
128 | local $TODO = "This makes another 2 trips to the database, it can't be right"; |
5e8cb53c |
129 | # artist -> 2 cds -> 2 genres -> 2 cds for each genre + distinct = 2 |
130 | is($rs->search_related('cds')->all, 2, 'prefetched distinct with prefetch (objects)'); |
131 | is($rs->search_related('cds')->count, 2, 'prefetched distinct with prefetch (count)'); |
d992eae8 |
132 | } |
5fc1d686 |
133 | |
e711f316 |
134 | }, 'distinct generally works with prefetch on deep search_related chains'); |
5fc1d686 |
135 | |
136 | done_testing; |