Commit | Line | Data |
639cf8f9 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use lib qw(t/lib); |
5 | |
6 | use Test::More; |
7 | use DBICTest; |
8 | use DBIC::SqlMakerTest; |
639cf8f9 |
9 | |
f785d72e |
10 | plan tests => 14; |
639cf8f9 |
11 | |
12 | my $schema = DBICTest->init_schema(); |
13 | |
14 | # collapsing prefetch |
15 | { |
16 | my $rs = $schema->resultset("Artist") |
17 | ->search_related('cds', |
18 | { 'tracks.position' => [1,2] }, |
19 | { prefetch => [qw/tracks artist/] }, |
20 | ); |
21 | is ($rs->all, 5, 'Correct number of objects'); |
639cf8f9 |
22 | is ($rs->count, 5, 'Correct count'); |
23 | |
24 | is_same_sql_bind ( |
f785d72e |
25 | $rs->count_rs->as_query, |
26 | '( |
27 | SELECT COUNT( * ) |
28 | FROM ( |
29 | SELECT cds.cdid |
30 | FROM artist me |
31 | JOIN cd cds ON cds.artist = me.artistid |
32 | LEFT JOIN track tracks ON tracks.cd = cds.cdid |
33 | JOIN artist artist ON artist.artistid = cds.artist |
34 | WHERE tracks.position = ? OR tracks.position = ? |
35 | GROUP BY cds.cdid |
36 | ) count_subq |
37 | )', |
38 | [ map { [ 'tracks.position' => $_ ] } (1, 2) ], |
cfcc66e9 |
39 | ); |
40 | } |
41 | |
6a05068a |
42 | # collapsing prefetch with distinct |
f785d72e |
43 | { |
44 | my $first_cd = $schema->resultset('Artist')->first->cds->first; |
45 | $first_cd->update ({ |
46 | genreid => $first_cd->create_related ( |
47 | genre => ({ name => 'vague genre' }) |
48 | )->id |
49 | }); |
50 | |
6a05068a |
51 | my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1}) |
52 | ->search_related('cds')->search_related('genre', |
f785d72e |
53 | { 'genre.name' => { '!=', 'foo' } }, |
6a05068a |
54 | { prefetch => q(cds) }, |
55 | ); |
f785d72e |
56 | is ($rs->all, 1, 'Correct number of objects'); |
57 | is ($rs->count, 1, 'Correct count'); |
6a05068a |
58 | |
59 | is_same_sql_bind ( |
f785d72e |
60 | $rs->count_rs->as_query, |
61 | '( |
62 | SELECT COUNT( * ) |
63 | FROM ( |
64 | SELECT genre.genreid |
65 | FROM artist me |
66 | JOIN cd cds ON cds.artist = me.artistid |
67 | JOIN genre genre ON genre.genreid = cds.genreid |
68 | LEFT JOIN cd cds_2 ON cds_2.genreid = genre.genreid |
69 | WHERE ( genre.name != ? ) |
70 | GROUP BY genre.genreid |
71 | ) count_subq |
72 | )', |
73 | [ [ 'genre.name' => 'foo' ] ], |
639cf8f9 |
74 | ); |
75 | } |
76 | |
77 | # non-collapsing prefetch (no multi prefetches) |
78 | { |
79 | my $rs = $schema->resultset("CD") |
80 | ->search_related('tracks', |
81 | { position => [1,2] }, |
82 | { prefetch => [qw/disc lyrics/] }, |
83 | ); |
84 | is ($rs->all, 10, 'Correct number of objects'); |
85 | |
86 | |
639cf8f9 |
87 | is ($rs->count, 10, 'Correct count'); |
88 | |
89 | is_same_sql_bind ( |
f785d72e |
90 | $rs->count_rs->as_query, |
91 | '( |
92 | SELECT COUNT( * ) |
93 | FROM cd me |
94 | JOIN track tracks ON tracks.cd = me.cdid |
95 | JOIN cd disc ON disc.cdid = tracks.cd |
96 | LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid |
97 | WHERE position = ? OR position = ? |
98 | )', |
99 | [ map { [ position => $_ ] } (1, 2) ], |
639cf8f9 |
100 | ); |
101 | } |
cfcc66e9 |
102 | |
f785d72e |
103 | { |
cfcc66e9 |
104 | my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) |
105 | ->search_related('artwork_to_artist')->search_related('artist', |
106 | undef, |
107 | { prefetch => q(cds) }, |
108 | ); |
109 | is($rs->all, 0, 'failure without WHERE'); |
110 | |
111 | $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1}) |
112 | ->search_related('artwork_to_artist')->search_related('artist', |
113 | { 'cds.title' => 'foo' }, # this line has changed |
114 | { prefetch => q(cds) }, |
115 | ); |
116 | is($rs->all, 0, 'success with WHERE'); |
f785d72e |
117 | |
118 | |
cfcc66e9 |
119 | # different case |
cfcc66e9 |
120 | $rs = $schema->resultset("Artist")->search(undef)#, {distinct => 1}) |
121 | ->search_related('cds')->search_related('genre', |
122 | { 'genre.name' => 'foo' }, |
123 | { prefetch => q(cds) }, |
124 | ); |
125 | is($rs->all, 0, 'success without distinct'); |
f785d72e |
126 | |
cfcc66e9 |
127 | $rs = $schema->resultset("Artist")->search(undef, {distinct => 1}) |
128 | ->search_related('cds')->search_related('genre', |
129 | { 'genre.name' => 'foo' }, |
130 | #{ prefetch => q(cds) }, |
131 | ); |
132 | is($rs->all, 0, 'success without prefetch'); |
133 | |
134 | $rs = $schema->resultset("Artist")->search(undef, {distinct => 1}) |
135 | ->search_related('cds')->search_related('genre', |
136 | { 'genre.name' => 'foo' }, |
137 | { prefetch => q(cds) }, |
138 | ); |
139 | is($rs->all, 0, 'failure with distinct'); |
140 | } |