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