From: Peter Rabbitson Date: Thu, 5 Jan 2012 02:46:01 +0000 (+0100) Subject: Extra tests from an old debugging session X-Git-Tag: v0.08204~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=commitdiff_plain;h=ff46b7b38691f09bcb57c0e0ce69e0b754b6f423 Extra tests from an old debugging session --- diff --git a/t/count/distinct.t b/t/count/distinct.t index 1ef8ccf..64fa856 100644 --- a/t/count/distinct.t +++ b/t/count/distinct.t @@ -111,6 +111,64 @@ throws_ok( is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly'); } +# and check distinct has_many join count +{ + my $rs = $schema->resultset('Artist')->search( + { 'cds.title' => { '!=', 'fooooo' } }, + { + join => 'cds', + distinct => 1, + '+select' => [ { count => 'cds.cdid', -as => 'amount_of_cds' } ], + '+as' => [qw/num_cds/], + order_by => { -desc => 'amount_of_cds' }, + } + ); + + is_same_sql_bind ( + $rs->as_query, + '( + SELECT me.artistid, me.name, me.rank, me.charfield, COUNT( cds.cdid ) AS amount_of_cds + FROM artist me + LEFT JOIN cd cds + ON cds.artist = me.artistid + WHERE cds.title != ? + GROUP BY me.artistid, me.name, me.rank, me.charfield + ORDER BY amount_of_cds DESC + )', + [ + [{ + sqlt_datatype => 'varchar', + dbic_colname => 'cds.title', + sqlt_size => 100, + } => 'fooooo' ], + ], + ); + + is_same_sql_bind ( + $rs->count_rs->as_query, + '( + SELECT COUNT( * ) + FROM ( + SELECT me.artistid, me.name, me.rank, me.charfield + FROM artist me + LEFT JOIN cd cds + ON cds.artist = me.artistid + WHERE cds.title != ? + GROUP BY me.artistid, me.name, me.rank, me.charfield + ) me + )', + [ + [{ + sqlt_datatype => 'varchar', + dbic_colname => 'cds.title', + sqlt_size => 100, + } => 'fooooo' ], + ], + ); + + is ($rs->next->get_column ('num_cds'), 3, 'Function aliased correctly'); +} + # These two rely on the database to throw an exception. This might not be the case one day. Please revise. dies_ok(sub { my $count = $schema->resultset('Tag')->search({}, { '+select' => \'tagid AS tag_id', distinct => 1 })->count }, 'expecting to die'); diff --git a/t/resultset/as_subselect_rs.t b/t/resultset/as_subselect_rs.t index 8644079..c0f8110 100644 --- a/t/resultset/as_subselect_rs.t +++ b/t/resultset/as_subselect_rs.t @@ -40,4 +40,35 @@ is_same_sql_bind ( 'Resultset-class attributes do not seep outside of the subselect', ); +$schema->storage->debug(1); + +is_same_sql_bind( + $schema->resultset('CD')->search ({}, { + rows => 2, + join => [ 'genre', { artist => 'cds' } ], + distinct => 1, + columns => { + title => 'me.title', + artist__name => 'artist.name', + genre__name => 'genre.name', + cds_for_artist => \ '(SELECT COUNT(*) FROM cds WHERE cd.artist = artist.id)', + }, + order_by => { -desc => 'me.year' }, + })->count_rs->as_query, + '( + SELECT COUNT( * ) + FROM ( + SELECT artist.name AS artist__name, (SELECT COUNT(*) FROM cds WHERE cd.artist = artist.id), genre.name AS genre__name, me.title, me.year + FROM cd me + LEFT JOIN genre genre + ON genre.genreid = me.genreid + JOIN artist artist ON artist.artistid = me.artist + GROUP BY artist.name, (SELECT COUNT(*) FROM cds WHERE cd.artist = artist.id), genre.name, me.title, me.year + LIMIT ? + ) me + )', + [ [{ sqlt_datatype => 'integer' } => 2 ] ], +); + + done_testing;