From: Daniel Ruoso Date: Mon, 22 Nov 2010 16:51:53 +0000 (-0300) Subject: adds a test with many to many and extended rels that is currently failing. X-Git-Tag: v0.08190~1^2~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e98e6478c9b65275ee6afb9710f13f5dc2090b09;p=dbsrgits%2FDBIx-Class.git adds a test with many to many and extended rels that is currently failing. --- diff --git a/t/lib/DBICTest/Schema/Artwork.pm b/t/lib/DBICTest/Schema/Artwork.pm index 4eecef5..6aa8df9 100644 --- a/t/lib/DBICTest/Schema/Artwork.pm +++ b/t/lib/DBICTest/Schema/Artwork.pm @@ -17,4 +17,22 @@ __PACKAGE__->has_many('images', 'DBICTest::Schema::Image', 'artwork_id'); __PACKAGE__->has_many('artwork_to_artist', 'DBICTest::Schema::Artwork_to_Artist', 'artwork_cd_id'); __PACKAGE__->many_to_many('artists', 'artwork_to_artist', 'artist'); +# both to test manytomany with custom rel +__PACKAGE__->many_to_many('artists_test_m2m', 'artwork_to_artist', 'artist_test_m2m'); +__PACKAGE__->many_to_many('artists_test_m2m_noopt', 'artwork_to_artist', 'artist_test_m2m_noopt'); + +# other test to manytomany +__PACKAGE__->has_many('artwork_to_artist_test_m2m', 'DBICTest::Schema::Artwork_to_Artist', + sub { + my $args = shift; + return ( + { "$args->{foreign_alias}.artwork_cd_id" => { -ident => "$args->{self_alias}.cd_id" }, + }, + $args->{self_rowobj} && { + "$args->{foreign_alias}.artwork_cd_id" => $args->{self_rowobj}->cd_id, + } + ); + }); +__PACKAGE__->many_to_many('artists_test_m2m2', 'artwork_to_artist_test_m2m', 'artist'); + 1; diff --git a/t/lib/DBICTest/Schema/Artwork_to_Artist.pm b/t/lib/DBICTest/Schema/Artwork_to_Artist.pm index 0859080..7cb25ca 100644 --- a/t/lib/DBICTest/Schema/Artwork_to_Artist.pm +++ b/t/lib/DBICTest/Schema/Artwork_to_Artist.pm @@ -18,4 +18,28 @@ __PACKAGE__->set_primary_key(qw/artwork_cd_id artist_id/); __PACKAGE__->belongs_to('artwork', 'DBICTest::Schema::Artwork', 'artwork_cd_id'); __PACKAGE__->belongs_to('artist', 'DBICTest::Schema::Artist', 'artist_id'); +__PACKAGE__->belongs_to('artist_test_m2m', 'DBICTest::Schema::Artist', + sub { + my $args = shift; + return ( + { "$args->{foreign_alias}.artistid" => { -ident => "$args->{self_alias}.artist_id" }, + "$args->{foreign_alias}.rank" => { '<' => 10 }, + }, + $args->{self_rowobj} && { + "$args->{foreign_alias}.artistid" => $args->{self_rowobj}->artist_id, + "$args->{foreign_alias}.rank" => { '<' => 10 }, + } + ); + }); + +__PACKAGE__->belongs_to('artist_test_m2m_noopt', 'DBICTest::Schema::Artist', + sub { + my $args = shift; + return ( + { "$args->{foreign_alias}.artistid" => { -ident => "$args->{self_alias}.artist_id" }, + "$args->{foreign_alias}.rank" => { '<' => 10 }, + } + ); + }); + 1; diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 369aeb0..24556f9 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -8,15 +8,25 @@ use DBICTest; my $schema = DBICTest->init_schema(); +my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson', rank => 20 }); +my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque', rank => 1 }) ; +my $artist3 = $schema->resultset("Artist")->create({ name => 'Ziraldo', rank => 1 }); +my $artist4 = $schema->resultset("Artist")->create({ name => 'Paulo Caruso', rank => 20 }); + +my @artworks; -my $artist = $schema->resultset("Artist")->create({ name => 'Michael Jackson' }); foreach my $year (1975..1985) { - $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); + my $cd = $artist->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); + push @artworks, $cd->create_related('artwork', {}); } -my $artist2 = $schema->resultset("Artist")->create({ name => 'Chico Buarque' }) ; foreach my $year (1975..1995) { - $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); + my $cd = $artist2->create_related('cds', { year => $year, title => 'Compilation from ' . $year }); + push @artworks, $cd->create_related('artwork', {}); +} + +foreach my $artwork (@artworks) { + $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4); } my @cds_80s = $artist->cds_80s; @@ -98,4 +108,38 @@ is_deeply ( 'last group-entry via self-join works', ); +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'); + +my @artwork_artists = $artwork->artwork_to_artist->all; +foreach (@artwork_artists) { + lives_ok { + my $artista = $_->artist; + my $artistb = $_->artist_test_m2m; + ok($artista->rank < 10 ? $artistb : 1, 'belongs_to with custom rel works.'); + my $artistc = $_->artist_test_m2m_noopt; + 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'); + +@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'); + +@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'); + + done_testing;