Switch CDBICompat and its tests to OptDeps
[dbsrgits/DBIx-Class.git] / t / cdbi / DeepAbstractSearch / 01_search.t
CommitLineData
83eef562 1use DBIx::Class::Optional::Dependencies -skip_all_without => qw( cdbicompat Class::DBI::Plugin::DeepAbstractSearch>=0 );
2
902133a3 3use strict;
4a233f30 4use warnings;
902133a3 5
83eef562 6use Test::More;
d9bd5195 7
83eef562 8use lib 't/lib';
9use DBICTest;
902133a3 10
d9bd5195 11my $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 17my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 0 });
18
19package Music::DBI;
20use base qw(DBIx::Class::CDBICompat);
21use Class::DBI::Plugin::DeepAbstractSearch;
22__PACKAGE__->connection(@DSN);
23
24my $sql = <<'SQL_END';
25
26---------------------------------------
27-- Artists
28---------------------------------------
29CREATE TABLE artists (
30 id INTEGER NOT NULL PRIMARY KEY,
31 name VARCHAR(32)
32);
33
34INSERT INTO artists VALUES (1, "Willie Nelson");
35INSERT INTO artists VALUES (2, "Patsy Cline");
36
37---------------------------------------
38-- Labels
39---------------------------------------
40CREATE TABLE labels (
41 id INTEGER NOT NULL PRIMARY KEY,
42 name VARCHAR(32)
43);
44
45INSERT INTO labels VALUES (1, "Columbia");
46INSERT INTO labels VALUES (2, "Sony");
47INSERT INTO labels VALUES (3, "Supraphon");
48
49---------------------------------------
50-- CDs
51---------------------------------------
52CREATE TABLE cds (
53 id INTEGER NOT NULL PRIMARY KEY,
54 label INTEGER,
55 artist INTEGER,
56 title VARCHAR(32),
57 year INTEGER
58);
59INSERT INTO cds VALUES (1, 1, 1, "Songs", 2005);
60INSERT INTO cds VALUES (2, 2, 1, "Read Headed Stanger", 2000);
61INSERT INTO cds VALUES (3, 1, 1, "Wanted! The Outlaws", 2004);
62INSERT INTO cds VALUES (4, 2, 1, "The Very Best of Willie Nelson", 1999);
63
64INSERT INTO cds VALUES (5, 1, 2, "12 Greates Hits", 1999);
65INSERT INTO cds VALUES (6, 2, 2, "Sweet Dreams", 1995);
66INSERT INTO cds VALUES (7, 3, 2, "The Best of Patsy Cline", 1991);
67
68---------------------------------------
69-- Tracks
70---------------------------------------
71CREATE TABLE tracks (
72 id INTEGER NOT NULL PRIMARY KEY,
73 cd INTEGER,
74 position INTEGER,
75 title VARCHAR(32)
76);
77INSERT INTO tracks VALUES (1, 1, 1, "Songs: Track 1");
78INSERT INTO tracks VALUES (2, 1, 2, "Songs: Track 2");
79INSERT INTO tracks VALUES (3, 1, 3, "Songs: Track 3");
80INSERT INTO tracks VALUES (4, 1, 4, "Songs: Track 4");
81
82INSERT INTO tracks VALUES (5, 2, 1, "Read Headed Stanger: Track 1");
83INSERT INTO tracks VALUES (6, 2, 2, "Read Headed Stanger: Track 2");
84INSERT INTO tracks VALUES (7, 2, 3, "Read Headed Stanger: Track 3");
85INSERT INTO tracks VALUES (8, 2, 4, "Read Headed Stanger: Track 4");
86
87INSERT INTO tracks VALUES (9, 3, 1, "Wanted! The Outlaws: Track 1");
88INSERT INTO tracks VALUES (10, 3, 2, "Wanted! The Outlaws: Track 2");
89
90INSERT INTO tracks VALUES (11, 4, 1, "The Very Best of Willie Nelson: Track 1");
91INSERT INTO tracks VALUES (12, 4, 2, "The Very Best of Willie Nelson: Track 2");
92INSERT INTO tracks VALUES (13, 4, 3, "The Very Best of Willie Nelson: Track 3");
93INSERT INTO tracks VALUES (14, 4, 4, "The Very Best of Willie Nelson: Track 4");
94INSERT INTO tracks VALUES (15, 4, 5, "The Very Best of Willie Nelson: Track 5");
95INSERT INTO tracks VALUES (16, 4, 6, "The Very Best of Willie Nelson: Track 6");
96
97INSERT INTO tracks VALUES (17, 5, 1, "12 Greates Hits: Track 1");
98INSERT INTO tracks VALUES (18, 5, 2, "12 Greates Hits: Track 2");
99INSERT INTO tracks VALUES (19, 5, 3, "12 Greates Hits: Track 3");
100INSERT INTO tracks VALUES (20, 5, 4, "12 Greates Hits: Track 4");
101
102INSERT INTO tracks VALUES (21, 6, 1, "Sweet Dreams: Track 1");
103INSERT INTO tracks VALUES (22, 6, 2, "Sweet Dreams: Track 2");
104INSERT INTO tracks VALUES (23, 6, 3, "Sweet Dreams: Track 3");
105INSERT INTO tracks VALUES (24, 6, 4, "Sweet Dreams: Track 4");
106
107INSERT INTO tracks VALUES (25, 7, 1, "The Best of Patsy Cline: Track 1");
108INSERT INTO tracks VALUES (26, 7, 2, "The Best of Patsy Cline: Track 2");
109
110SQL_END
111
112foreach 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
119Music::DBI->dbi_commit;
120
121package Music::Artist;
122use base 'Music::DBI';
123Music::Artist->table('artists');
124Music::Artist->columns(All => qw/id name/);
125
126
127package Music::Label;
128use base 'Music::DBI';
129Music::Label->table('labels');
130Music::Label->columns(All => qw/id name/);
131
132package Music::CD;
133use base 'Music::DBI';
134Music::CD->table('cds');
135Music::CD->columns(All => qw/id label artist title year/);
136
137
138package Music::Track;
139use base 'Music::DBI';
140Music::Track->table('tracks');
141Music::Track->columns(All => qw/id cd position title/);
142
143Music::Artist->has_many(cds => 'Music::CD');
144Music::Label->has_many(cds => 'Music::CD');
145Music::CD->has_many(tracks => 'Music::Track');
146Music::CD->has_a(artist => 'Music::Artist');
147Music::CD->has_a(label => 'Music::Label');
148Music::Track->has_a(cd => 'Music::CD');
149
150package 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 294done_testing;