X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F80unique.t;h=8bd7e2cd0189a74a6463b0fec2740cb6ea63b77a;hb=48850f9af126611240b7843d0f7913c96f30f5ba;hp=7d4dd08ba48c1e8d743f96321eb06597c603f6c7;hpb=89034887963b728f979ec9185350061a827db7af;p=dbsrgits%2FDBIx-Class.git diff --git a/t/80unique.t b/t/80unique.t index 7d4dd08..8bd7e2c 100644 --- a/t/80unique.t +++ b/t/80unique.t @@ -7,7 +7,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 43; +plan tests => 49; # Check the defined unique constraints is_deeply( @@ -126,8 +126,9 @@ is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); is($cd8->title, $cd1->title, 'title is correct'); is($cd8->year, $cd1->year, 'year is correct'); -my $cd9 = $artist->update_or_create_related('cds', +my $cd9 = $artist->cds->update_or_create( { + cdid => $cd1->cdid, title => $title, year => 2021, }, @@ -161,7 +162,51 @@ my $row = $schema->resultset('NoPrimaryKey')->update_or_create( }, { key => 'foo_bar' } ); -is(! $row->is_changed, 1, 'update_or_create on table without primary key: row is clean'); + +ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean'); is($row->foo, 1, 'foo is correct'); is($row->bar, 2, 'bar is correct'); is($row->baz, 3, 'baz is correct'); + +# Test a unique condition with extra information in the where attr +{ + my $artist = $schema->resultset('Artist')->find({ artistid => 1 }); + my $cd = $artist->cds->find_or_new( + { + cdid => 1, + title => 'Not The Real Title', + year => 3000, + }, + { key => 'primary' } + ); + + ok($cd->in_storage, 'find correctly grepped the key across a relationship'); + is($cd->cdid, 1, 'cdid is correct'); +} + +# Test update_or_new +{ + my $cd1 = $schema->resultset('CD')->update_or_new( + { + artist => $artistid, + title => "SuperHits $$", + year => 2007, + }, + { key => 'cd_artist_title' } + ); + + ok(!$cd1->in_storage, 'CD is not in storage yet after update_or_new'); + $cd1->insert; + ok($cd1->in_storage, 'CD got added to strage after update_or_new && insert'); + + my $cd2 = $schema->resultset('CD')->update_or_new( + { + artist => $artistid, + title => "SuperHits $$", + year => 2008, + }, + { key => 'cd_artist_title' } + ); + ok($cd2->in_storage, 'Updating year using update_or_new was successful'); + is($cd2->id, $cd1->id, 'Got the same CD using update_or_new'); +} \ No newline at end of file