X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F76select.t;h=8f45fd056d2984a0c04e0137750fb2e27cf30367;hb=26148d36e66d7f7c67863cea8d6501739b765692;hp=cd128b063914a3f4f31f44d9903eae3126acf66a;hpb=2b4fdf57c88d1a6a9a85853f4905f31e8c166f8c;p=dbsrgits%2FDBIx-Class.git diff --git a/t/76select.t b/t/76select.t index cd128b0..8f45fd0 100644 --- a/t/76select.t +++ b/t/76select.t @@ -1,5 +1,5 @@ use strict; -use warnings; +use warnings; use Test::More; use Test::Exception; @@ -9,8 +9,6 @@ use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); -plan tests => 21; - my $rs = $schema->resultset('CD')->search({}, { '+select' => \ 'COUNT(*)', @@ -29,16 +27,6 @@ $rs = $schema->resultset('CD')->search({}, lives_ok(sub { $rs->first->get_column('count') }, 'multiple +select/+as columns, 1st rscolumn present'); lives_ok(sub { $rs->first->get_column('addedtitle') }, 'multiple +select/+as columns, 2nd rscolumn present'); -# Tests a regression in ResultSetColumn wrt +select -$rs = $schema->resultset('CD')->search(undef, - { - '+select' => [ \'COUNT(*) AS year_count' ], - order_by => 'year_count' - } -); -my @counts = $rs->get_column('cdid')->all; -ok(scalar(@counts), 'got rows from ->all using +select'); - $rs = $schema->resultset('CD')->search({}, { '+select' => [ \ 'COUNT(*)', 'title' ], @@ -64,6 +52,7 @@ my $cds = $schema->resultset ('CD')->search ({}, { order_by => 'me.cdid'}); # ma cmp_ok ($cds->count, '>', 2, 'Initially populated with more than 2 CDs'); my $table = $cds->result_source->name; +$table = $$table if ref $table eq 'SCALAR'; my $subsel = $cds->search ({}, { columns => [qw/cdid title/], from => \ "(SELECT cdid, title FROM $table LIMIT 2) me", @@ -84,10 +73,8 @@ $rs = $schema->resultset('CD')->search({}, } ); -my ($sql, @bind) = @${$rs->as_query}; is_same_sql_bind ( - $sql, - \@bind, + $rs->as_query, '(SELECT me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)', [], 'Use of columns attribute results in proper sql' @@ -102,29 +89,27 @@ lives_ok(sub { }, 'columns 2nd rscolumn present'); lives_ok(sub { - $rs->first->artist->get_column('name') -}, 'columns 3rd rscolumn present'); + $rs->first->artist->get_column('name') +}, 'columns 3rd rscolumn present'); $rs = $schema->resultset('CD')->search({}, - { + { 'join' => 'artist', '+columns' => ['cdid', 'title', 'artist.name'], } ); -($sql, @bind) = @${$rs->as_query}; is_same_sql_bind ( - $sql, - \@bind, + $rs->as_query, '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.cdid, me.title, artist.name FROM cd me JOIN artist artist ON artist.artistid = me.artist)', [], 'Use of columns attribute results in proper sql' ); lives_ok(sub { - $rs->first->get_column('cdid') + $rs->first->get_column('cdid') }, 'columns 1st rscolumn present'); lives_ok(sub { @@ -134,3 +119,50 @@ lives_ok(sub { lives_ok(sub { $rs->first->artist->get_column('name') }, 'columns 3rd rscolumn present'); + + +$rs = $schema->resultset('CD')->search({'tracks.position' => { -in => [2] } }, + { + join => 'tracks', + columns => [qw/me.cdid me.title/], + '+select' => ['tracks.position'], + '+as' => ['track_position'], + + # get a hashref of CD1 only (the first with a second track) + result_class => 'DBIx::Class::ResultClass::HashRefInflator', + order_by => 'cdid', + rows => 1, + } +); + +is_deeply ( + $rs->single, + { + cdid => 1, + track_position => 2, + title => 'Spoonful of bees', + }, + 'limited prefetch via column works on a multi-relationship', +); + +my $sub_rs = $rs->search ({}, + { + columns => [qw/artist tracks.trackid/], # columns should not be merged but override $rs columns + '+select' => ['tracks.title'], + '+as' => ['tracks.title'], + } +); + +is_deeply( + $sub_rs->single, + { + artist => 1, + tracks => { + title => 'Apiary', + trackid => 17, + }, + }, + 'columns/select/as fold properly on sub-searches', +); + +done_testing;