X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fcustom.t;h=32c8cf8f2ca574cd808b31814ffe34705cf8798c;hb=2d9a96fd40eb4be24a9a254953e1e3cd8e20ea3a;hp=c136224bb821c01aa132636c0c5fedbc5f8cda50;hpb=fe4118b138326291850bbe2438f61cd0e296034b;p=dbsrgits%2FDBIx-Class.git diff --git a/t/relationship/custom.t b/t/relationship/custom.t index c136224..32c8cf8 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -1,11 +1,13 @@ +BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) } + use strict; use warnings; use Test::More; use Test::Exception; -use lib qw(t/lib); -use DBICTest; -use DBIC::SqlMakerTest; +use Test::Warn; + +use DBICTest ':DiffSQL'; my $schema = DBICTest->init_schema(); @@ -44,7 +46,7 @@ is_same_sql_bind( )', [ [ - { sqlt_datatype => 'integer', dbic_colname => 'me.artist' } + {} => 21 ], [ @@ -86,6 +88,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 +141,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 +153,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' on source 'Artist' 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 +185,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' on source 'Artist' does not resolve to a join-free condition fragment/, 'Create failed - non-simplified rel'; # Do a self-join last-entry search @@ -161,8 +197,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 ( @@ -183,38 +219,111 @@ is_deeply ( '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; +my @artists = $artwork->artists->search({}, { order_by => 'artistid' } ); is(scalar @artists, 2, 'the two artists are associated'); my @artwork_artists = $artwork->artwork_to_artist->all; foreach (@artwork_artists) { lives_ok { my $artista = $_->artist; - my $artistb = $_->artist_test_m2m; + my $artistb = $_->artist_limited_rank; ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.'); - my $artistc = $_->artist_test_m2m_noopt; + my $artistc = $_->artist_limited_rank_opaque; ok($artista->rank < 10 ? $artistc : 1, 'belongs_to with custom rel works even in non-simplified.'); } 'belongs_to works with custom rels'; } -@artists = (); -lives_ok { - @artists = $artwork->artists_test_m2m2->all; -} 'manytomany with extended rels in the has many works'; -is(scalar @artists, 2, 'two artists'); +is( + $schema->resultset('Artwork') + ->related_resultset( 'artwork_to_artist_via_opaque_customcond' ) + ->related_resultset( 'artist' ) + ->search({}, { collapse => 1 }) + ->count, + 2, + 'Custom rel works correctly', +); -@artists = (); -lives_ok { - @artists = $artwork->artists_test_m2m->all; -} 'can fetch many to many with optimized version'; -is(scalar @artists, 1, 'only one artist is associated'); +is ( + scalar $artwork->all_artists_via_opaque_customcond->all, + 2, + 'Expected two m2m associated artist objects via opaque costom cond' +); -@artists = (); -lives_ok { - @artists = $artwork->artists_test_m2m_noopt->all; -} 'can fetch many to many with non-optimized version'; -is(scalar @artists, 1, 'only one artist is associated'); +for (qw( artist_limited_rank artist_limited_rank_opaque )) { + is( + $schema->resultset('Artwork') + ->related_resultset( 'artwork_to_artist_via_opaque_customcond' ) + ->related_resultset( $_ ) + ->search({}, { collapse => 1 }) + ->count, + 1, + 'Condition over double custom rel works correctly', + ); + + is ( + scalar $artwork->$_->all, + 1, + 'Expected one m2m associated artist object via opaque custom cond + conditional far cond' + ); + + cmp_ok( + $artwork->${\"remove_from_$_"} ( $artists[1] ), + '==', + 0, + 'deletion action reports 0' + ); + + is ( + scalar $artwork->all_artists_via_opaque_customcond->all, + 2, + 'Indeed nothing was removed' + ); + + cmp_ok( + $artwork->${\"remove_from_$_"} ( $artists[0] ), + '==', + 1, + 'Removal reports correct count' + ); + + is ( + scalar $artwork->all_artists_via_opaque_customcond->all, + 1, + 'Indeed removed the matching artist' + ); + + $artwork->${\"set_$_"}([]); + + is ( + scalar $artwork->all_artists_via_opaque_customcond->all, + 0, + 'Everything removed via limited far cond' + ); + + # can't use the opaque one - need set_from_related to work + $artwork->set_artist_limited_rank( \@artists ); + + { + local $TODO = 'Taking into account the relationship bridge condition is not likely to ever work... unless we get DQ hooked somehow'; + + is ( + scalar $artwork->all_artists_via_opaque_customcond->all, + 1, + 'Re-Addition passed through only one of the artists' + ); + } + + throws_ok { $artwork->set_all_artists_via_opaque_customcond( \@artists ) } + qr/\QRelationship 'artwork_to_artist_via_opaque_customcond' on source 'Artwork' does not resolve to a join-free condition fragment/; + + is ( + scalar $artwork->all_artists_via_opaque_customcond->all, + 2, + 'Everything still there as expected' + ); +} # Make a single for each last_track @@ -240,4 +349,71 @@ is_deeply ( 'Prefetched singles in proper order' ); +# test set_from_related/find_related with a belongs_to custom condition +my $preexisting_cd = $schema->resultset('CD')->find(1); + +my $cd_single_track = $schema->resultset('CD')->create({ + artist => $artist, + title => 'one one one', + year => 2001, + tracks => [{ title => 'uno uno uno' }] +}); + +my $single_track = $cd_single_track->tracks->next; + +is( + $single_track->cd_cref_cond->title, + $cd_single_track->title, + 'Got back the expected single-track cd title', +); + +is_deeply + { $schema->resultset('Track')->find({ cd_cref_cond => { cdid => $cd_single_track->id } })->get_columns }, + { $single_track->get_columns }, + 'Proper find with related via coderef cond', +; + +warnings_exist { + is_same_sql_bind( + $single_track->deliberately_broken_all_cd_tracks->as_query, + '( + SELECT me.trackid, me.cd, me.position, me.title, me.last_updated_on, me.last_updated_at + FROM track track__row + JOIN track me + ON me.cd = ? + WHERE track__row.trackid = ? + )', + [ + [{ dbic_colname => "me.cd", sqlt_datatype => "integer" } + => "track__row.cd" ], + [{ dbic_colname => "track__row.trackid", sqlt_datatype => "integer" } + => 19 ], + ], + 'Expected nonsensical JOIN cond', + ), +} qr/\Qrelationship 'deliberately_broken_all_cd_tracks' on source 'Track' specifies equality of column 'cd' and the *VALUE* 'cd' (you did not use the { -ident => ... } operator)/, + 'Warning on 99.9999% malformed custom cond' +; + +$single_track->set_from_related( cd_cref_cond => undef ); +ok $single_track->is_column_changed('cd'); +is $single_track->get_column('cd'), undef, 'UNset from related via coderef cond'; +is $single_track->cd, undef, 'UNset related object via coderef cond'; + +$single_track->discard_changes; + +$single_track->set_from_related( cd_cref_cond => $preexisting_cd ); +ok $single_track->is_column_changed('cd'); +is $single_track->get_column('cd'), 1, 'set from related via coderef cond'; +is_deeply + { $single_track->cd->get_columns }, + { $preexisting_cd->get_columns }, + 'set from related via coderef cond inflates properly', +; + +throws_ok { + local $schema->source('Track')->relationship_info('cd_cref_cond')->{cond} = sub { 1,2,3 }; + $schema->resultset('Track')->find({ cd_cref_cond => {} }); +} qr/\QA custom condition coderef can return at most 2 conditions, but relationship 'cd_cref_cond' on source 'Track' returned extra values: 3/; + done_testing;