Refactor ::DBIHacks::_extract_fixed_condition_columns (sequel to 8d005ad9)
[dbsrgits/DBIx-Class.git] / t / sqlmaker / dbihacks_internals.t
index 81deb89..66f0148 100644 (file)
@@ -5,6 +5,7 @@ use Test::Warn;
 
 use lib qw(t/lib);
 use DBICTest ':DiffSQL';
+use DBIx::Class::_Util 'UNRESOLVABLE_CONDITION';
 
 use Data::Dumper;
 
@@ -30,67 +31,89 @@ for my $t (
     where => { artistid => 1, charfield => undef },
     cc_result => { artistid => 1, charfield => undef },
     sql => 'WHERE artistid = ? AND charfield IS NULL',
-    efcc_result => [qw( artistid )],
+    efcc_result => { artistid => 1 },
+    efcc_n_result => { artistid => 1, charfield => undef },
   },
   {
     where => { -and => [ artistid => 1, charfield => undef, { rank => 13 } ] },
     cc_result => { artistid => 1, charfield => undef, rank => 13 },
     sql => 'WHERE artistid = ?  AND charfield IS NULL AND rank = ?',
-    efcc_result => [qw( artistid rank )],
+    efcc_result => { artistid => 1, rank => 13 },
+    efcc_n_result => { artistid => 1, charfield => undef, rank => 13 },
   },
   {
     where => { -and => [ { artistid => 1, charfield => undef}, { rank => 13 } ] },
     cc_result => { artistid => 1, charfield => undef, rank => 13 },
     sql => 'WHERE artistid = ?  AND charfield IS NULL AND rank = ?',
-    efcc_result => [qw( artistid rank )],
+    efcc_result => { artistid => 1, rank => 13 },
+    efcc_n_result => { artistid => 1, charfield => undef, rank => 13 },
   },
   {
     where => { -and => [ -or => { name => 'Caterwauler McCrae' }, 'rank' ] },
     cc_result => { name => 'Caterwauler McCrae', rank => undef },
     sql => 'WHERE name = ? AND rank IS NULL',
-    efcc_result => [qw( name )],
+    efcc_result => { name => 'Caterwauler McCrae' },
+    efcc_n_result => { name => 'Caterwauler McCrae', rank => undef },
   },
   {
     where => { -and => [ [ [ artist => {'=' => \'foo' } ] ], { name => \[ '= ?', 'bar' ] } ] },
     cc_result => { artist => {'=' => \'foo' }, name => \[ '= ?', 'bar' ] },
     sql => 'WHERE artist = foo AND name = ?',
-    efcc_result => [qw( artist )],
+    efcc_result => { artist => \'foo' },
   },
   {
     where => { -and => [ -or => { name => 'Caterwauler McCrae', artistid => 2 } ] },
     cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] },
     sql => 'WHERE artistid = ? OR name = ?',
-    efcc_result => [],
+    efcc_result => {},
+  },
+  {
+    where => { -or => { name => 'Caterwauler McCrae', artistid => 2 } },
+    cc_result => { -or => [ artistid => 2, name => 'Caterwauler McCrae' ] },
+    sql => 'WHERE artistid = ? OR name = ?',
+    efcc_result => {},
   },
   {
     where => { -and => [ \'foo=bar',  [ { artistid => { '=', $num } } ], { name => 'Caterwauler McCrae'} ] },
     cc_result => { '' => \'foo=bar', name => 'Caterwauler McCrae', artistid => $num },
     sql => 'WHERE foo=bar AND artistid = ? AND name = ?',
-    efcc_result => [qw( artistid name )],
+    efcc_result => { name => 'Caterwauler McCrae', artistid => $num },
   },
   {
     where => { artistid => [ $num ], rank => [ 13, 2, 3 ], charfield => [ undef ] },
     cc_result => { artistid => $num, charfield => undef, rank => [13, 2, 3] },
     sql => 'WHERE artistid = ? AND charfield IS NULL AND ( rank = ? OR rank = ? OR rank = ? )',
-    efcc_result => [qw( artistid )],
+    efcc_result => { artistid => $num },
+    efcc_n_result => { artistid => $num, charfield => undef },
   },
   {
     where => { artistid => { '=' => 1 }, rank => { '>' => 12 }, charfield => { '=' => undef } },
     cc_result => { artistid => 1, charfield => undef, rank => { '>' => 12 } },
     sql => 'WHERE artistid = ? AND charfield IS NULL AND rank > ?',
-    efcc_result => [qw( artistid )],
+    efcc_result => { artistid => 1 },
+    efcc_n_result => { artistid => 1, charfield => undef },
   },
   {
     where => { artistid => { '=' => [ 1 ], }, charfield => { '=' => [-and => \'1', \['?',2] ] }, rank => { '=' => [ $num, $num ] } },
     cc_result => { artistid => 1, charfield => [-and => { '=' => \'1' }, { '=' => \['?',2] } ], rank => { '=' => [$num, $num] } },
     sql => 'WHERE artistid = ? AND charfield = 1 AND charfield = ? AND ( rank = ? OR rank = ? )',
-    efcc_result => [qw( artistid charfield )],
+    efcc_result => { artistid => 1, charfield => UNRESOLVABLE_CONDITION },
   },
   {
     where => { -and => [ artistid => 1, artistid => 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => [ -or => { '=', 2 } ], rank => [-and => undef, { '=', undef }, { '!=', 2 } ] },
     cc_result => { artistid => [ -and => 1, 2 ], name => [ -and => { '!=', 1 }, 2 ], charfield => 2, rank => [ -and => undef, undef, { '!=', 2 } ] },
     sql => 'WHERE artistid = ? AND artistid = ? AND charfield = ? AND name != ? AND name = ? AND rank IS NULL AND rank IS NULL AND rank != ?',
-    efcc_result => [qw( artistid charfield name )],
+    efcc_result => {
+      artistid => UNRESOLVABLE_CONDITION,
+      name => 2,
+      charfield => 2,
+    },
+    efcc_n_result => {
+      artistid => UNRESOLVABLE_CONDITION,
+      name => 2,
+      charfield => 2,
+      rank => undef,
+    },
   },
   {
     where => { -and => [
@@ -106,24 +129,24 @@ for my $t (
       ],
     },
     sql => 'WHERE ( _macro.to LIKE ? OR _wc_macros.to LIKE ? ) AND group.is_active = ? AND me.is_active = ?',
-    efcc_result => [qw( group.is_active me.is_active )],
+    efcc_result => { 'group.is_active' => 1, 'me.is_active' => 1 },
   },
   {
     where => { artistid => [] },
     cc_result => { artistid => [] },
-    efcc_result => [],
+    efcc_result => {},
   },
   (map {
     {
       where => { -and => $_ },
       cc_result => undef,
-      efcc_result => [],
+      efcc_result => {},
       sql => '',
     },
     {
       where => { -or => $_ },
       cc_result => undef,
-      efcc_result => [],
+      efcc_result => {},
       sql => '',
     },
   } (
@@ -138,14 +161,15 @@ for my $t (
   )),
 
   # FIXME legacy compat crap, possibly worth undef/dieing in SQLMaker
-  { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => [] },
+  { where => { artistid => {} }, sql => '', cc_result => undef, efcc_result => {}, efcc_n_result => {} },
 
   # batshit insanity, just to be thorough
   {
     where => { -and => [ [ 'artistid' ], [ -and => [ artistid => { '!=', 69 }, artistid => undef, artistid => { '=' => 200 } ]], artistid => [], { -or => [] }, { -and => [] }, [ 'charfield' ], { name => [] }, 'rank' ] },
     cc_result => { artistid => [ -and => undef, { '!=', 69 }, undef, 200, [] ], charfield => undef, name => [], rank => undef },
     sql => 'WHERE artistid IS NULL AND artistid != ? AND artistid IS NULL AND artistid = ? AND 0=1 AND charfield IS NULL AND 0=1 AND rank IS NULL',
-    efcc_result => [qw( artistid )],
+    efcc_result => { artistid => UNRESOLVABLE_CONDITION },
+    efcc_n_result => { artistid => UNRESOLVABLE_CONDITION, charfield => undef, rank => undef },
   },
 
   # original test from RT#93244
@@ -166,14 +190,21 @@ for my $t (
       'me.title' => 'Spoonful of bees',
     },
     sql => 'WHERE LOWER(me.title) LIKE ? AND me.title = ?',
-    efcc_result => [qw( me.title )],
+    efcc_result => { 'me.title' => 'Spoonful of bees' },
   }
 ) {
 
   for my $w (
     $t->{where},
     [ -and => $t->{where} ],
-    ( keys %{$t->{where}} <= 1 ) ? [ %{$t->{where}} ] : ()
+    ( keys %{$t->{where}} <= 1 ? [ %{$t->{where}} ] : () ),
+    ( (keys %{$t->{where}} == 1 and $t->{where}{-or})
+      ? ( ref $t->{where}{-or} eq 'HASH'
+        ? [ map { $_ => $t->{where}{-or}{$_} } sort keys %{$t->{where}{-or}} ]
+        : $t->{where}{-or}
+      )
+      : ()
+    ),
   ) {
     my $name = do { local ($Data::Dumper::Indent, $Data::Dumper::Terse, $Data::Dumper::Sortkeys) = (0, 1, 1); Dumper $w };
 
@@ -201,6 +232,12 @@ for my $t (
       $t->{efcc_result},
       "Expected fixed_condition produced on $name",
     );
+
+    is_deeply(
+      $schema->storage->_extract_fixed_condition_columns($w, 'consider_nulls'),
+      $t->{efcc_n_result},
+      "Expected fixed_condition including NULLs produced on $name",
+    ) if $t->{efcc_n_result};
   }
 }