From: Peter Rabbitson Date: Fri, 3 Jul 2009 13:07:49 +0000 (+0000) Subject: Double an existing might_have test as has_one X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cc9d96d02d84dd73db40b17d77739321fd354465;p=dbsrgits%2FDBIx-Class-Historic.git Double an existing might_have test as has_one --- diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm index ec6ab24..80af1df 100644 --- a/t/lib/DBICTest/Schema/CD.pm +++ b/t/lib/DBICTest/Schema/CD.pm @@ -56,6 +56,7 @@ __PACKAGE__->might_have( { proxy => [ qw/notes/ ] }, ); __PACKAGE__->might_have(artwork => 'DBICTest::Schema::Artwork', 'cd_id'); +__PACKAGE__->has_one(mandatory_artwork => 'DBICTest::Schema::Artwork', 'cd_id'); __PACKAGE__->many_to_many( producers => cd_to_producer => 'producer' ); __PACKAGE__->many_to_many( diff --git a/t/multi_create/multilev_might_have_PKeqFK.t b/t/multi_create/multilev_might_have_PKeqFK.t deleted file mode 100644 index 702ffba..0000000 --- a/t/multi_create/multilev_might_have_PKeqFK.t +++ /dev/null @@ -1,65 +0,0 @@ -use strict; -use warnings; - -use Test::More; -use Test::Exception; -use lib qw(t/lib); -use DBICTest; - -sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} }; - -plan tests => 8; - -my $schema = DBICTest->init_schema(); - -mc_diag (<<'DG'); -* Test a multilevel might-have with a PK == FK in the might_have/has_many table - -CD -> might have -> Artwork - \ - \-> has_many \ - --> Artwork_to_Artist - /-> has_many / - / - Artist -DG - -lives_ok (sub { - my $someartist = $schema->resultset('Artist')->first; - my $cd = $schema->resultset('CD')->create ({ - artist => $someartist, - title => 'Music to code by until the cows come home', - year => 2008, - artwork => { - artwork_to_artist => [ - { artist => { name => 'cowboy joe' } }, - { artist => { name => 'billy the kid' } }, - ], - }, - }); - - isa_ok ($cd, 'DBICTest::CD', 'Main CD object created'); - is ($cd->title, 'Music to code by until the cows come home', 'Correct CD title'); - - my $art_obj = $cd->artwork; - ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object'); - is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object'); - is_deeply ( - [ sort $art_obj->artists->get_column ('name')->all ], - [ 'billy the kid', 'cowboy joe' ], - 'Artists named correctly when queried via object', - ); - - my $artwork = $schema->resultset('Artwork')->search ( - { 'cd.title' => 'Music to code by until the cows come home' }, - { join => 'cd' }, - )->single; - is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search'); - is_deeply ( - [ sort $artwork->artists->get_column ('name')->all ], - [ 'billy the kid', 'cowboy joe' ], - 'Artists named correctly queried via a new search', - ); -}, 'multilevel might-have with a PK == FK in the might_have/has_many table ok'); - -1; diff --git a/t/multi_create/multilev_single_PKeqFK.t b/t/multi_create/multilev_single_PKeqFK.t new file mode 100644 index 0000000..c1c7c1c --- /dev/null +++ b/t/multi_create/multilev_single_PKeqFK.t @@ -0,0 +1,83 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use lib qw(t/lib); +use DBICTest; + +sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} }; + +plan tests => 16; + +my $schema = DBICTest->init_schema(); + +mc_diag (<<'DG'); +* Test a multilevel might-have/has_one with a PK == FK in the mid-table + +CD -> might have -> Artwork + \- has_one -/ \ + \ + \-> has_many \ + --> Artwork_to_Artist + /-> has_many / + / + Artist +DG + +my $rels = { + has_one => 'mandatory_artwork', + might_have => 'artwork', +}; + +my $artist_rs = $schema->resultset('Artist'); + +for my $type (qw/has_one might_have/) { + + my $rel = $rels->{$type}; + + my $cd_title = "Test $type cd"; + my $artist_names = [ map { "Artist via $type $_" } (1, 2) ]; + + my $someartist = $artist_rs->next; + + lives_ok (sub { + my $cd = $schema->resultset('CD')->create ({ + artist => $someartist, + title => $cd_title, + year => 2008, + $rel => { + artwork_to_artist => [ map { + { artist => { name => $_ } } + } (@$artist_names) + ] + }, + }); + + + isa_ok ($cd, 'DBICTest::CD', 'Main CD object created'); + is ($cd->title, $cd_title, 'Correct CD title'); + + my $art_obj = $cd->$rel; + ok ($art_obj->has_column_loaded ('cd_id'), 'PK/FK present on artwork object'); + is ($art_obj->artists->count, 2, 'Correct artwork creator count via the new object'); + is_deeply ( + [ sort $art_obj->artists->get_column ('name')->all ], + $artist_names, + 'Artists named correctly when queried via object', + ); + + my $artwork = $schema->resultset('Artwork')->search ( + { 'cd.title' => $cd_title }, + { join => 'cd' }, + )->single; + is ($artwork->artists->count, 2, 'Correct artwork creator count via a new search'); + is_deeply ( + [ sort $artwork->artists->get_column ('name')->all ], + $artist_names, + 'Artists named correctly queried via a new search', + ); + }, "multilevel $type with a PK == FK in the $type/has_many table ok"); +} + +1;