X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F80unique.t;h=8bd7e2cd0189a74a6463b0fec2740cb6ea63b77a;hb=67e894d2d14f9c10f363646c567cd465cb4b1f6f;hp=30767d24bccfd5aae8bf66ffc7694cef8fa977bd;hpb=a47e123334d8bcea0d34dc9ea09738d6f3b1fd49;p=dbsrgits%2FDBIx-Class.git diff --git a/t/80unique.t b/t/80unique.t index 30767d2..8bd7e2c 100644 --- a/t/80unique.t +++ b/t/80unique.t @@ -7,7 +7,24 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 34; +plan tests => 49; + +# 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'; @@ -23,14 +40,14 @@ my $cd2 = $schema->resultset('CD')->find( artist => $artistid, title => $title, }, - { key => 'artist_title' } + { key => 'cd_artist_title' } ); is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct'); is($cd2->title, $cd1->title, 'title is correct'); is($cd2->year, $cd1->year, 'year is correct'); -my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'artist_title' }); +my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' }); is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct'); is($cd3->title, $cd1->title, 'title is correct'); @@ -56,7 +73,7 @@ my $cd5 = $schema->resultset('CD')->update_or_create( title => $title, year => 2007, }, - { key => 'artist_title' } + { key => 'cd_artist_title' } ); ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean'); @@ -87,7 +104,7 @@ my $cd7 = $schema->resultset('CD')->find_or_create( title => $title, year => 2010, }, - { key => 'artist_title' } + { key => 'cd_artist_title' } ); is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct'); @@ -98,11 +115,10 @@ 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, }, - { key => 'artist_title' } + { key => 'cd_artist_title' } ); is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct'); @@ -110,13 +126,13 @@ 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, }, - { key => 'artist_title' } + { key => 'cd_artist_title' } ); ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean'); @@ -125,3 +141,72 @@ 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'); +} + +# 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