use Test::More;
use DBICTest;
use DBIC::SqlMakerTest;
-use DBIC::DebugObj;
-
-plan tests => 17;
my $schema = DBICTest->init_schema();
{ prefetch => [qw/tracks artist/] },
);
is ($rs->all, 5, 'Correct number of objects');
-
-
- my ($sql, @bind);
- $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
- $schema->storage->debug(1);
-
-
- is ($rs->count, 5, 'Correct count');
-
- is_same_sql_bind (
- $sql,
- \@bind,
- 'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
- [ qw/'1' '2'/ ],
- );
-}
-
-# Added test by mo per http://scsys.co.uk:8001/31870
-TODO: {
- todo_skip "This breaks stuff", 3;
- my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
- ->search_related('cds')->search_related('genre',
- { 'genre.name' => 'foo' },
- { prefetch => q(cds) },
- );
- is ($rs->all, 5, 'Correct number of objects');
-
-
- my ($sql, @bind);
- $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
- $schema->storage->debug(1);
-
-
is ($rs->count, 5, 'Correct count');
is_same_sql_bind (
- $sql,
- \@bind,
- 'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
- [ qw/'1' '2'/ ],
+ $rs->count_rs->as_query,
+ '(
+ SELECT COUNT( * )
+ FROM (
+ SELECT cds.cdid
+ FROM artist me
+ JOIN cd cds ON cds.artist = me.artistid
+ LEFT JOIN track tracks ON tracks.cd = cds.cdid
+ JOIN artist artist ON artist.artistid = cds.artist
+ WHERE tracks.position = ? OR tracks.position = ?
+ GROUP BY cds.cdid
+ ) count_subq
+ )',
+ [ map { [ 'tracks.position' => $_ ] } (1, 2) ],
);
}
# collapsing prefetch with distinct
-TODO: {
- todo_skip "This is busted", 3;
+{
my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
->search_related('cds')->search_related('genre',
- { 'genre.name' => 'foo' },
+ { 'genre.name' => 'emo' },
{ prefetch => q(cds) },
);
- is ($rs->all, 5, 'Correct number of objects');
-
-
- my ($sql, @bind);
- $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
- $schema->storage->debug(1);
-
-
- is ($rs->count, 5, 'Correct count');
+ is ($rs->all, 1, 'Correct number of objects');
+ is ($rs->count, 1, 'Correct count');
is_same_sql_bind (
- $sql,
- \@bind,
- 'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid) count_subq',
- [ qw/'1' '2'/ ],
+ $rs->count_rs->as_query,
+ '(
+ SELECT COUNT( * )
+ FROM (
+ SELECT genre.genreid
+ FROM (
+ SELECT cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track
+ FROM (
+ SELECT me.artistid, me.name, me.rank, me.charfield
+ FROM artist me GROUP BY me.artistid, me.name, me.rank, me.charfield
+ ) me
+ LEFT JOIN cd cds ON cds.artist = me.artistid
+ GROUP BY cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track
+ ) cds
+ JOIN genre genre ON genre.genreid = cds.genreid
+ LEFT JOIN cd cds_2 ON cds_2.genreid = genre.genreid
+ WHERE ( genre.name = ? )
+ GROUP BY genre.genreid
+ ) count_subq
+ )',
+ [ [ 'genre.name' => 'emo' ] ],
);
}
is ($rs->all, 10, 'Correct number of objects');
- my ($sql, @bind);
- $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
- $schema->storage->debug(1);
-
-
is ($rs->count, 10, 'Correct count');
is_same_sql_bind (
- $sql,
- \@bind,
- 'SELECT COUNT( * ) FROM cd me JOIN track tracks ON tracks.cd = me.cdid JOIN cd disc ON disc.cdid = tracks.cd LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid WHERE ( ( position = ? OR position = ? ) )',
- [ qw/'1' '2'/ ],
+ $rs->count_rs->as_query,
+ '(
+ SELECT COUNT( * )
+ FROM cd me
+ JOIN track tracks ON tracks.cd = me.cdid
+ JOIN cd disc ON disc.cdid = tracks.cd
+ LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid
+ WHERE position = ? OR position = ?
+ )',
+ [ map { [ position => $_ ] } (1, 2) ],
);
}
-# Added test by mo per http://scsys.co.uk:8001/31873
-TODO: {
- todo_skip "This breaks stuff", 5;
- my $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
- ->search_related('artwork_to_artist')->search_related('artist',
- undef,
- { prefetch => q(cds) },
- );
- is($rs->all, 0, 'failure without WHERE');
-
- $rs = $schema->resultset("Artwork")->search(undef, {distinct => 1})
- ->search_related('artwork_to_artist')->search_related('artist',
- { 'cds.title' => 'foo' }, # this line has changed
- { prefetch => q(cds) },
- );
- is($rs->all, 0, 'success with WHERE');
-
- # different case
-
- $rs = $schema->resultset("Artist")->search(undef)#, {distinct => 1})
- ->search_related('cds')->search_related('genre',
- { 'genre.name' => 'foo' },
- { prefetch => q(cds) },
- );
- is($rs->all, 0, 'success without distinct');
-
- $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
- ->search_related('cds')->search_related('genre',
- { 'genre.name' => 'foo' },
- #{ prefetch => q(cds) },
- );
- is($rs->all, 0, 'success without prefetch');
-
- $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
- ->search_related('cds')->search_related('genre',
- { 'genre.name' => 'foo' },
- { prefetch => q(cds) },
- );
- is($rs->all, 0, 'failure with distinct');
-}
+done_testing;