X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F96multi_create.t;h=356aae65a9d33c55d51bcce5ac4b21663ddd96dd;hb=8999fbced5ffc23f8d2644bd536a255966956f94;hp=810b6eb68de3a9f150f5869d41c7c7f40346bb3d;hpb=2bc3c81ece67606c69cfb18eaeebb05db706d776;p=dbsrgits%2FDBIx-Class.git diff --git a/t/96multi_create.t b/t/96multi_create.t index 810b6eb..356aae6 100644 --- a/t/96multi_create.t +++ b/t/96multi_create.t @@ -51,6 +51,23 @@ my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3' is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create'); +my $artist2 = $schema->resultset('Artist')->create({ artistid => 1000, + name => 'Fred 3', + cds => [ + { artist => 1000, + title => 'Music to code by', + year => 2007, + }, + ], + cds_unordered => [ + { artist => 1000, + title => 'Music to code by', + year => 2007, + }, + ] + }); + +is($artist2->in_storage, 1, 'artist with duplicate rels inserted okay'); CREATE_RELATED1 :{ @@ -161,3 +178,42 @@ SPECIAL_CASE2: { is($a->name, 'Pink Floyd', 'Artist insertion ok'); is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok'); } + +## Create foreign key col obj including PK +## See test 20 in 66relationships.t +my $new_cd_hashref = { + cdid => 27, + title => 'Boogie Woogie', + year => '2007', + artist => { artistid => 17, name => 'king luke' } + }; + +my $cd = $schema->resultset("CD")->find(1); + +is($cd->artist->id, 1, 'rel okay'); + +my $new_cd = $schema->resultset("CD")->create($new_cd_hashref); +is($new_cd->artist->id, 17, 'new id retained okay'); + + +# Test find or create related functionality +my $new_artist = $schema->resultset("Artist")->create({ artistid => 18, name => 'larry' }); + +eval { + $schema->resultset("CD")->create({ + cdid => 28, + title => 'Boogie Wiggle', + year => '2007', + artist => { artistid => 18, name => 'larry' } + }); +}; +is($@, '', 'new cd created without clash on related artist'); + +# Make sure exceptions from errors in created rels propogate +eval { + my $t = $schema->resultset("Track")->new({}); + $t->cd($t->new_related('cd', { artist => undef } ) ); + $t->{_rel_in_storage} = 0; + $t->insert; +}; +like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");