1<?php
2require_once('strataquerytest.inc.php');
3
4/**
5 * Tests queries - numeric optional operators.
6 *
7 * @group plugin_strata_optional
8 * @group plugins
9 */
10class query_operators_numeric_optional_test extends Strata_Query_UnitTestCase {
11
12    function setup() {
13        parent::setup();
14    }
15
16    function testGtLtePartiallyNumeric() {
17        $query = array (
18            'type' => 'select',
19            'grouping'=>array(),
20            'group' => array (
21                'type' => 'filter',
22                'lhs' => array (
23                    'type' => 'triple',
24                    'subject' => array (
25                        'type' => 'variable',
26                        'text' => 'p'
27                    ),
28                    'predicate' => array (
29                        'type' => 'literal',
30                        'text' => 'tax rate'
31                    ),
32                    'object' => array (
33                        'type' => 'variable',
34                        'text' => 'tax'
35                    )
36                ),
37                'rhs' => array (
38                    array (
39                        'type' => 'operator',
40                        'lhs' => array (
41                            'type' => 'variable',
42                            'text' => 'tax'
43                        ),
44                        'operator' => '>',
45                        'rhs' => array (
46                            'type' => 'literal',
47                            'text' => '2'
48                        )
49                    ),
50                    array (
51                        'type' => 'operator',
52                        'lhs' => array (
53                            'type' => 'variable',
54                            'text' => 'tax'
55                        ),
56                        'operator' => '<=',
57                        'rhs' => array (
58                            'type' => 'literal',
59                            'text' => '25'
60                        )
61                    )
62            )),
63            'projection' => array (
64                'p',
65                'tax'
66            ),
67            'ordering' => array (
68                array (
69                    'variable' => 'p',
70                    'direction' => 'asc'
71                )
72            )
73        );
74
75        $expected = array (
76            array (
77                'p' => array('person:alice'),
78                'tax' => array('10%')
79            ),
80            array (
81                'p' => array('person:bob'),
82                'tax' => array('25%')
83            )
84        );
85
86        $this->assertQueryResult($query, $expected, 'Partial numeric comparison (first numbers, then text) unsupported');
87    }
88
89    function testGteLtPartiallyNumeric() {
90        $query = array (
91            'type' => 'select',
92            'grouping'=>array(),
93            'group' => array (
94                'type' => 'filter',
95                'lhs' => array (
96                    'type' => 'triple',
97                    'subject' => array (
98                        'type' => 'variable',
99                        'text' => 'p'
100                    ),
101                    'predicate' => array (
102                        'type' => 'literal',
103                        'text' => 'tax rate'
104                    ),
105                    'object' => array (
106                        'type' => 'variable',
107                        'text' => 'tax'
108                    )
109                ),
110                'rhs' => array (
111                    array (
112                        'type' => 'operator',
113                        'lhs' => array (
114                            'type' => 'variable',
115                            'text' => 'tax'
116                        ),
117                        'operator' => '>=',
118                        'rhs' => array (
119                            'type' => 'literal',
120                            'text' => '2'
121                        )
122                    ),
123                    array (
124                        'type' => 'operator',
125                        'lhs' => array (
126                            'type' => 'variable',
127                            'text' => 'tax'
128                        ),
129                        'operator' => '<',
130                        'rhs' => array (
131                            'type' => 'literal',
132                            'text' => '25'
133                        )
134                    )
135            )),
136            'projection' => array (
137                'p',
138                'tax'
139            ),
140            'ordering' => array (
141                array (
142                    'variable' => 'p',
143                    'direction' => 'asc'
144                )
145            )
146        );
147
148        $expected = array (
149            array (
150                'p' => array('person:alice'),
151                'tax' => array('10%')
152            ),
153            array (
154                'p' => array('person:carol'),
155                'tax' => array('2%')
156            )
157        );
158
159        $this->assertQueryResult($query, $expected, 'Partial numeric comparison (first numbers, then text) unsupported');
160    }
161
162    function testGtLteNatural() {
163        $query = array (
164            'type' => 'select',
165            'grouping'=>array(),
166            'group' => array (
167                'type' => 'filter',
168                'lhs' => array (
169                    'type' => 'triple',
170                    'subject' => array (
171                        'type' => 'variable',
172                        'text' => 'p'
173                    ),
174                    'predicate' => array (
175                        'type' => 'literal',
176                        'text' => 'has length'
177                    ),
178                    'object' => array (
179                        'type' => 'variable',
180                        'text' => 'length'
181                    )
182                ),
183                'rhs' => array (
184                    array (
185                        'type' => 'operator',
186                        'lhs' => array (
187                            'type' => 'variable',
188                            'text' => 'length'
189                        ),
190                        'operator' => '>',
191                        'rhs' => array (
192                            'type' => 'literal',
193                            'text' => '4 ft'
194                        )
195                    ),
196                    array (
197                        'type' => 'operator',
198                        'lhs' => array (
199                            'type' => 'variable',
200                            'text' => 'length'
201                        ),
202                        'operator' => '<=',
203                        'rhs' => array (
204                            'type' => 'literal',
205                            'text' => '5 ft 5 in'
206                        )
207                    )
208            )),
209            'projection' => array (
210                'p',
211                'length'
212            ),
213            'ordering' => array (
214                array (
215                    'variable' => 'p',
216                    'direction' => 'asc'
217                )
218            )
219        );
220
221        $expected = array (
222            array (
223                'p' => array('person:alice'),
224                'length' => array('5 ft 5 in')
225            ),
226            array (
227                'p' => array('person:carol'),
228                'length' => array('4 ft 11 in')
229            )
230        );
231
232        $this->assertQueryResult($query, $expected, 'Natural comparison unsupported');
233    }
234
235    function testGteLtNatural() {
236        $query = array (
237            'type' => 'select',
238            'grouping'=>array(),
239            'group' => array (
240                'type' => 'filter',
241                'lhs' => array (
242                    'type' => 'triple',
243                    'subject' => array (
244                        'type' => 'variable',
245                        'text' => 'p'
246                    ),
247                    'predicate' => array (
248                        'type' => 'literal',
249                        'text' => 'has length'
250                    ),
251                    'object' => array (
252                        'type' => 'variable',
253                        'text' => 'length'
254                    )
255                ),
256                'rhs' => array (
257                    array (
258                        'type' => 'operator',
259                        'lhs' => array (
260                            'type' => 'variable',
261                            'text' => 'length'
262                        ),
263                        'operator' => '>=',
264                        'rhs' => array (
265                            'type' => 'literal',
266                            'text' => '4 ft'
267                        )
268                    ),
269                    array (
270                        'type' => 'operator',
271                        'lhs' => array (
272                            'type' => 'variable',
273                            'text' => 'length'
274                        ),
275                        'operator' => '<',
276                        'rhs' => array (
277                            'type' => 'literal',
278                            'text' => '5 ft 10 in'
279                        )
280                    )
281            )),
282            'projection' => array (
283                'p',
284                'length'
285            ),
286            'ordering' => array (
287                array (
288                    'variable' => 'p',
289                    'direction' => 'asc'
290                )
291            )
292        );
293
294        $expected = array (
295            array (
296                'p' => array('person:alice'),
297                'length' => array('5 ft 5 in')
298            ),
299            array (
300                'p' => array('person:carol'),
301                'length' => array('4 ft 11 in')
302            )
303        );
304
305        $this->assertQueryResult($query, $expected, 'Natural comparison unsupported');
306    }
307
308}
309