X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Frelationship%2Fupdate_or_create_multi.t;h=5dde83db13ed865e7e93c58ea8bbcc32ca28d885;hb=c5d7250ed959c707f2179ae5e10e8edb27adc92e;hp=885b15cdb5704980f1890cc4f89b9e2e42132b9d;hpb=a2287768f5c3dd665c469d7f9dfe99d369ff6781;p=dbsrgits%2FDBIx-Class.git diff --git a/t/relationship/update_or_create_multi.t b/t/relationship/update_or_create_multi.t index 885b15c..5dde83d 100644 --- a/t/relationship/update_or_create_multi.t +++ b/t/relationship/update_or_create_multi.t @@ -3,21 +3,19 @@ use warnings; use Test::More; use Test::Exception; +use Test::Warn; use lib qw(t/lib); use DBICTest; -use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); -my $sdebug = $schema->storage->debug; -plan tests => 6; - -my $artist = $schema->resultset ('Artist')->first; +my $artist = $schema->resultset ('Artist')->find(1); 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 +25,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 +38,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,31 +58,51 @@ 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/DBI Exception.+(?x: + \QNOT NULL constraint failed: cd.artist\E + | + \Qcd.artist may not be NULL\E +)/s, 'ambiguous find + create failed' +; # expect a create, after a failed search using *only* the # *current* relationship and the unique column constraints # (so no year) -my @sql; -$schema->storage->debugcb(sub { push @sql, $_[1] }); -$schema->storage->debug (1); - -$genre->update_or_create_related ('cds', { - title => 'the best thing since vertical toasters', - artist => $artist, - year => 2012, -}); - -$schema->storage->debugcb(undef); -$schema->storage->debug ($sdebug); - -is_same_sql ( - $sql[0], - 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track - FROM cd me - WHERE ( me.artist = ? AND me.title = ? AND me.genreid = ? ) - ', - 'expected select issued', -); +$schema->is_executed_sql_bind( sub { + $genre->update_or_create_related ('cds', { + title => 'the best thing since vertical toasters', + artist => $artist, + year => 2012, + }); +}, [ + [ + 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track + FROM cd me + WHERE ( me.artist = ? AND me.genreid = ? AND me.title = ? ) + ', + 1, + 2, + "the best thing since vertical toasters", + ], + [ + 'INSERT INTO cd ( artist, genreid, title, year) VALUES ( ?, ?, ?, ? )', + 1, + 2, + "the best thing since vertical toasters", + 2012, + ], +], 'expected select issued' ); # a has_many search without a unique constraint makes no sense # but I am not sure what to test for - leaving open + +done_testing;