Trailing WS crusade - got to save them bits
[dbsrgits/DBIx-Class.git] / t / resultset / update_delete.t
index fc535e6..4a9eab4 100644 (file)
@@ -5,9 +5,8 @@ use lib qw(t/lib);
 use Test::More;
 use Test::Exception;
 use DBICTest;
-
-#plan tests => 5;
-plan 'no_plan';
+use DBIC::DebugObj;
+use DBIC::SqlMakerTest;
 
 my $schema = DBICTest->init_schema();
 
@@ -79,17 +78,86 @@ throws_ok (
 );
 
 # grouping on PKs only should pass
-$sub_rs->search ({}, { group_by => [ reverse $sub_rs->result_source->primary_columns ] })     # reverse to make sure the comaprison works
-          ->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
+$sub_rs->search (
+  {},
+  {
+    group_by => [ reverse $sub_rs->result_source->primary_columns ],     # reverse to make sure the PK-list comaprison works
+  },
+)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
 
 is_deeply (
   [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
-            ->get_column ('pilot_sequence')->all 
+            ->get_column ('pilot_sequence')->all
   ],
   [qw/11 21 30 40/],
   'Only two rows incremented',
 );
 
-$sub_rs->delete;
+# also make sure weird scalarref usage works (RT#51409)
+$tkfks->search (
+  \ 'pilot_sequence BETWEEN 11 AND 21',
+)->update ({ pilot_sequence => \ 'pilot_sequence + 1' });
 
+is_deeply (
+  [ $tkfks->search ({ autopilot => [qw/a b x y/]}, { order_by => 'autopilot' })
+            ->get_column ('pilot_sequence')->all
+  ],
+  [qw/12 22 30 40/],
+  'Only two rows incremented (where => scalarref works)',
+);
+
+{
+  my $rs = $schema->resultset('FourKeys_to_TwoKeys')->search (
+    {
+      -or => [
+        { 'me.pilot_sequence' => 12 },
+        { 'me.autopilot'      => 'b' },
+      ],
+    }
+  );
+  lives_ok { $rs->update({ autopilot => 'z' }) }
+    'Update with table name qualifier in -or conditions lives';
+  is_deeply (
+    [ $tkfks->search ({ pilot_sequence => [12, 22]})
+              ->get_column ('autopilot')->all
+    ],
+    [qw/z z/],
+    '... and yields the right data',
+  );
+}
+
+
+$sub_rs->delete;
 is ($tkfks->count, $tkfk_cnt -= 2, 'Only two rows deleted');
+
+# make sure limit-only deletion works
+cmp_ok ($tkfk_cnt, '>', 1, 'More than 1 row left');
+$tkfks->search ({}, { rows => 1 })->delete;
+is ($tkfks->count, $tkfk_cnt -= 1, 'Only one row deleted');
+
+
+# Make sure prefetch is properly stripped too
+# check with sql-equality, as sqlite will accept bad sql just fine
+my ($sql, @bind);
+my $orig_debugobj = $schema->storage->debugobj;
+my $orig_debug = $schema->storage->debug;
+
+$schema->storage->debugobj (DBIC::DebugObj->new (\$sql, \@bind) );
+$schema->storage->debug (1);
+$schema->resultset('CD')->search(
+  { year => { '!=' => 2010 } },
+  { prefetch => 'liner_notes' },
+)->delete;
+
+$schema->storage->debugobj ($orig_debugobj);
+$schema->storage->debug ($orig_debug);
+
+is_same_sql_bind (
+  $sql,
+  \@bind,
+  'DELETE FROM cd WHERE ( cdid IN ( SELECT me.cdid FROM cd me WHERE ( year != ? ) GROUP BY me.cdid ) )',
+  ["'2010'"],
+  'Update on prefetching resultset strips prefetch correctly'
+);
+
+done_testing;