fix find_related-based queries to correctly grep the unique key
[dbsrgits/DBIx-Class.git] / t / 80unique.t
index ad62f0f..6108f28 100644 (file)
@@ -7,7 +7,7 @@ use DBICTest;
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 39;
+plan tests => 45;
 
 # Check the defined unique constraints
 is_deeply(
@@ -126,8 +126,9 @@ 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');
 
-my $cd9 = $artist->update_or_create_related('cds',
+my $cd9 = $artist->cds->update_or_create(
   {
+    cdid   => $cd1->cdid,
     title  => $title,
     year   => 2021,
   },
@@ -140,7 +141,7 @@ is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
 is($cd9->title, $cd1->title, 'title is correct');
 is($cd9->year, 2021, 'year is correct');
 
-# KNOWN TO FAIL: Table with two unique constraints, and we're satisying one of them
+# Table with two unique constraints, and we're satisying one of them
 my $track = $schema->resultset('Track')->find(
   {
     cd       => 1,
@@ -151,3 +152,34 @@ my $track = $schema->resultset('Track')->find(
 
 is($track->get_column('cd'), 1, 'track cd is correct');
 is($track->get_column('position'), 3, 'track position is correct');
+
+# Test a table with a unique constraint but no primary key
+my $row = $schema->resultset('NoPrimaryKey')->update_or_create(
+  {
+    foo => 1,
+    bar => 2,
+    baz => 3,
+  },
+  { key => 'foo_bar' }
+);
+
+ok(! $row->is_changed, 'update_or_create on table without primary key: row is clean');
+is($row->foo, 1, 'foo is correct');
+is($row->bar, 2, 'bar is correct');
+is($row->baz, 3, 'baz is correct');
+
+# Test a unique condition with extra information in the where attr
+{
+  my $artist = $schema->resultset('Artist')->find({ artistid => 1 });
+  my $cd = $artist->cds->find_or_new(
+    {
+      cdid  => 1,
+      title => 'Not The Real Title',
+      year  => 3000,
+    },
+    { key => 'primary' }
+  );
+
+  ok($cd->in_storage, 'find correctly grepped the key across a relationship');
+  is($cd->cdid, 1, 'cdid is correct');
+}