X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F80unique.t;h=6108f289983d6c46a5704d95f522eefbbc7128c9;hb=c1cac6332247a092ddc886c52607b24104c3fb46;hp=65403330e62c5c4803e5866fb6f3aecae1157b04;hpb=368a5228b107faaef1af5d09b0a25ea8bb603421;p=dbsrgits%2FDBIx-Class.git diff --git a/t/80unique.t b/t/80unique.t index 6540333..6108f28 100644 --- a/t/80unique.t +++ b/t/80unique.t @@ -7,10 +7,24 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 36; +plan tests => 45; -is_deeply([ sort $schema->source('CD')->unique_constraint_names ], [ qw/cd_artist_title primary/ ], 'CD source has an automatically named unique constraint'); -is_deeply([ sort $schema->source('Producer')->unique_constraint_names ], [ qw/primary prod_name/ ], 'Producer source has a named unique constraint'); +# Check the defined unique constraints +is_deeply( + [ sort $schema->source('CD')->unique_constraint_names ], + [ qw/cd_artist_title primary/ ], + 'CD source has an automatically named unique constraint' +); +is_deeply( + [ sort $schema->source('Producer')->unique_constraint_names ], + [ qw/primary prod_name/ ], + 'Producer source has a named unique constraint' +); +is_deeply( + [ sort $schema->source('Track')->unique_constraint_names ], + [ qw/primary track_cd_position track_cd_title/ ], + 'Track source has three unique constraints' +); my $artistid = 1; my $title = 'UNIQUE Constraint'; @@ -101,7 +115,6 @@ is($cd7->year, $cd1->year, 'year is correct'); my $artist = $schema->resultset('Artist')->find($artistid); my $cd8 = $artist->find_or_create_related('cds', { - artist => $artistid, title => $title, year => 2020, }, @@ -113,9 +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( { - artist => $artistid, + cdid => $cd1->cdid, title => $title, year => 2021, }, @@ -128,3 +141,45 @@ is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); is($cd9->title, $cd1->title, 'title is correct'); is($cd9->year, 2021, 'year is correct'); +# Table with two unique constraints, and we're satisying one of them +my $track = $schema->resultset('Track')->find( + { + cd => 1, + position => 3, + }, + { order_by => 'position' } +); + +is($track->get_column('cd'), 1, 'track cd is correct'); +is($track->get_column('position'), 3, 'track position is correct'); + +# Test a table with a unique constraint but no primary key +my $row = $schema->resultset('NoPrimaryKey')->update_or_create( + { + foo => 1, + bar => 2, + baz => 3, + }, + { key => 'foo_bar' } +); + +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'); +}