Commit | Line | Data |
cbbd7939 |
1 | use strict; |
69989ea9 |
2 | use warnings; |
cbbd7939 |
3 | |
cbbd7939 |
4 | use Test::More; |
d0a3e4e2 |
5 | |
cbbd7939 |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | use DBIC::SqlMakerTest; |
9 | |
cbbd7939 |
10 | my $schema = DBICTest->init_schema(); |
11 | my $art_rs = $schema->resultset('Artist'); |
12 | my $cdrs = $schema->resultset('CD'); |
13 | |
69989ea9 |
14 | my @tests = ( |
15 | { |
16 | rs => $cdrs, |
a17640f1 |
17 | search => \[ "title = ? AND year LIKE ?", [ title => 'buahaha' ], [ year => '20%' ] ], |
f21e372a |
18 | attrs => { rows => 5 }, |
19 | sqlbind => \[ |
0491b597 |
20 | "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE (title = ? AND year LIKE ?) LIMIT 5)", |
a17640f1 |
21 | [ title => 'buahaha' ], |
22 | [ year => '20%' ], |
f21e372a |
23 | ], |
24 | }, |
25 | |
26 | { |
27 | rs => $cdrs, |
69989ea9 |
28 | search => { |
29 | artist_id => { 'in' => $art_rs->search({}, { rows => 1 })->get_column( 'id' )->as_query }, |
30 | }, |
31 | sqlbind => \[ |
0491b597 |
32 | "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE artist_id IN ( SELECT id FROM artist me LIMIT 1 ) )", |
69989ea9 |
33 | ], |
34 | }, |
cbbd7939 |
35 | |
69989ea9 |
36 | { |
37 | rs => $art_rs, |
38 | attrs => { |
0a62f675 |
39 | 'select' => [ |
7f51e1c8 |
40 | $cdrs->search({}, { rows => 1 })->get_column('id')->as_query, |
41 | ], |
0a62f675 |
42 | }, |
69989ea9 |
43 | sqlbind => \[ |
44 | "( SELECT (SELECT id FROM cd me LIMIT 1) FROM artist me )", |
45 | ], |
46 | }, |
0a62f675 |
47 | |
69989ea9 |
48 | { |
49 | rs => $art_rs, |
50 | attrs => { |
0a62f675 |
51 | '+select' => [ |
52 | $cdrs->search({}, { rows => 1 })->get_column('id')->as_query, |
7f51e1c8 |
53 | ], |
54 | }, |
69989ea9 |
55 | sqlbind => \[ |
56 | "( SELECT me.artistid, me.name, me.rank, me.charfield, (SELECT id FROM cd me LIMIT 1) FROM artist me )", |
57 | ], |
58 | }, |
7f51e1c8 |
59 | |
69989ea9 |
60 | { |
61 | rs => $cdrs, |
62 | attrs => { |
7f51e1c8 |
63 | alias => 'cd2', |
64 | from => [ |
65 | { cd2 => $cdrs->search({ id => { '>' => 20 } })->as_query }, |
66 | ], |
67 | }, |
69989ea9 |
68 | sqlbind => \[ |
0491b597 |
69 | "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM ( |
70 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE id > ? |
71 | ) cd2 |
72 | )", |
866557f9 |
73 | [ 'id', 20 ] |
74 | ], |
69989ea9 |
75 | }, |
7f51e1c8 |
76 | |
a3c8d695 |
77 | { |
69989ea9 |
78 | rs => $art_rs, |
79 | attrs => { |
539ffe87 |
80 | from => [ |
81 | { 'me' => 'artist' }, |
82 | [ |
83 | { 'cds' => $cdrs->search({}, { 'select' => [\'me.artist as cds_artist' ]})->as_query }, |
84 | { 'me.artistid' => 'cds_artist' } |
85 | ] |
86 | ] |
69989ea9 |
87 | }, |
88 | sqlbind => \[ |
89 | "( SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me JOIN (SELECT me.artist as cds_artist FROM cd me) cds ON me.artistid = cds_artist )" |
90 | ], |
91 | }, |
a3c8d695 |
92 | |
69989ea9 |
93 | { |
94 | rs => $cdrs, |
95 | attrs => { |
d20198ab |
96 | alias => 'cd2', |
97 | from => [ |
98 | { cd2 => $cdrs->search( |
99 | { id => { '>' => 20 } }, |
100 | { |
101 | alias => 'cd3', |
102 | from => [ |
103 | { cd3 => $cdrs->search( { id => { '<' => 40 } } )->as_query } |
104 | ], |
105 | }, )->as_query }, |
106 | ], |
107 | }, |
69989ea9 |
108 | sqlbind => \[ |
109 | "( SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track |
110 | FROM |
0491b597 |
111 | (SELECT cd3.cdid, cd3.artist, cd3.title, cd3.year, cd3.genreid, cd3.single_track |
69989ea9 |
112 | FROM |
0491b597 |
113 | (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
69989ea9 |
114 | FROM cd me WHERE id < ?) cd3 |
115 | WHERE id > ?) cd2 |
116 | )", |
117 | [ 'id', 40 ], |
866557f9 |
118 | [ 'id', 20 ] |
119 | ], |
69989ea9 |
120 | }, |
d20198ab |
121 | |
69989ea9 |
122 | { |
123 | rs => $cdrs, |
124 | search => { |
125 | year => { |
126 | '=' => $cdrs->search( |
127 | { artistid => { '=' => \'me.artistid' } }, |
128 | { alias => 'inner' } |
129 | )->get_column('year')->max_rs->as_query, |
130 | }, |
4fa7bc22 |
131 | }, |
69989ea9 |
132 | sqlbind => \[ |
133 | "( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE year = (SELECT MAX(inner.year) FROM cd inner WHERE artistid = me.artistid) )", |
134 | ], |
135 | }, |
4fa7bc22 |
136 | |
69989ea9 |
137 | { |
138 | rs => $cdrs, |
139 | attrs => { |
8996a659 |
140 | alias => 'cd2', |
141 | from => [ |
142 | { cd2 => $cdrs->search({ title => 'Thriller' })->as_query }, |
143 | ], |
144 | }, |
69989ea9 |
145 | sqlbind => \[ |
0491b597 |
146 | "(SELECT cd2.cdid, cd2.artist, cd2.title, cd2.year, cd2.genreid, cd2.single_track FROM ( |
147 | SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me WHERE title = ? |
148 | ) cd2 |
149 | )", |
828ceb09 |
150 | [ 'title', |
151 | 'Thriller' |
152 | ] |
153 | ], |
69989ea9 |
154 | }, |
155 | ); |
156 | |
157 | |
69989ea9 |
158 | for my $i (0 .. $#tests) { |
159 | my $t = $tests[$i]; |
160 | for my $p (1, 2) { # repeat everything twice, make sure we do not clobber search arguments |
161 | is_same_sql_bind ( |
162 | $t->{rs}->search ($t->{search}, $t->{attrs})->as_query, |
163 | $t->{sqlbind}, |
164 | sprintf 'Testcase %d, pass %d', $i+1, $p, |
165 | ); |
166 | } |
8996a659 |
167 | } |
a17640f1 |
168 | |
169 | done_testing; |