5 use lib 't/cdbi/testlib';
6 use DBIC::Test::SQLite (); # this will issue the necessary SKIPs on missing reqs
9 eval { require Class::DBI::Plugin::DeepAbstractSearch }
10 or plan skip_all => 'Class::DBI::Plugin::DeepAbstractSearch required for this test';
13 my $DB = DBICTest->_sqlite_dbname(sqlite_use_file => 1);;
15 # not usre why this test needs an AutoCommit => 0 and a commit further
17 $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1;
19 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 0 });
22 use base qw(DBIx::Class::CDBICompat);
23 use Class::DBI::Plugin::DeepAbstractSearch;
24 __PACKAGE__->connection(@DSN);
26 my $sql = <<'SQL_END';
28 ---------------------------------------
30 ---------------------------------------
31 CREATE TABLE artists (
32 id INTEGER NOT NULL PRIMARY KEY,
36 INSERT INTO artists VALUES (1, "Willie Nelson");
37 INSERT INTO artists VALUES (2, "Patsy Cline");
39 ---------------------------------------
41 ---------------------------------------
43 id INTEGER NOT NULL PRIMARY KEY,
47 INSERT INTO labels VALUES (1, "Columbia");
48 INSERT INTO labels VALUES (2, "Sony");
49 INSERT INTO labels VALUES (3, "Supraphon");
51 ---------------------------------------
53 ---------------------------------------
55 id INTEGER NOT NULL PRIMARY KEY,
61 INSERT INTO cds VALUES (1, 1, 1, "Songs", 2005);
62 INSERT INTO cds VALUES (2, 2, 1, "Read Headed Stanger", 2000);
63 INSERT INTO cds VALUES (3, 1, 1, "Wanted! The Outlaws", 2004);
64 INSERT INTO cds VALUES (4, 2, 1, "The Very Best of Willie Nelson", 1999);
66 INSERT INTO cds VALUES (5, 1, 2, "12 Greates Hits", 1999);
67 INSERT INTO cds VALUES (6, 2, 2, "Sweet Dreams", 1995);
68 INSERT INTO cds VALUES (7, 3, 2, "The Best of Patsy Cline", 1991);
70 ---------------------------------------
72 ---------------------------------------
74 id INTEGER NOT NULL PRIMARY KEY,
79 INSERT INTO tracks VALUES (1, 1, 1, "Songs: Track 1");
80 INSERT INTO tracks VALUES (2, 1, 2, "Songs: Track 2");
81 INSERT INTO tracks VALUES (3, 1, 3, "Songs: Track 3");
82 INSERT INTO tracks VALUES (4, 1, 4, "Songs: Track 4");
84 INSERT INTO tracks VALUES (5, 2, 1, "Read Headed Stanger: Track 1");
85 INSERT INTO tracks VALUES (6, 2, 2, "Read Headed Stanger: Track 2");
86 INSERT INTO tracks VALUES (7, 2, 3, "Read Headed Stanger: Track 3");
87 INSERT INTO tracks VALUES (8, 2, 4, "Read Headed Stanger: Track 4");
89 INSERT INTO tracks VALUES (9, 3, 1, "Wanted! The Outlaws: Track 1");
90 INSERT INTO tracks VALUES (10, 3, 2, "Wanted! The Outlaws: Track 2");
92 INSERT INTO tracks VALUES (11, 4, 1, "The Very Best of Willie Nelson: Track 1");
93 INSERT INTO tracks VALUES (12, 4, 2, "The Very Best of Willie Nelson: Track 2");
94 INSERT INTO tracks VALUES (13, 4, 3, "The Very Best of Willie Nelson: Track 3");
95 INSERT INTO tracks VALUES (14, 4, 4, "The Very Best of Willie Nelson: Track 4");
96 INSERT INTO tracks VALUES (15, 4, 5, "The Very Best of Willie Nelson: Track 5");
97 INSERT INTO tracks VALUES (16, 4, 6, "The Very Best of Willie Nelson: Track 6");
99 INSERT INTO tracks VALUES (17, 5, 1, "12 Greates Hits: Track 1");
100 INSERT INTO tracks VALUES (18, 5, 2, "12 Greates Hits: Track 2");
101 INSERT INTO tracks VALUES (19, 5, 3, "12 Greates Hits: Track 3");
102 INSERT INTO tracks VALUES (20, 5, 4, "12 Greates Hits: Track 4");
104 INSERT INTO tracks VALUES (21, 6, 1, "Sweet Dreams: Track 1");
105 INSERT INTO tracks VALUES (22, 6, 2, "Sweet Dreams: Track 2");
106 INSERT INTO tracks VALUES (23, 6, 3, "Sweet Dreams: Track 3");
107 INSERT INTO tracks VALUES (24, 6, 4, "Sweet Dreams: Track 4");
109 INSERT INTO tracks VALUES (25, 7, 1, "The Best of Patsy Cline: Track 1");
110 INSERT INTO tracks VALUES (26, 7, 2, "The Best of Patsy Cline: Track 2");
114 foreach 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 "$@ $!";
121 Music::DBI->dbi_commit;
123 package Music::Artist;
124 use base 'Music::DBI';
125 Music::Artist->table('artists');
126 Music::Artist->columns(All => qw/id name/);
129 package Music::Label;
130 use base 'Music::DBI';
131 Music::Label->table('labels');
132 Music::Label->columns(All => qw/id name/);
135 use base 'Music::DBI';
136 Music::CD->table('cds');
137 Music::CD->columns(All => qw/id label artist title year/);
140 package Music::Track;
141 use base 'Music::DBI';
142 Music::Track->table('tracks');
143 Music::Track->columns(All => qw/id cd position title/);
145 Music::Artist->has_many(cds => 'Music::CD');
146 Music::Label->has_many(cds => 'Music::CD');
147 Music::CD->has_many(tracks => 'Music::Track');
148 Music::CD->has_a(artist => 'Music::Artist');
149 Music::CD->has_a(label => 'Music::Label');
150 Music::Track->has_a(cd => 'Music::CD');
157 my @artists = Music::Artist->deep_search_where($where, $attr);
158 is_deeply [ sort @artists ], [ 1, 2 ], "all without order";
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";
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";
176 my $where = { name => { -like => 'Patsy Cline' }, };
178 my @artists = Music::Artist->deep_search_where($where, $attr);
179 is_deeply \@artists, [ 2 ], "simple search";
183 my $where = { 'artist.name' => 'Patsy Cline' };
185 my @cds = Music::CD->deep_search_where($where, $attr);
186 is_deeply [ sort @cds ], [ 5, 6, 7 ], "Patsy's CDs";
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";
195 my $count = Music::CD->count_deep_search_where($where);
196 is_deeply $count, 3, "count Patsy's CDs by title";
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'";
208 'cd.artist.name' => { -like => 'W%' },
209 'cd.year' => { '>' => 2000 },
210 'position' => { '<' => 3 }
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 ";
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";
222 'cd.artist.name' => { -like => 'W%' },
223 'cd.year' => { '>' => 2000 },
224 'position' => { '<' => 3 }
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 ";
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";
235 my $where = { 'cd.title' => [ -and => { -like => '%o%' }, { -like => '%W%' } ] };
236 my $attr = { order_by => [ 'cd.id' ] } ;
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'";
243 my $where = { 'cd.year' => [ 1995, 1999 ] };
244 my $attr = { order_by => [ 'cd.id' ] } ;
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";
252 my $where = { 'cd.year' => { -in => [ 1995, 1999 ] } };
253 my $attr = { order_by => [ 'cd.id' ] } ;
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";
261 my $where = { -and => [ 'cd.year' => [ 1995, 1999 ], position => { '<=', 2 } ] };
262 my $attr = { order_by => [ 'cd.id' ] } ;
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";
270 my $where = { -and => [ 'cd.year' => { -in => [ 1995, 1999 ] }, position => { '<=', 2 } ] };
271 my $attr = { order_by => [ 'cd.id' ] } ;
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";
279 my $where = { 'label.name' => { -in => [ 'Sony', 'Supraphon', 'Bogus' ] } };
280 my $attr = { order_by => [ 'id' ] } ;
282 my @cds = Music::CD->deep_search_where($where, $attr);
283 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
284 "CDs from Sony or Supraphon";
288 my $where = { 'label.name' => [ 'Sony', 'Supraphon', 'Bogus' ] };
289 my $attr = { order_by => [ 'id' ] } ;
291 my @cds = Music::CD->deep_search_where($where, $attr);
292 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
293 "CDs from Sony or Supraphon";