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