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