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