Revert workarounds for $@ broken during 5.13.x - mainly 1f870d5a
[dbsrgits/DBIx-Class.git] / t / 80unique.t
index 0e4108b..7f78ef5 100644 (file)
@@ -2,6 +2,8 @@ use strict;
 use warnings;
 
 use Test::More;
+use Test::Exception;
+use Test::Warn;
 use lib qw(t/lib);
 use DBICTest;
 use DBIC::SqlMakerTest;
@@ -25,6 +27,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 +133,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,
@@ -216,6 +229,8 @@ is($row->baz, 3, 'baz is correct');
   my $artist = $schema->resultset('Artist')->next;
 
   my ($sql, @bind);
+  my $old_debugobj = $schema->storage->debugobj;
+  my $old_debug = $schema->storage->debug;
   $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
   $schema->storage->debug(1);
 
@@ -228,8 +243,48 @@ is($row->baz, 3, 'baz is correct');
     [qw/'1'/],
   );
 
-  $schema->storage->debug(0);
-  $schema->storage->debugobj(undef);
+  $schema->storage->debug($old_debug);
+  $schema->storage->debugobj($old_debugobj);
 }
 
+{
+  throws_ok {
+    eval <<'MOD' or die $@;
+      package # hide from PAUSE
+        DBICTest::Schema::UniqueConstraintWarningTest;
+
+      use base qw/DBIx::Class::Core/;
+
+      __PACKAGE__->table('dummy');
+
+      __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;
+