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