Port tests and extra operator from master 40f2f231
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
index 1f305bb..72bdd6c 100644 (file)
@@ -5,6 +5,7 @@ use warnings;
 use Test::More;
 use Test::Warn;
 use Test::Exception;
+use Data::Dumper;
 
 use SQL::Abstract::Test import => ['is_same_sql_bind'];
 
@@ -78,6 +79,13 @@ my @tests = (
       },
       {
               func   => 'select',
+              args   => [[\'test1', 'test2'], '*', { 'test1.a' => { 'In', ['boom', 'bang'] } }],
+              stmt   => 'SELECT * FROM test1, test2 WHERE ( test1.a IN ( ?, ? ) )',
+              stmt_q => 'SELECT * FROM test1, `test2` WHERE ( `test1`.`a` IN ( ?, ? ) )',
+              bind   => ['boom', 'bang']
+      },
+      {
+              func   => 'select',
               args   => ['test', '*', { a => { 'between', ['boom', 'bang'] } }],
               stmt   => 'SELECT * FROM test WHERE ( a BETWEEN ? AND ? )',
               stmt_q => 'SELECT * FROM `test` WHERE ( `a` BETWEEN ? AND ? )',
@@ -537,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 $"=', ';
@@ -578,8 +662,12 @@ for my $t (@tests) {
         );
       }
       else {
-        $cref->();
+        unless (eval { $cref->(); 1 }) {
+          die "Unexpected exception thrown for structure:\n"
+              .Dumper($t)."Exception was: $@";
+        }
       }
+
       is_same_sql_bind(
         $stmt,
         \@bind,
@@ -589,3 +677,5 @@ for my $t (@tests) {
     }
   }
 }
+
+done_testing;