X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F80unique.t;h=b38022504c49faf06501d0ca38d8bc766ac62d7f;hb=c9c2d15144cf21836b1eb69d37210eb02c99ac1d;hp=2245511d1d90815b96d32e68c7fc72861e3ce2c3;hpb=246fa39d71feecdc126a39d916f34a28c1a554d5;p=dbsrgits%2FDBIx-Class.git diff --git a/t/80unique.t b/t/80unique.t index 2245511..b380225 100644 --- a/t/80unique.t +++ b/t/80unique.t @@ -2,10 +2,10 @@ use strict; use warnings; use Test::More; +use Test::Exception; +use Test::Warn; use lib qw(t/lib); use DBICTest; -use DBIC::SqlMakerTest; -use DBIC::DebugObj; my $schema = DBICTest->init_schema(); @@ -25,6 +25,11 @@ is_deeply( [ qw/primary track_cd_position track_cd_title/ ], 'Track source has three unique constraints' ); +is_deeply( + [ sort $schema->source('Tag')->unique_constraint_names ], + [ qw/primary tagid_cd tagid_cd_tag tags_tagid_tag tags_tagid_tag_cd/ ], + 'Tag source has five unique constraints (from add_unique_constraings)' +); my $artistid = 1; my $title = 'UNIQUE Constraint'; @@ -126,6 +131,12 @@ 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'); +# Add an extra row to potentially confuse the query +$schema->resultset('CD')->create ({ + artist => 2, + title => $title, + year => 2022, +}); my $cd9 = $artist->cds->update_or_create( { cdid => $cd1->cdid, @@ -195,7 +206,7 @@ is($row->baz, 3, 'baz is correct'); { key => 'cd_artist_title' } ); - ok(!$cd1->in_storage, 'CD is not in storage yet after update_or_new'); + is($cd1->in_storage, 0, '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'); @@ -213,23 +224,54 @@ is($row->baz, 3, 'baz is correct'); # make sure the ident condition is assembled sanely { - my $artist = $schema->resultset('Artist')->next; + my $artist = $schema->resultset('Artist')->find(1); + + $schema->is_executed_sql_bind( sub { $artist->discard_changes }, [ + [ + 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?', + [ { dbic_colname => "me.artistid", sqlt_datatype => "integer" } => 1 ], + ] + ], 'Expected query on discard_changes'); +} - my ($sql, @bind); - $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)), - $schema->storage->debug(1); +{ + throws_ok { + eval <<'MOD' or die $@; + package # hide from PAUSE + DBICTest::Schema::UniqueConstraintWarningTest; - $artist->discard_changes; + use base qw/DBIx::Class::Core/; - is_same_sql_bind ( - $sql, - \@bind, - 'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?', - [qw/'1'/], - ); + __PACKAGE__->table('dummy'); - $schema->storage->debug(0); - $schema->storage->debugobj(undef); + __PACKAGE__->add_column(qw/ foo bar /); + + __PACKAGE__->add_unique_constraint( + constraint1 => [qw/ foo /], + constraint2 => [qw/ bar /], + ); + + 1; +MOD + } qr/\Qadd_unique_constraint() does not accept multiple constraints, use add_unique_constraints() instead\E/, + 'add_unique_constraint throws when more than one constraint specified'; } +# make sure NULL is not considered condition-deterministic +my $art_rs = $schema->resultset('Artist')->search({}, { order_by => 'artistid' }); +$art_rs->create ({ artistid => $_ + 640, name => "Outranked $_" }) for (1..2); +warnings_are { + is( + $art_rs->find ({ artistid => 642, rank => 13, charfield => undef })->name, + 'Outranked 2', + 'Correct artist retrieved with find' + ); + + is ( + $art_rs->search({ charfield => undef })->find ({ artistid => 642, rank => 13 })->name, + 'Outranked 2', + 'Correct artist retrieved with find' + ); +} [], 'no warnings'; done_testing; +