Port tests and extra operator from master 40f2f231
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
index 126a36d..72bdd6c 100644 (file)
@@ -545,11 +545,87 @@ my @tests = (
               stmt_q => 'SELECT * FROM `test` WHERE ( `Y` = ( MAX( LENGTH( MIN ? ) ) ) )',
               bind   => [[Y => 'x']],
       },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { -in => [] }, b => { -not_in => [] }, c => { -in => 42 } }],
+              stmt => 'SELECT * FROM test WHERE ( 0=1 AND 1=1 AND c IN ( ? ))',
+              stmt_q => 'SELECT * FROM `test` WHERE ( 0=1 AND 1=1 AND `c` IN ( ? ))',
+              bind => [ 42 ],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { -in => [] }, b => { -not_in => [] } }],
+              stmt => 'SELECT * FROM test WHERE ( 0=1 AND 1=1 )',
+              stmt_q => 'SELECT * FROM `test` WHERE ( 0=1 AND 1=1 )',
+              bind => [],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } } ],
+              stmt => 'SELECT * FROM test WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
+              stmt_q => 'SELECT * FROM `test` WHERE ( ( `a` IN ( ? ) OR `a` IS NULL ) AND `b` NOT IN ( ? ) AND `b` IS NOT NULL )',
+              bind => [ 42, 42 ],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { -in => [undef] }, b => { -not_in => [undef] } } ],
+              stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NOT NULL )',
+              stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NOT NULL )',
+              bind => [],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { '=' => undef }, b => { -is => undef }, c => { -like => undef } }],
+              stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL AND c IS NULL )',
+              stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL AND `c` IS NULL )',
+              bind => [],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { '!=' => undef }, b => { -is_not => undef }, c => { -not_like => undef } }],
+              stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT  NULL AND c IS NOT  NULL )',
+              stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT  NULL AND `b` IS NOT  NULL AND `c` IS NOT  NULL )',
+              bind => [],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { IS => undef }, b => { LIKE => undef } }],
+              stmt => 'SELECT * FROM test WHERE ( a IS NULL AND b IS NULL )',
+              stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NULL AND `b` IS NULL )',
+              bind => [],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { 'IS NOT' => undef }, b => { 'NOT LIKE' => undef } }],
+              stmt => 'SELECT * FROM test WHERE ( a IS NOT NULL AND b IS NOT  NULL )',
+              stmt_q => 'SELECT * FROM `test` WHERE ( `a` IS NOT  NULL AND `b` IS NOT  NULL )',
+              bind => [],
+      },
+      {
+              func => 'select',
+              args => ['test', '*', { a => { -in => undef } }],
+              exception_like => qr/Argument passed to the 'IN' operator can not be undefined/,
+      },
 );
 
+# check is( not) => undef
+for my $op ( qw(not is is_not), 'is not' ) {
+  (my $sop = uc $op) =~ s/_/ /gi;
 
-plan tests => scalar(grep { !$_->{warning_like} } @tests) * 2
-            + scalar(grep { $_->{warning_like} } @tests) * 4;
+  $sop = 'IS NOT' if $sop eq 'NOT';
+
+  for my $uc (0, 1) {
+    for my $prefix ('', '-') {
+      push @tests, {
+        func => 'where',
+        args => [{ a => { ($prefix . ($uc ? uc $op : lc $op) ) => undef } }],
+        stmt => "WHERE a $sop NULL",
+        stmt_q => "WHERE `a` $sop NULL",
+        bind => [],
+      };
+    }
+  }
+}
 
 for my $t (@tests) {
   local $"=', ';
@@ -591,6 +667,7 @@ for my $t (@tests) {
               .Dumper($t)."Exception was: $@";
         }
       }
+
       is_same_sql_bind(
         $stmt,
         \@bind,
@@ -600,3 +677,5 @@ for my $t (@tests) {
     }
   }
 }
+
+done_testing;