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