Fix find() with an explicit constraint name (... { key => $cname } )
[dbsrgits/DBIx-Class.git] / t / 80unique.t
index 6108f28..2264f61 100644 (file)
@@ -1,14 +1,15 @@
 use strict;
-use warnings;  
+use warnings;
 
 use Test::More;
+use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
+use DBIC::SqlMakerTest;
+use DBIC::DebugObj;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 45;
-
 # Check the defined unique constraints
 is_deeply(
   [ sort $schema->source('CD')->unique_constraint_names ],
@@ -25,6 +26,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 +132,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,
@@ -183,3 +195,78 @@ is($row->baz, 3, 'baz is correct');
   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' }
+    );
+
+    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');
+
+    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');
+}
+
+# make sure the ident condition is assembled sanely
+{
+  my $artist = $schema->resultset('Artist')->next;
+
+  my ($sql, @bind);
+  $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)),
+  $schema->storage->debug(1);
+
+  $artist->discard_changes;
+
+  is_same_sql_bind (
+    $sql,
+    \@bind,
+    'SELECT me.artistid, me.name, me.rank, me.charfield FROM artist me WHERE me.artistid = ?',
+    [qw/'1'/],
+  );
+
+  $schema->storage->debug(0);
+  $schema->storage->debugobj(undef);
+}
+
+{
+  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';
+}
+
+
+done_testing;
+