4 use lib 't/cdbi/testlib';
5 use DBIC::Test::SQLite (); # this will issue the necessary SKIPs on missing reqs
8 eval { require Class::DBI::Plugin::DeepAbstractSearch }
9 or plan skip_all => 'Class::DBI::Plugin::DeepAbstractSearch required for this test';
12 my $DB = DBICTest->_sqlite_dbname(sqlite_use_file => 1);;
14 # not usre why this test needs an AutoCommit => 0 and a commit further
16 $ENV{DBIC_UNSAFE_AUTOCOMMIT_OK} = 1;
18 my @DSN = ("dbi:SQLite:dbname=$DB", '', '', { AutoCommit => 0 });
21 use base qw(DBIx::Class::CDBICompat);
22 use Class::DBI::Plugin::DeepAbstractSearch;
23 __PACKAGE__->connection(@DSN);
25 my $sql = <<'SQL_END';
27 ---------------------------------------
29 ---------------------------------------
30 CREATE TABLE artists (
31 id INTEGER NOT NULL PRIMARY KEY,
35 INSERT INTO artists VALUES (1, "Willie Nelson");
36 INSERT INTO artists VALUES (2, "Patsy Cline");
38 ---------------------------------------
40 ---------------------------------------
42 id INTEGER NOT NULL PRIMARY KEY,
46 INSERT INTO labels VALUES (1, "Columbia");
47 INSERT INTO labels VALUES (2, "Sony");
48 INSERT INTO labels VALUES (3, "Supraphon");
50 ---------------------------------------
52 ---------------------------------------
54 id INTEGER NOT NULL PRIMARY KEY,
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);
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);
69 ---------------------------------------
71 ---------------------------------------
73 id INTEGER NOT NULL PRIMARY KEY,
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");
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");
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");
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");
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");
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");
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");
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 "$@ $!";
120 Music::DBI->dbi_commit;
122 package Music::Artist;
123 use base 'Music::DBI';
124 Music::Artist->table('artists');
125 Music::Artist->columns(All => qw/id name/);
128 package Music::Label;
129 use base 'Music::DBI';
130 Music::Label->table('labels');
131 Music::Label->columns(All => qw/id name/);
134 use base 'Music::DBI';
135 Music::CD->table('cds');
136 Music::CD->columns(All => qw/id label artist title year/);
139 package Music::Track;
140 use base 'Music::DBI';
141 Music::Track->table('tracks');
142 Music::Track->columns(All => qw/id cd position title/);
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');
156 my @artists = Music::Artist->deep_search_where($where, $attr);
157 is_deeply [ sort @artists ], [ 1, 2 ], "all without order";
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";
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";
175 my $where = { name => { -like => 'Patsy Cline' }, };
177 my @artists = Music::Artist->deep_search_where($where, $attr);
178 is_deeply \@artists, [ 2 ], "simple search";
182 my $where = { 'artist.name' => 'Patsy Cline' };
184 my @cds = Music::CD->deep_search_where($where, $attr);
185 is_deeply [ sort @cds ], [ 5, 6, 7 ], "Patsy's CDs";
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";
194 my $count = Music::CD->count_deep_search_where($where);
195 is_deeply $count, 3, "count Patsy's CDs by title";
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'";
207 'cd.artist.name' => { -like => 'W%' },
208 'cd.year' => { '>' => 2000 },
209 'position' => { '<' => 3 }
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 ";
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";
221 'cd.artist.name' => { -like => 'W%' },
222 'cd.year' => { '>' => 2000 },
223 'position' => { '<' => 3 }
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 ";
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";
234 my $where = { 'cd.title' => [ -and => { -like => '%o%' }, { -like => '%W%' } ] };
235 my $attr = { order_by => [ 'cd.id' ] } ;
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'";
242 my $where = { 'cd.year' => [ 1995, 1999 ] };
243 my $attr = { order_by => [ 'cd.id' ] } ;
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";
251 my $where = { 'cd.year' => { -in => [ 1995, 1999 ] } };
252 my $attr = { order_by => [ 'cd.id' ] } ;
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";
260 my $where = { -and => [ 'cd.year' => [ 1995, 1999 ], position => { '<=', 2 } ] };
261 my $attr = { order_by => [ 'cd.id' ] } ;
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";
269 my $where = { -and => [ 'cd.year' => { -in => [ 1995, 1999 ] }, position => { '<=', 2 } ] };
270 my $attr = { order_by => [ 'cd.id' ] } ;
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";
278 my $where = { 'label.name' => { -in => [ 'Sony', 'Supraphon', 'Bogus' ] } };
279 my $attr = { order_by => [ 'id' ] } ;
281 my @cds = Music::CD->deep_search_where($where, $attr);
282 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
283 "CDs from Sony or Supraphon";
287 my $where = { 'label.name' => [ 'Sony', 'Supraphon', 'Bogus' ] };
288 my $attr = { order_by => [ 'id' ] } ;
290 my @cds = Music::CD->deep_search_where($where, $attr);
291 is_deeply [ @cds ], [ 2, 4, 6, 7 ],
292 "CDs from Sony or Supraphon";