Merge branch 'gewicht'
[kivitendo-erp.git] / t / controllers / helpers / parse_filter.t
index 179c236..c72c33f 100644 (file)
@@ -1,12 +1,14 @@
 use lib 't';
 
-use Test::More tests => 17;
+use Test::More tests => 23;
 use Test::Deep;
 use Data::Dumper;
 
 use_ok 'Support::TestSetup';
 use_ok 'SL::Controller::Helper::ParseFilter';
 
+use SL::DB::OrderItem;
+
 undef *::any; # Test::Deep exports any (for junctions) and MoreCommon exports any (like in List::Moreutils)
 
 Support::TestSetup::login();
@@ -16,7 +18,7 @@ sub test ($$$;%) {
   my ($filter, $expect, $msg, %params) = @_;
   my $target = delete $params{target};
   my $args = { parse_filter($filter, %params) };
-  my $got  = $args;
+  my $got  = $args; $target ||= '';
      $got = $filter             if $target =~ /filter/;
      $got = $params{launder_to} if $target =~ /launder/;
   cmp_deeply(
@@ -35,10 +37,10 @@ test {
   name => 'Test',
   whut => 'moof',
 }, {
-  query => [ %{{
+  query => bag(
     name => 'Test',
     whut => 'moof'
-  }} ],
+  ),
 }, 'basic test';
 
 test {
@@ -58,7 +60,7 @@ test {
   }
 }, {
   query => [ 'customer.chart.accno' => 'test' ],
-  with_objects => bag( 'customer', 'chart' ),
+  with_objects => bag( 'customer', 'customer.chart' ),
 }, 'nested joins';
 
 test {
@@ -82,7 +84,7 @@ test {
 },
 {
   query => [ 'customer.chart.accno' => { like => '%1200' } ],
-  with_objects => bag('customer', 'chart' ),
+  with_objects => bag('customer', 'customer.chart' ),
 }, 'all together';
 
 
@@ -96,11 +98,11 @@ test {
     },
   },
 }, {
-  'query' => [ %{{
+  'query' => bag(
                'invoice.customer.name'  => 'test',
                'customer.name'          => 'test',
-             }} ],
-  'with_objects' => bag( 'invoice', 'customer' )
+            ),
+  'with_objects' => bag( 'invoice.customer', 'customer', 'invoice' )
 }, 'object in more than one relationship';
 
 test {
@@ -140,7 +142,7 @@ test {
   ]
 }, {
   'sellprice:number' => [ '123,4', '2,34', '0,4' ],
-  'sellprice_number' => [ '123,4', '2,34', '0,4' ],
+  'sellprice_number_' => { '123,4' => 1, '2,34' => 1, '0,4' => 1 },
 }, 'laundering with array', target => 'filter';
 
 my %args = (
@@ -177,3 +179,71 @@ test {
   }
 }, 'deep laundering, check for laundered hash', target => 'launder', launder_to => { };
 
+### bug: sub objects
+
+test {
+  order => {
+    customer => {
+      'name:substr::ilike' => 'test',
+    }
+  }
+}, {
+  query => [ 'order.customer.name' => { ilike => '%test%' } ],
+  with_objects => bag('order.customer', 'order'),
+}, 'sub objects have to retain their prefix';
+
+### class filter dispatch
+#
+test {
+  name => 'Test',
+  whut => 'moof',
+}, {
+  query => bag(
+    name => 'Test',
+    whut => 'moof'
+  ),
+}, 'object test simple', class => 'SL::DB::Manager::Part';
+
+test {
+  'type' => 'assembly',
+}, {
+  query => [
+    'assembly' => 1
+  ],
+}, 'object test without prefix', class => 'SL::DB::Manager::Part';
+
+test {
+  'part.type' => 'assembly',
+}, {
+  query => [
+    'part.assembly' => 1
+  ],
+}, 'object test with prefix', class => 'SL::DB::Manager::OrderItem';
+
+test {
+  'type' => [ 'part', 'assembly' ],
+}, {
+  query => [
+    or => [
+     and => [ or => [ assembly => 0, assembly => undef ],
+              "!inventory_accno_id" => 0,
+              "!inventory_accno_id" => undef,
+     ],
+     assembly => 1,
+    ]
+  ],
+}, 'object test without prefix but complex value', class => 'SL::DB::Manager::Part';
+
+test {
+  'part.type' => [ 'part', 'assembly' ],
+}, {
+  query => [
+    or => [
+     and => [ or => [ 'part.assembly' => 0, 'part.assembly' => undef ],
+              "!part.inventory_accno_id" => 0,
+              "!part.inventory_accno_id" => undef,
+     ],
+     'part.assembly' => 1,
+    ]
+  ],
+}, 'object test with prefix but complex value', class => 'SL::DB::Manager::OrderItem';