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