Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / t / controllers / helpers / parse_filter.t
1 use lib 't';
2
3 use Test::More tests => 13;
4 use Test::Deep;
5 use Data::Dumper;
6
7 use_ok 'Support::TestSetup';
8 use_ok 'SL::Controller::Helper::ParseFilter';
9
10 undef *::any; # Test::Deep exports any (for junctions) and MoreCommon exports any (like in List::Moreutils)
11
12 Support::TestSetup::login();
13 my ($filter, $expected);
14
15 sub test ($$$) {
16   my $got = { parse_filter($_[0]) };
17   cmp_deeply(
18     $got,
19     $_[1],
20     $_[2]
21   ) or do {
22     print STDERR "expected => ", Dumper($_[1]), "\ngot: => ", Dumper($got), $/;
23   }
24 }
25
26 test { }, {
27 }, 'minimal test';
28
29 test {
30   name => 'Test',
31   whut => 'moof',
32 }, {
33   query => [ %{{
34     name => 'Test',
35     whut => 'moof'
36   }} ],
37 }, 'basic test';
38
39 test {
40   customer => {
41     name => 'rainer',
42   }
43 }, {
44   query => [ 'customer.name' => 'rainer' ],
45   with_objects => [ 'customer' ],
46 }, 'joining customers';
47
48 test {
49   customer => {
50     chart => {
51       accno => 'test',
52     }
53   }
54 }, {
55   query => [ 'customer.chart.accno' => 'test' ],
56   with_objects => bag( 'customer', 'chart' ),
57 }, 'nested joins';
58
59 test {
60   'customer:substr' => 'Meyer'
61 }, {
62   query => [ customer => '%Meyer%' ]
63 }, 'simple filter substr';
64
65 test {
66   'customer::ilike' => 'Meyer'
67 }, {
68   query => [ customer => { ilike => 'Meyer' } ]
69 }, 'simple method ilike';
70
71 test {
72   customer => {
73     chart => {
74       'accno:tail::like' => '1200'
75     }
76   },
77 },
78 {
79   query => [ 'customer.chart.accno' => { like => '%1200' } ],
80   with_objects => bag('customer', 'chart' ),
81 }, 'all together';
82
83
84 test {
85   customer => {
86     name => 'test',
87   },
88   invoice => {
89     customer => {
90       name => 'test',
91     },
92   },
93 }, {
94   'query' => [ %{{
95                'invoice.customer.name'  => 'test',
96                'customer.name'          => 'test',
97              }} ],
98   'with_objects' => bag( 'invoice', 'customer' )
99 }, 'object in more than one relationship';
100
101 test {
102   'orddate:date::' => 'lt',
103   'orddate:date' => '20.3.2010',
104 }, {
105     'query' => [
106                  'orddate' => { 'lt' => isa('DateTime') }
107                ]
108
109 }, 'method dispatch and date constructor';
110
111 test {
112   id => [
113     123, 125, 157
114   ]
115 }, {
116   query => [ id => [ 123,125,157 ] ],
117 }, 'arrays as value';
118
119 test {
120   'sellprice:number' => [
121     '123,4', '2,34', '0,4',
122   ]
123 }, {
124   query => [
125     sellprice => [ 123.4, 2.34, 0.4 ],
126   ],
127 }, 'arrays with filter';
128