Commit | Line | Data |
d70ca130 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use SQL::Abstract::AST::Compat; |
5 | |
00f4d23e |
6 | use Test::More tests => 12; |
d70ca130 |
7 | use Test::Differences; |
8 | |
9 | ok(my $visitor = SQL::Abstract::AST::Compat->new); |
10 | |
00f4d23e |
11 | |
c6039348 |
12 | my $foo_id = { -type => 'name', args => [qw/foo/] }; |
13 | my $bar_id = { -type => 'name', args => [qw/bar/] }; |
14 | |
00f4d23e |
15 | my $foo_eq_1 = field_op_value($foo_id, '==', 1); |
16 | my $bar_eq_str = field_op_value($bar_id, '==', 'some str'); |
d70ca130 |
17 | |
18 | eq_or_diff |
37716782 |
19 | $visitor->recurse_where({ foo => 1 }), |
d70ca130 |
20 | $foo_eq_1, |
21 | "Single value hash"; |
22 | |
23 | |
d70ca130 |
24 | |
25 | eq_or_diff |
37716782 |
26 | $visitor->recurse_where({ foo => 1, bar => 'some str' }), |
d70ca130 |
27 | { -type => 'expr', |
28 | op => 'and', |
29 | args => [ |
30 | $bar_eq_str, |
31 | $foo_eq_1, |
32 | ] |
33 | }, |
34 | "two keys in hash"; |
35 | |
36 | eq_or_diff |
37716782 |
37 | $visitor->recurse_where({ -or => { foo => 1, bar => 'some str' } }), |
d70ca130 |
38 | { -type => 'expr', |
39 | op => 'or', |
40 | args => [ |
41 | $bar_eq_str, |
42 | $foo_eq_1, |
43 | ] |
44 | }, |
45 | "-or key in hash"; |
46 | |
47 | |
48 | eq_or_diff |
37716782 |
49 | $visitor->recurse_where([ -and => { foo => 1, bar => 'some str' } ]), |
d70ca130 |
50 | { -type => 'expr', |
51 | op => 'and', |
52 | args => [ |
53 | $bar_eq_str, |
54 | $foo_eq_1, |
55 | ] |
56 | }, |
57 | "-and as first element of array"; |
58 | |
59 | |
60 | eq_or_diff |
37716782 |
61 | $visitor->recurse_where([ -and => { foo => 1, bar => 'some str' }, { foo => 1} ]), |
d70ca130 |
62 | { -type => 'expr', |
63 | op => 'or', |
64 | args => [ |
65 | { -type => 'expr', |
66 | op => 'and', |
67 | args => [ |
68 | $bar_eq_str, |
69 | $foo_eq_1, |
70 | ] |
71 | }, |
72 | $foo_eq_1, |
73 | ] |
74 | }, |
c6039348 |
75 | "-and as first element of array + hash"; |
76 | |
77 | eq_or_diff |
37716782 |
78 | $visitor->recurse_where({ foo => { '!=' => 'bar' } }), |
00f4d23e |
79 | field_op_value($foo_id, '!=', 'bar'), |
c6039348 |
80 | "foo => { '!=' => 'bar' }"; |
81 | |
82 | eq_or_diff |
37716782 |
83 | $visitor->recurse_where({ foo => [ 1, 'bar' ] }), |
c6039348 |
84 | { -type => 'expr', |
85 | op => 'or', |
86 | args => [ |
87 | $foo_eq_1, |
00f4d23e |
88 | field_op_value($foo_id, '==', 'bar'), |
c6039348 |
89 | ], |
90 | }, |
91 | "foo => [ 1, 'bar' ]"; |
92 | |
2e828b0b |
93 | eq_or_diff |
37716782 |
94 | $visitor->recurse_where({ foo => { -in => [ 1, 'bar' ] } }), |
2e828b0b |
95 | { -type => 'expr', |
96 | op => 'in', |
97 | args => [ |
98 | $foo_id, |
99 | { -type => 'value', value => 1 }, |
100 | { -type => 'value', value => 'bar' }, |
101 | ] |
102 | }, |
103 | "foo => { -in => [ 1, 'bar' ] }"; |
104 | |
105 | eq_or_diff |
37716782 |
106 | $visitor->recurse_where({ foo => { -not_in => [ 1, 'bar' ] } }), |
2e828b0b |
107 | { -type => 'expr', |
108 | op => 'not_in', |
109 | args => [ |
110 | $foo_id, |
111 | { -type => 'value', value => 1 }, |
112 | { -type => 'value', value => 'bar' }, |
113 | ] |
114 | }, |
115 | "foo => { -not_in => [ 1, 'bar' ] }"; |
116 | |
117 | eq_or_diff |
37716782 |
118 | $visitor->recurse_where({ foo => { -in => [ ] } }), |
2e828b0b |
119 | { -type => 'expr', |
120 | op => 'in', |
121 | args => [ |
122 | $foo_id, |
123 | ] |
124 | }, |
125 | "foo => { -in => [ ] }"; |
126 | |
00f4d23e |
127 | my $worker_eq = sub { |
128 | return { |
129 | -type => 'expr', |
130 | op => '==', |
131 | args => [ |
132 | { -type => 'name', args => ['worker'] }, |
133 | { -type => 'value', value => $_[0] }, |
134 | ], |
135 | } |
136 | }; |
137 | eq_or_diff |
37716782 |
138 | $visitor->recurse_where( { |
00f4d23e |
139 | requestor => 'inna', |
140 | worker => ['nwiger', 'rcwe', 'sfz'], |
141 | status => { '!=', 'completed' } |
142 | } ), |
143 | { -type => 'expr', |
144 | op => 'and', |
145 | args => [ |
146 | field_op_value(qw/status != completed/), |
147 | { -type => 'expr', |
148 | op => 'or', |
149 | args => [ |
150 | field_op_value(qw/worker == nwiger/), |
151 | field_op_value(qw/worker == rcwe/), |
152 | field_op_value(qw/worker == sfz/), |
153 | ] |
154 | }, |
155 | field_op_value(qw/requestor == inna/), |
156 | ] |
157 | }, |
158 | "complex expr #1"; |
159 | |
160 | |
161 | |
162 | sub field_op_value { |
163 | my ($field, $op, $value) = @_; |
164 | |
165 | $field = ref $field eq 'HASH' |
166 | ? $field |
167 | : ref $field eq 'ARRAY' |
168 | ? { -type => 'name', args => $field } |
169 | : { -type => 'name', args => [$field] }; |
170 | |
171 | $value = ref $value eq 'HASH' |
172 | ? $value |
173 | : { -type => 'value', value => $value }; |
174 | |
175 | return { |
176 | -type => 'expr', |
177 | op => $op, |
178 | args => [ |
179 | $field, |
180 | $value |
181 | ] |
182 | }; |
183 | } |