Fix test expecting multi-column in to fail on DBD::SQLite
[dbsrgits/DBIx-Class.git] / t / resultset / update_delete.t
index ee32717..7826c94 100644 (file)
@@ -1,10 +1,18 @@
+BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
+
 use strict;
 use warnings;
 
-use lib qw(t/lib);
 use Test::More;
 use Test::Exception;
 
+# MASSIVE FIXME - there is a hole in ::RSC / as_subselect_rs
+# losing the order. Needs a rework/extract of the realiaser,
+# and that's a whole another bag of dicks
+BEGIN { $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} = 0 }
+
+use DBIx::Class::_Util 'scope_guard';
+
 use DBICTest::Schema::CD;
 BEGIN {
   # the default scalarref table name will not work well for this test
@@ -31,7 +39,7 @@ my ($fa, $fb, $fc) = $tkfks->related_resultset ('fourkeys')->populate ([
 #  [qw/2       2  /],
 #]);
 my ($ta, $tb) = $schema->resultset ('TwoKeys')
-                  ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ])
+                  ->search ( [ { artist => 1, cd => 1 }, { artist => 2, cd => 2 } ], { order_by => 'artist' })
                     ->all;
 
 my $tkfk_cnt = $tkfks->count;
@@ -56,9 +64,11 @@ my $fks = $schema->resultset ('FourKeys')->search (
   }, { join => { fourkeys_to_twokeys => 'twokeys' }}
 );
 
+my $read_count = 0;
 is ($fks->count, 4, 'Joined FourKey count correct (2x2)');
 $schema->is_executed_sql_bind( sub {
-  $fks->update ({ read_count => \ 'read_count + 1' })
+  $fks->update ({ read_count => \ 'read_count + 1' });
+  $read_count++;
 }, [[
   'UPDATE fourkeys
    SET read_count = read_count + 1
@@ -68,14 +78,18 @@ $schema->is_executed_sql_bind( sub {
   'c',
 ]], 'Correct update-SQL with multijoin with pruning' );
 
-is ($fa->discard_changes->read_count, 11, 'Update ran only once on discard-join resultset');
-is ($fb->discard_changes->read_count, 21, 'Update ran only once on discard-join resultset');
+is ($fa->discard_changes->read_count, 10 + $read_count, 'Update ran only once on discard-join resultset');
+is ($fb->discard_changes->read_count, 20 + $read_count, 'Update ran only once on discard-join resultset');
 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
 
 # make the multi-join stick
-my $fks_multi = $fks->search({ 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } });
+my $fks_multi = $fks->search(
+  { 'fourkeys_to_twokeys.pilot_sequence' => { '!=' => 666 } },
+  { order_by => [ $fks->result_source->primary_columns ] },
+);
 $schema->is_executed_sql_bind( sub {
-  $fks_multi->update ({ read_count => \ 'read_count + 1' })
+  $fks_multi->update ({ read_count => \ 'read_count + 1' });
+  $read_count++;
 }, [
   [ 'BEGIN' ],
   [
@@ -85,6 +99,7 @@ $schema->is_executed_sql_bind( sub {
         ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
       WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
       GROUP BY me.foo, me.bar, me.hello, me.goodbye
+      ORDER BY foo, bar, hello, goodbye
     ',
     (1, 2) x 2,
     666,
@@ -101,16 +116,50 @@ $schema->is_executed_sql_bind( sub {
   [ 'COMMIT' ],
 ], 'Correct update-SQL with multijoin without pruning' );
 
-is ($fa->discard_changes->read_count, 12, 'Update ran only once on joined resultset');
-is ($fb->discard_changes->read_count, 22, 'Update ran only once on joined resultset');
+is ($fa->discard_changes->read_count, 10 + $read_count, 'Update ran only once on joined resultset');
+is ($fb->discard_changes->read_count, 20 + $read_count, 'Update ran only once on joined resultset');
 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
 
+$schema->is_executed_sql_bind( sub {
+  my $res = $fks_multi->search (\' "blah" = "bleh" ')->delete;
+  ok ($res, 'operation is true');
+  cmp_ok ($res, '==', 0, 'zero rows affected');
+}, [
+  [ 'BEGIN' ],
+  [
+    'SELECT me.foo, me.bar, me.hello, me.goodbye
+      FROM fourkeys me
+      LEFT JOIN fourkeys_to_twokeys fourkeys_to_twokeys
+        ON fourkeys_to_twokeys.f_bar = me.bar AND fourkeys_to_twokeys.f_foo = me.foo AND fourkeys_to_twokeys.f_goodbye = me.goodbye AND fourkeys_to_twokeys.f_hello = me.hello
+      WHERE "blah" = "bleh" AND ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
+      GROUP BY me.foo, me.bar, me.hello, me.goodbye
+      ORDER BY foo, bar, hello, goodbye
+    ',
+    (1, 2) x 2,
+    666,
+    (1, 2) x 2,
+    'c',
+  ],
+  [ 'COMMIT' ],
+], 'Correct null-delete-SQL with multijoin without pruning' );
+
+
 # try the same sql with forced multicolumn in
+my $multicolumn_in = 0;
 $schema->is_executed_sql_bind( sub {
-  local $schema->storage->{_use_multicolumn_in} = 1;
 
-  # this can't actually execute on sqlite
-  eval { $fks_multi->update ({ read_count => \ 'read_count + 1' }) };
+  my $orig_umi = $schema->storage->_use_multicolumn_in;
+  my $sg = scope_guard {
+    $schema->storage->_use_multicolumn_in($orig_umi);
+  };
+
+  $schema->storage->_use_multicolumn_in(1);
+
+  # this can't actually execute on sqlite < 3.14
+  eval {
+    $fks_multi->update ({ read_count => \ 'read_count + 1' });
+    $read_count++; $multicolumn_in++;
+  };
 }, [[
   'UPDATE fourkeys
     SET read_count = read_count + 1
@@ -124,6 +173,7 @@ $schema->is_executed_sql_bind( sub {
             AND fourkeys_to_twokeys.f_goodbye = me.goodbye
             AND fourkeys_to_twokeys.f_hello = me.hello
         WHERE ( bar = ? OR bar = ? ) AND ( foo = ? OR foo = ? ) AND fourkeys_to_twokeys.pilot_sequence != ? AND ( goodbye = ? OR goodbye = ? ) AND ( hello = ? OR hello = ? ) AND sensors != ?
+        ORDER BY foo, bar, hello, goodbye
       )
     )
   ',
@@ -133,8 +183,15 @@ $schema->is_executed_sql_bind( sub {
   'c',
 ]], 'Correct update-SQL with multicolumn in support' );
 
+if ($multicolumn_in) {
+  is ($fa->discard_changes->read_count, 10 + $read_count, 'Update ran only once on joined resultset');
+  is ($fb->discard_changes->read_count, 20 + $read_count, 'Update ran only once on joined resultset');
+  is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
+}
+
 $schema->is_executed_sql_bind( sub {
   $fks->search({ 'twokeys.artist' => { '!=' => 666 } })->update({ read_count => \ 'read_count + 1' });
+  $read_count++;
 }, [
   [ 'BEGIN' ],
   [
@@ -161,8 +218,8 @@ $schema->is_executed_sql_bind( sub {
   [ 'COMMIT' ],
 ], 'Correct update-SQL with premultiplied restricting join without pruning' );
 
-is ($fa->discard_changes->read_count, 13, 'Update ran only once on joined resultset');
-is ($fb->discard_changes->read_count, 23, 'Update ran only once on joined resultset');
+is ($fa->discard_changes->read_count, 10 + $read_count, 'Update ran only once on joined resultset');
+is ($fb->discard_changes->read_count, 20 + $read_count, 'Update ran only once on joined resultset');
 is ($fc->discard_changes->read_count, 30, 'Update did not touch outlier');
 
 #
@@ -246,6 +303,10 @@ 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');
 
+throws_ok {
+  $tkfks->search ({}, { rows => 0 })->delete
+} qr/rows attribute must be a positive integer/;
+is ($tkfks->count, $tkfk_cnt, 'Nothing deleted');
 
 # check with sql-equality, as sqlite will accept most bad sql just fine
 {