X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class.git;a=blobdiff_plain;f=t%2Frelationship%2Fupdate_or_create_multi.t;h=6bb0e860757169e74d817e0697084b3f24c5a40f;hp=56936f85674a21e1c0a7a3d76d76ef47cac22338;hb=b7743dabe3354b4a43954ec44a226dc1c44722ac;hpb=a780a0ff7c99dd9b640f8c29ad0466bd0c907bf3 diff --git a/t/relationship/update_or_create_multi.t b/t/relationship/update_or_create_multi.t index 56936f8..6bb0e86 100644 --- a/t/relationship/update_or_create_multi.t +++ b/t/relationship/update_or_create_multi.t @@ -3,6 +3,7 @@ use warnings; use Test::More; use Test::Exception; +use Test::Warn; use lib qw(t/lib); use DBICTest; use DBIC::SqlMakerTest; @@ -10,14 +11,13 @@ use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); my $sdebug = $schema->storage->debug; -plan tests => 6; - my $artist = $schema->resultset ('Artist')->first; my $genre = $schema->resultset ('Genre') ->create ({ name => 'par excellence' }); +my $genre_cds = $genre->cds; -is ($genre->search_related( 'cds' )->count, 0, 'No cds yet'); +is ($genre_cds->count, 0, 'No cds yet'); # expect a create $genre->update_or_create_related ('cds', { @@ -27,8 +27,8 @@ $genre->update_or_create_related ('cds', { }); # verify cd was inserted ok -is ($genre->search_related( 'cds' )->count, 1, 'One cd'); -my $cd = $genre->find_related ('cds', {}); +is ($genre_cds->count, 1, 'One cd'); +my $cd = $genre_cds->first; is_deeply ( { map { $_, $cd->get_column ($_) } qw/artist year title/ }, { @@ -40,15 +40,16 @@ is_deeply ( ); # expect a year update on the only related row -# (non-qunique column + unique column as disambiguator) +# (non-qunique column + unique column set as disambiguator) $genre->update_or_create_related ('cds', { year => 2010, title => 'the best thing since sliced bread', + artist => 1, }); # re-fetch the cd, verify update is ($genre->search_related( 'cds' )->count, 1, 'Still one cd'); -$cd = $genre->find_related ('cds', {}); +$cd = $genre_cds->first; is_deeply ( { map { $_, $cd->get_column ($_) } qw/artist year title/ }, { @@ -59,6 +60,16 @@ is_deeply ( 'CD year column updated correctly', ); +# expect a failing create: +# the unique constraint is not complete, and there is nothing +# in the database with such a year yet - insertion will fail due +# to missing artist fk +throws_ok { + $genre->update_or_create_related ('cds', { + year => 2020, + title => 'the best thing since sliced bread', + }) +} qr/\Qcd.artist may not be NULL/, 'ambiguous find + create failed'; # expect a create, after a failed search using *only* the # *current* relationship and the unique column constraints @@ -88,3 +99,5 @@ is_same_sql ( # a has_many search without a unique constraint makes no sense # but I am not sure what to test for - leaving open + +done_testing;