Port tests and extra operator from master 40f2f231
[dbsrgits/SQL-Abstract.git] / t / 01generate.t
index f015d27..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 ? )',
@@ -333,13 +341,12 @@ my @tests = (
               stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
               bind   => ['02/02/02', 8],
       },
-      { #TODO in SQLA >= 2.0 it will die instead (we kept this just because old SQLA passed it through)
+      {
               func   => 'insert',
-              args   => ['test', {a => 1, b => 2, c => 3, d => 4, e => { answer => 42 }}],
-              stmt   => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ?)',
-              stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ?)',
-              bind   => [qw/1 2 3 4/, { answer => 42}],
-              warning_like => qr/HASH ref as bind value in insert is not supported/i,
+              args   => ['test', {a => 1, b => 2, c => 3, d => 4, e => { -answer => 42 }}],
+              stmt   => 'INSERT INTO test (a, b, c, d, e) VALUES (?, ?, ?, ?, ANSWER(?))',
+              stmt_q => 'INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES (?, ?, ?, ?, ANSWER(?))',
+              bind   => [qw/1 2 3 4 42/],
       },
       {
               func   => 'update',
@@ -451,8 +458,8 @@ my @tests = (
               func   => 'update',
               new    => {bindtype => 'columns'},
               args   => ['test', {a => 1, b => \["to_date(?, 'MM/DD/YY')", [{dummy => 1} => '02/02/02']], c => { -lower => 'foo' }}, {a => {'between', [1,2]}}],
-              stmt   => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER ? WHERE ( a BETWEEN ? AND ? )",
-              stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER ? WHERE ( `a` BETWEEN ? AND ? )",
+              stmt   => "UPDATE test SET a = ?, b = to_date(?, 'MM/DD/YY'), c = LOWER(?) WHERE ( a BETWEEN ? AND ? )",
+              stmt_q => "UPDATE `test` SET `a` = ?, `b` = to_date(?, 'MM/DD/YY'), `c` = LOWER(?) WHERE ( `a` BETWEEN ? AND ? )",
               bind   => [[a => '1'], [{dummy => 1} => '02/02/02'], [c => 'foo'], [a => '1'], [a => '2']],
       },
       {
@@ -545,9 +552,81 @@ my @tests = (
               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;
+
+  $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 $"=', ';
 
@@ -583,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,