X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fcustom.t;h=10ab27c9ccbc28267799225d6ba8926b4acf6d2a;hb=83a6b244;hp=99a0786a9b82bbf9e2f7ea07cc73d73e79cdb6ce;hpb=b4e9f590228d1d73d4089c2ec88372e683e17aeb;p=dbsrgits%2FDBIx-Class.git diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 99a0786..10ab27c 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -4,8 +4,7 @@ use warnings; use Test::More; use Test::Exception; use lib qw(t/lib); -use DBICTest; -use DBIC::SqlMakerTest; +use DBICTest ':DiffSQL'; my $schema = DBICTest->init_schema(); @@ -44,7 +43,7 @@ is_same_sql_bind( )', [ [ - { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } + {} => 21 ], [ @@ -86,6 +85,39 @@ is_same_sql_bind( ], ] ); + +# re-test with ::-containing moniker name +# (we don't have any currently, so fudge it with lots of local() ) +{ + local $schema->source('Artist')->{source_name} = 'Ar::Tist'; + local $artist2->{related_resultsets}; + + is_same_sql_bind( + $artist2->cds_90s->as_query, + '( + SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + FROM artist ar_tist__row + JOIN cd me + ON ( me.artist = ar_tist__row.artistid AND ( me.year < ? AND me.year > ? ) ) + WHERE ( ar_tist__row.artistid = ? ) + )', + [ + [ + { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } + => 2000 + ], + [ + { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'me.year' } + => 1989 + ], + [ { sqlt_datatype => 'integer', dbic_colname => 'ar_tist__row.artistid' } + => 22 + ], + ] + ); +} + + my @cds_90s = $cds_90s_rs->all; is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search'); map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s; @@ -106,9 +138,6 @@ is_deeply( '16 correct cds found' ); -TODO: { -local $TODO = 'Prefetch on custom rels can not work until the collapse rewrite is finished ' - . '(currently collapser requires a right-side (which is indeterministic) order-by)'; lives_ok { my @all_artists_with_80_cds_pref = $schema->resultset("Artist")->search @@ -121,17 +150,21 @@ is_deeply( ); } 'prefetchy-fetchy-fetch'; -} # end of TODO +# create_related a plain cd via the equoivalent coderef cond, with no extra conditions +lives_ok { + $artist->create_related('cds_cref_cond', { title => 'related creation via coderef cond', year => '2010' } ); +} 'created_related with simple condition works'; # try to create_related a 80s cd throws_ok { $artist->create_related('cds_80s', { title => 'related creation 1' }); -} qr/\QCustom relationship 'cds_80s' not definitive - returns conditions instead of values for column(s): 'year'/, +} qr/\QUnable to complete value inferrence - custom relationship 'cds_80s' returns conditions instead of values for column(s): 'year'/, 'Create failed - complex cond'; # now supply an explicit arg overwriting the ambiguous cond -my $id_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' })->id; +my $cd_2020 = $artist->create_related('cds_80s', { title => 'related creation 2', year => '2020' }); +my $id_2020 = $cd_2020->id; is( $schema->resultset('CD')->find($id_2020)->title, 'related creation 2', @@ -149,7 +182,7 @@ is( # try a specific everything via a non-simplified rel throws_ok { $artist->create_related('cds_90s', { title => 'related_creation 4', year => '2038' }); -} qr/\QCustom relationship 'cds_90s' does not resolve to a join-free condition fragment/, +} qr/\QRelationship 'cds_90s' does not resolve to a join-free condition fragment/, 'Create failed - non-simplified rel'; # Do a self-join last-entry search @@ -161,8 +194,8 @@ for my $cd ($schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all) { } my $last_tracks_rs = $schema->resultset('Track')->search ( - {'next_track.trackid' => undef}, - { join => 'next_track', order_by => 'me.cd' }, + {'next_tracks.trackid' => undef}, + { join => 'next_tracks', order_by => 'me.cd' }, ); is_deeply ( @@ -171,6 +204,18 @@ is_deeply ( 'last group-entry via self-join works', ); +is_deeply ( + [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid', prefetch => 'last_track'})->all], + [ map { $_->trackid } @last_tracks ], + 'last_track via insane subquery condition works', +); + +is_deeply ( + [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all], + [ map { $_->trackid } @last_tracks ], + 'last_track via insane subquery condition works, even without prefetch', +); + my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first; my @artists = $artwork->artists->all; is(scalar @artists, 2, 'the two artists are associated'); @@ -228,4 +273,21 @@ is_deeply ( 'Prefetched singles in proper order' ); +# test set_from_related with a belongs_to custom condition +my $cd = $schema->resultset("CD")->find(4); +$artist = $cd->search_related('artist'); +my $track = $schema->resultset("Track")->create( { + trackid => 1, + cd => 3, + position => 99, + title => 'Some Track' +} ); +$track->set_from_related( cd_cref_cond => $cd ); +is ($track->get_column('cd'), 4, 'set from related via coderef cond'); +is_deeply ( + { $track->cd->get_columns }, + { $cd->get_columns }, + 'set from related via coderef cond inflates properly', +); + done_testing;