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