Commit | Line | Data |
902133a3 |
1 | use strict; |
2 | use Test::More; |
3 | |
4 | BEGIN { |
68de9438 |
5 | eval "use DBIx::Class::CDBICompat; require Class::DBI::Plugin::DeepAbstractSearch;"; |
95cbe02e |
6 | if ($@) { |
68de9438 |
7 | plan (skip_all => "Class::DBI::Plugin::DeepAbstractSearch, Class::Trigger and DBIx::ContextualFetch required: $@"); |
95cbe02e |
8 | } |
d526af80 |
9 | plan tests => 19; |
902133a3 |
10 | } |
11 | |
50891152 |
12 | my $DB = "t/var/cdbi_testdb"; |
902133a3 |
13 | unlink $DB if -e $DB; |
14 | |
15 | my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 0 }); |
16 | |
17 | package Music::DBI; |
18 | use base qw(DBIx::Class::CDBICompat); |
19 | use Class::DBI::Plugin::DeepAbstractSearch; |
20 | __PACKAGE__->connection(@DSN); |
21 | |
22 | my $sql = <<'SQL_END'; |
23 | |
24 | --------------------------------------- |
25 | -- Artists |
26 | --------------------------------------- |
27 | CREATE TABLE artists ( |
28 | id INTEGER NOT NULL PRIMARY KEY, |
29 | name VARCHAR(32) |
30 | ); |
31 | |
32 | INSERT INTO artists VALUES (1, "Willie Nelson"); |
33 | INSERT INTO artists VALUES (2, "Patsy Cline"); |
34 | |
35 | --------------------------------------- |
36 | -- Labels |
37 | --------------------------------------- |
38 | CREATE TABLE labels ( |
39 | id INTEGER NOT NULL PRIMARY KEY, |
40 | name VARCHAR(32) |
41 | ); |
42 | |
43 | INSERT INTO labels VALUES (1, "Columbia"); |
44 | INSERT INTO labels VALUES (2, "Sony"); |
45 | INSERT INTO labels VALUES (3, "Supraphon"); |
46 | |
47 | --------------------------------------- |
48 | -- CDs |
49 | --------------------------------------- |
50 | CREATE TABLE cds ( |
51 | id INTEGER NOT NULL PRIMARY KEY, |
52 | label INTEGER, |
53 | artist INTEGER, |
54 | title VARCHAR(32), |
55 | year INTEGER |
56 | ); |
57 | INSERT INTO cds VALUES (1, 1, 1, "Songs", 2005); |
58 | INSERT INTO cds VALUES (2, 2, 1, "Read Headed Stanger", 2000); |
59 | INSERT INTO cds VALUES (3, 1, 1, "Wanted! The Outlaws", 2004); |
60 | INSERT INTO cds VALUES (4, 2, 1, "The Very Best of Willie Nelson", 1999); |
61 | |
62 | INSERT INTO cds VALUES (5, 1, 2, "12 Greates Hits", 1999); |
63 | INSERT INTO cds VALUES (6, 2, 2, "Sweet Dreams", 1995); |
64 | INSERT INTO cds VALUES (7, 3, 2, "The Best of Patsy Cline", 1991); |
65 | |
66 | --------------------------------------- |
67 | -- Tracks |
68 | --------------------------------------- |
69 | CREATE TABLE tracks ( |
70 | id INTEGER NOT NULL PRIMARY KEY, |
71 | cd INTEGER, |
72 | position INTEGER, |
73 | title VARCHAR(32) |
74 | ); |
75 | INSERT INTO tracks VALUES (1, 1, 1, "Songs: Track 1"); |
76 | INSERT INTO tracks VALUES (2, 1, 2, "Songs: Track 2"); |
77 | INSERT INTO tracks VALUES (3, 1, 3, "Songs: Track 3"); |
78 | INSERT INTO tracks VALUES (4, 1, 4, "Songs: Track 4"); |
79 | |
80 | INSERT INTO tracks VALUES (5, 2, 1, "Read Headed Stanger: Track 1"); |
81 | INSERT INTO tracks VALUES (6, 2, 2, "Read Headed Stanger: Track 2"); |
82 | INSERT INTO tracks VALUES (7, 2, 3, "Read Headed Stanger: Track 3"); |
83 | INSERT INTO tracks VALUES (8, 2, 4, "Read Headed Stanger: Track 4"); |
84 | |
85 | INSERT INTO tracks VALUES (9, 3, 1, "Wanted! The Outlaws: Track 1"); |
86 | INSERT INTO tracks VALUES (10, 3, 2, "Wanted! The Outlaws: Track 2"); |
87 | |
88 | INSERT INTO tracks VALUES (11, 4, 1, "The Very Best of Willie Nelson: Track 1"); |
89 | INSERT INTO tracks VALUES (12, 4, 2, "The Very Best of Willie Nelson: Track 2"); |
90 | INSERT INTO tracks VALUES (13, 4, 3, "The Very Best of Willie Nelson: Track 3"); |
91 | INSERT INTO tracks VALUES (14, 4, 4, "The Very Best of Willie Nelson: Track 4"); |
92 | INSERT INTO tracks VALUES (15, 4, 5, "The Very Best of Willie Nelson: Track 5"); |
93 | INSERT INTO tracks VALUES (16, 4, 6, "The Very Best of Willie Nelson: Track 6"); |
94 | |
95 | INSERT INTO tracks VALUES (17, 5, 1, "12 Greates Hits: Track 1"); |
96 | INSERT INTO tracks VALUES (18, 5, 2, "12 Greates Hits: Track 2"); |
97 | INSERT INTO tracks VALUES (19, 5, 3, "12 Greates Hits: Track 3"); |
98 | INSERT INTO tracks VALUES (20, 5, 4, "12 Greates Hits: Track 4"); |
99 | |
100 | INSERT INTO tracks VALUES (21, 6, 1, "Sweet Dreams: Track 1"); |
101 | INSERT INTO tracks VALUES (22, 6, 2, "Sweet Dreams: Track 2"); |
102 | INSERT INTO tracks VALUES (23, 6, 3, "Sweet Dreams: Track 3"); |
103 | INSERT INTO tracks VALUES (24, 6, 4, "Sweet Dreams: Track 4"); |
104 | |
105 | INSERT INTO tracks VALUES (25, 7, 1, "The Best of Patsy Cline: Track 1"); |
106 | INSERT INTO tracks VALUES (26, 7, 2, "The Best of Patsy Cline: Track 2"); |
107 | |
108 | SQL_END |
109 | |
110 | foreach my $statement (split /;/, $sql) { |
111 | $statement =~ s/^\s*//gs; |
112 | $statement =~ s/\s*$//gs; |
113 | next unless $statement; |
114 | Music::DBI->db_Main->do($statement) or die "$@ $!"; |
115 | } |
116 | |
117 | Music::DBI->dbi_commit; |
118 | |
119 | package Music::Artist; |
120 | use base 'Music::DBI'; |
121 | Music::Artist->table('artists'); |
122 | Music::Artist->columns(All => qw/id name/); |
123 | |
124 | |
125 | package Music::Label; |
126 | use base 'Music::DBI'; |
127 | Music::Label->table('labels'); |
128 | Music::Label->columns(All => qw/id name/); |
129 | |
130 | package Music::CD; |
131 | use base 'Music::DBI'; |
132 | Music::CD->table('cds'); |
133 | Music::CD->columns(All => qw/id label artist title year/); |
134 | |
135 | |
136 | package Music::Track; |
137 | use base 'Music::DBI'; |
138 | Music::Track->table('tracks'); |
139 | Music::Track->columns(All => qw/id cd position title/); |
140 | |
141 | Music::Artist->has_many(cds => 'Music::CD'); |
142 | Music::Label->has_many(cds => 'Music::CD'); |
143 | Music::CD->has_many(tracks => 'Music::Track'); |
144 | Music::CD->has_a(artist => 'Music::Artist'); |
145 | Music::CD->has_a(label => 'Music::Label'); |
146 | Music::Track->has_a(cd => 'Music::CD'); |
147 | |
148 | package main; |
149 | |
150 | { |
151 | my $where = { }; |
152 | my $attr; |
153 | my @artists = Music::Artist->deep_search_where($where, $attr); |
154 | is_deeply [ sort @artists ], [ 1, 2 ], "all without order"; |
155 | } |
156 | |
157 | { |
158 | my $where = { }; |
159 | my $attr = { order_by => 'name' }; |
160 | my @artists = Music::Artist->deep_search_where($where, $attr); |
161 | is_deeply \@artists, [ 2, 1 ], "all with ORDER BY name"; |
162 | } |
163 | |
164 | { |
165 | my $where = { }; |
166 | my $attr = { order_by => 'name DESC' }; |
167 | my @artists = Music::Artist->deep_search_where($where, $attr); |
168 | is_deeply \@artists, [ 1, 2 ], "all with ORDER BY name DESC"; |
169 | } |
170 | |
171 | { |
172 | my $where = { name => { -like => 'Patsy Cline' }, }; |
173 | my $attr; |
174 | my @artists = Music::Artist->deep_search_where($where, $attr); |
175 | is_deeply \@artists, [ 2 ], "simple search"; |
176 | } |
177 | |
178 | { |
179 | my $where = { 'artist.name' => 'Patsy Cline' }; |
180 | my $attr = { } ; |
181 | my @cds = Music::CD->deep_search_where($where, $attr); |
182 | is_deeply [ sort @cds ], [ 5, 6, 7 ], "Patsy's CDs"; |
183 | } |
184 | |
185 | { |
186 | my $where = { 'artist.name' => 'Patsy Cline' }; |
187 | my $attr = { order_by => "title" } ; |
188 | my @cds = Music::CD->deep_search_where($where, $attr); |
189 | is_deeply [ @cds ], [ 5, 6, 7 ], "Patsy's CDs by title"; |
190 | |
191 | my $count = Music::CD->count_deep_search_where($where); |
192 | is_deeply $count, 3, "count Patsy's CDs by title"; |
193 | } |
194 | |
195 | { |
196 | my $where = { 'cd.title' => { -like => 'S%' }, }; |
197 | my $attr = { order_by => "cd.title, title" } ; |
198 | my @cds = Music::Track->deep_search_where($where, $attr); |
199 | is_deeply [ @cds ], [1, 2, 3, 4, 21, 22, 23, 24 ], "Tracks from CDs whose name starts with 'S'"; |
200 | } |
201 | |
202 | { |
203 | my $where = { |
204 | 'cd.artist.name' => { -like => 'W%' }, |
205 | 'cd.year' => { '>' => 2000 }, |
206 | 'position' => { '<' => 3 } |
207 | }; |
208 | my $attr = { order_by => "cd.title DESC, title" } ; |
209 | my @cds = Music::Track->deep_search_where($where, $attr); |
210 | is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000 "; |
211 | |
212 | my $count = Music::Track->count_deep_search_where($where); |
213 | is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000"; |
214 | } |
215 | |
216 | { |
217 | my $where = { |
218 | 'cd.artist.name' => { -like => 'W%' }, |
219 | 'cd.year' => { '>' => 2000 }, |
220 | 'position' => { '<' => 3 } |
221 | }; |
222 | my $attr = { order_by => [ 'cd.title DESC' , 'title' ] } ; |
223 | my @cds = Music::Track->deep_search_where($where, $attr); |
224 | is_deeply [ @cds ], [ 9, 10, 1, 2 ], "First 2 tracks from W's albums after 2000, array ref order "; |
225 | |
226 | my $count = Music::Track->count_deep_search_where($where); |
227 | is_deeply $count, 4, "Count First 2 tracks from W's albums after 2000, array ref order"; |
228 | } |
229 | |
230 | { |
231 | my $where = { 'cd.title' => [ -and => { -like => '%o%' }, { -like => '%W%' } ] }; |
232 | my $attr = { order_by => [ 'cd.id' ] } ; |
233 | |
234 | my @tracks = Music::Track->deep_search_where($where, $attr); |
235 | is_deeply [ @tracks ], [ 3, 3, 4, 4, 4, 4, 4, 4 ], "Tracks from CD titles containing 'o' AND 'W'"; |
236 | } |
237 | |
238 | { |
239 | my $where = { 'cd.year' => [ 1995, 1999 ] }; |
240 | my $attr = { order_by => [ 'cd.id' ] } ; |
241 | |
242 | my @tracks = Music::Track->deep_search_where($where, $attr); |
243 | is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ], |
244 | "Tracks from CDs from 1995, 1999"; |
245 | } |
246 | |
247 | { |
248 | my $where = { 'cd.year' => { -in => [ 1995, 1999 ] } }; |
249 | my $attr = { order_by => [ 'cd.id' ] } ; |
250 | |
251 | my @tracks = Music::Track->deep_search_where($where, $attr); |
252 | is_deeply [ @tracks ], [ 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6 ], |
253 | "Tracks from CDs in 1995, 1999"; |
254 | } |
255 | |
256 | { |
257 | my $where = { -and => [ 'cd.year' => [ 1995, 1999 ], position => { '<=', 2 } ] }; |
258 | my $attr = { order_by => [ 'cd.id' ] } ; |
259 | |
260 | my @tracks = Music::Track->deep_search_where($where, $attr); |
261 | is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ], |
262 | "First 2 tracks Tracks from CDs from 1995, 1999"; |
263 | } |
264 | |
265 | { |
266 | my $where = { -and => [ 'cd.year' => { -in => [ 1995, 1999 ] }, position => { '<=', 2 } ] }; |
267 | my $attr = { order_by => [ 'cd.id' ] } ; |
268 | |
269 | my @tracks = Music::Track->deep_search_where($where, $attr); |
270 | is_deeply [ @tracks ], [ 4, 4, 5, 5, 6, 6 ], |
271 | "First 2 tracks Tracks from CDs in 1995, 1999"; |
272 | } |
273 | |
274 | { |
275 | my $where = { 'label.name' => { -in => [ 'Sony', 'Supraphon', 'Bogus' ] } }; |
276 | my $attr = { order_by => [ 'id' ] } ; |
277 | |
278 | my @cds = Music::CD->deep_search_where($where, $attr); |
279 | is_deeply [ @cds ], [ 2, 4, 6, 7 ], |
280 | "CDs from Sony or Supraphon"; |
281 | } |
282 | |
283 | { |
284 | my $where = { 'label.name' => [ 'Sony', 'Supraphon', 'Bogus' ] }; |
285 | my $attr = { order_by => [ 'id' ] } ; |
286 | |
287 | my @cds = Music::CD->deep_search_where($where, $attr); |
288 | is_deeply [ @cds ], [ 2, 4, 6, 7 ], |
289 | "CDs from Sony or Supraphon"; |
290 | } |
291 | |
292 | END { unlink $DB if -e $DB } |
293 | |