1<?php
2require_once('strataquerytest.inc.php');
3
4/**
5 * Tests queries - numeric operators.
6 *
7 * @group plugin_strata
8 * @group plugins
9 */
10class query_operators_numeric_test extends Strata_Query_UnitTestCase {
11
12    function setup() {
13        parent::setup();
14    }
15
16    function testGtLte() {
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' => 'is rated'
31                    ),
32                    'object' => array (
33                        'type' => 'variable',
34                        'text' => 'rating'
35                    )
36                ),
37                'rhs' => array (
38                    array (
39                        'type' => 'operator',
40                        'lhs' => array (
41                            'type' => 'variable',
42                            'text' => 'rating'
43                        ),
44                        'operator' => '>',
45                        'rhs' => array (
46                            'type' => 'literal',
47                            'text' => '1'
48                        )
49                    ),
50                    array (
51                        'type' => 'operator',
52                        'lhs' => array (
53                            'type' => 'variable',
54                            'text' => 'rating'
55                        ),
56                        'operator' => '<=',
57                        'rhs' => array (
58                            'type' => 'literal',
59                            'text' => '10'
60                        )
61                    )
62            )),
63            'projection' => array (
64                'p',
65                'rating'
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                'rating' => array('10')
79            ),
80            array (
81                'p' => array('person:bob'),
82                'rating' => array('8')
83            )
84        );
85
86        $this->assertQueryResult($query, $expected);
87    }
88
89    function testGteLt() {
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' => 'is rated'
104                    ),
105                    'object' => array (
106                        'type' => 'variable',
107                        'text' => 'rating'
108                    )
109                ),
110                'rhs' => array (
111                    array (
112                        'type' => 'operator',
113                        'lhs' => array (
114                            'type' => 'variable',
115                            'text' => 'rating'
116                        ),
117                        'operator' => '>=',
118                        'rhs' => array (
119                            'type' => 'literal',
120                            'text' => '1'
121                        )
122                    ),
123                    array (
124                        'type' => 'operator',
125                        'lhs' => array (
126                            'type' => 'variable',
127                            'text' => 'rating'
128                        ),
129                        'operator' => '<',
130                        'rhs' => array (
131                            'type' => 'literal',
132                            'text' => '10'
133                        )
134                    )
135            )),
136            'projection' => array (
137                'p',
138                'rating'
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:bob'),
151                'rating' => array('8')
152            ),
153            array (
154                'p' => array('person:carol'),
155                'rating' => array('1')
156            )
157        );
158
159        $this->assertQueryResult($query, $expected);
160    }
161
162    function testPartiallyNumeric() {
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' => 'tax rate'
177                    ),
178                    'object' => array (
179                        'type' => 'variable',
180                        'text' => 'tax'
181                    )
182                ),
183                'rhs' => array (
184                    array (
185                        'type' => 'operator',
186                        'lhs' => array (
187                            'type' => 'variable',
188                            'text' => 'tax'
189                        ),
190                        'operator' => '>',
191                        'rhs' => array (
192                            'type' => 'literal',
193                            'text' => '2'
194                        )
195                    ),
196                    array (
197                        'type' => 'operator',
198                        'lhs' => array (
199                            'type' => 'variable',
200                            'text' => 'tax'
201                        ),
202                        'operator' => '<=',
203                        'rhs' => array (
204                            'type' => 'literal',
205                            'text' => '15'
206                        )
207                    )
208            )),
209            'projection' => array (
210                'p',
211                'tax'
212            ),
213            'ordering' => array (
214                array (
215                    'variable' => 'p',
216                    'direction' => 'asc'
217                )
218            )
219        );
220
221        // Result might vary depending on database backed, only require that it does not fail
222        $this->assertTrue($this->_triples->queryRelations($query));
223    }
224
225    function testNonNumeric() {
226        $query = array (
227            'type' => 'select',
228            'grouping'=>array(),
229            'group' => array (
230                'type' => 'filter',
231                'lhs' => array (
232                    'type' => 'triple',
233                    'subject' => array (
234                        'type' => 'variable',
235                        'text' => 'p'
236                    ),
237                    'predicate' => array (
238                        'type' => 'literal',
239                        'text' => 'class'
240                    ),
241                    'object' => array (
242                        'type' => 'variable',
243                        'text' => 'person'
244                    )
245                ),
246                'rhs' => array (
247                    array (
248                        'type' => 'operator',
249                        'lhs' => array (
250                            'type' => 'variable',
251                            'text' => 'p'
252                        ),
253                        'operator' => '>',
254                        'rhs' => array (
255                            'type' => 'literal',
256                            'text' => '2'
257                        )
258                    ),
259                    array (
260                        'type' => 'operator',
261                        'lhs' => array (
262                            'type' => 'variable',
263                            'text' => 'p'
264                        ),
265                        'operator' => '<=',
266                        'rhs' => array (
267                            'type' => 'literal',
268                            'text' => '25'
269                        )
270                    )
271            )),
272            'projection' => array (
273                'p'
274            ),
275            'ordering' => array (
276                array (
277                    'variable' => 'p',
278                    'direction' => 'asc'
279                )
280            )
281        );
282
283        // Result might vary depending on database backed, only require that it does not fail
284        $this->assertTrue($this->_triples->queryRelations($query));
285    }
286
287}
288