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