1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TBolier\RethinkQL\IntegrationTest\Query; |
5
|
|
|
|
6
|
|
|
use TBolier\RethinkQL\Response\Cursor; |
7
|
|
|
use TBolier\RethinkQL\Response\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class FilterTest extends AbstractTableTest |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @throws \Exception |
13
|
|
|
*/ |
14
|
|
|
public function testFilter() |
15
|
|
|
{ |
16
|
|
|
$this->insertDocument(1); |
17
|
|
|
|
18
|
|
|
/** @var Cursor $cursor */ |
19
|
|
|
$cursor = $this->r() |
20
|
|
|
->table('tabletest') |
21
|
|
|
->filter(['title' => 'Test document 1']) |
22
|
|
|
->run(); |
23
|
|
|
|
24
|
|
|
/** @var array $array */ |
25
|
|
|
$array = $cursor->current(); |
26
|
|
|
|
27
|
|
|
$this->assertInstanceOf(\Iterator::class, $cursor); |
28
|
|
|
$this->assertArraySubset(['title' => 'Test document 1'], $array); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function testFilterOnMultipleDocuments() |
35
|
|
|
{ |
36
|
|
|
$this->insertDocument(1); |
37
|
|
|
$this->insertDocument(2); |
38
|
|
|
$this->insertDocument(3); |
39
|
|
|
$this->insertDocument(4); |
40
|
|
|
$this->insertDocument(5); |
41
|
|
|
|
42
|
|
|
/** @var Cursor $cursor */ |
43
|
|
|
$cursor = $this->r() |
44
|
|
|
->table('tabletest') |
45
|
|
|
->filter(['title' => 'Test document 1']) |
46
|
|
|
->run(); |
47
|
|
|
|
48
|
|
|
$this->assertCount(1, $cursor); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return void |
53
|
|
|
* @throws \Exception |
54
|
|
|
*/ |
55
|
|
|
public function testFilterAndIsEmpty(): void |
56
|
|
|
{ |
57
|
|
|
$this->insertDocument(1); |
58
|
|
|
$this->insertDocument('stringId'); |
59
|
|
|
|
60
|
|
|
/** @var ResponseInterface $res */ |
61
|
|
|
$res = $this->r() |
62
|
|
|
->table('tabletest') |
63
|
|
|
->filter(['id' => 1]) |
64
|
|
|
->isEmpty() |
65
|
|
|
->run(); |
66
|
|
|
|
67
|
|
|
$this->assertFalse($res->getData()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void |
72
|
|
|
* @throws \Exception |
73
|
|
|
*/ |
74
|
|
|
public function testFilterAndCount(): void |
75
|
|
|
{ |
76
|
|
|
$this->insertDocument(1); |
77
|
|
|
$this->insertDocument('stringId'); |
78
|
|
|
|
79
|
|
|
/** @var ResponseInterface $res */ |
80
|
|
|
$res = $this->r() |
81
|
|
|
->table('tabletest') |
82
|
|
|
->filter(['description' => 'A document description.']) |
83
|
|
|
->count() |
84
|
|
|
->run(); |
85
|
|
|
|
86
|
|
|
$this->assertEquals(2, $res->getData()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return void |
91
|
|
|
* @throws \Exception |
92
|
|
|
*/ |
93
|
|
|
public function testFilterAndAvg(): void |
94
|
|
|
{ |
95
|
|
|
$this->insertDocumentWithNumber(1, 50); |
96
|
|
|
$this->insertDocumentWithNumber(2, 100); |
97
|
|
|
|
98
|
|
|
/** @var ResponseInterface $res */ |
99
|
|
|
$res = $this->r() |
100
|
|
|
->table('tabletest') |
101
|
|
|
->filter(['description' => 'A document description.']) |
102
|
|
|
->avg('number') |
103
|
|
|
->run(); |
104
|
|
|
|
105
|
|
|
$this->assertEquals(75, $res->getData()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return void |
110
|
|
|
* @throws \Exception |
111
|
|
|
*/ |
112
|
|
|
public function testFilterAndSum(): void |
113
|
|
|
{ |
114
|
|
|
$this->insertDocumentWithNumber(1, 50); |
115
|
|
|
$this->insertDocumentWithNumber(2, 100); |
116
|
|
|
|
117
|
|
|
/** @var ResponseInterface $res */ |
118
|
|
|
$res = $this->r() |
119
|
|
|
->table('tabletest') |
120
|
|
|
->filter(['description' => 'A document description.']) |
121
|
|
|
->sum('number') |
122
|
|
|
->run(); |
123
|
|
|
|
124
|
|
|
$this->assertEquals(150, $res->getData()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return void |
129
|
|
|
* @throws \Exception |
130
|
|
|
*/ |
131
|
|
|
public function testFilterAndLimit(): void |
132
|
|
|
{ |
133
|
|
|
$this->insertDocument(1); |
134
|
|
|
$this->insertDocument('stringId'); |
135
|
|
|
|
136
|
|
|
/** @var ResponseInterface $res */ |
137
|
|
|
$cursor = $this->r() |
138
|
|
|
->table('tabletest') |
139
|
|
|
->filter(['description' => 'A document description.']) |
140
|
|
|
->limit(1) |
141
|
|
|
->run(); |
142
|
|
|
|
143
|
|
|
$this->assertCount(1, $cursor); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return void |
148
|
|
|
* @throws \Exception |
149
|
|
|
*/ |
150
|
|
|
public function testFilterAndSkip(): void |
151
|
|
|
{ |
152
|
|
|
$this->insertDocument(1); |
153
|
|
|
$this->insertDocument('stringId'); |
154
|
|
|
|
155
|
|
|
/** @var ResponseInterface $res */ |
156
|
|
|
$cursor = $this->r() |
157
|
|
|
->table('tabletest') |
158
|
|
|
->filter(['description' => 'A document description.']) |
159
|
|
|
->skip(1) |
160
|
|
|
->run(); |
161
|
|
|
|
162
|
|
|
$this->assertCount(1, $cursor); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return void |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
public function testFilterAndOrderBy(): void |
170
|
|
|
{ |
171
|
|
|
$this->insertDocument(1); |
172
|
|
|
$this->insertDocument('stringId'); |
173
|
|
|
|
174
|
|
|
/** @var ResponseInterface $res */ |
175
|
|
|
$res = $this->r() |
176
|
|
|
->table('tabletest') |
177
|
|
|
->filter(['description' => 'A document description.']) |
178
|
|
|
->orderBy($this->r()->desc('id')) |
179
|
|
|
->run(); |
180
|
|
|
|
181
|
|
|
$this->assertArraySubset(['id' => 'stringId'], $res->getData()[0]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return void |
186
|
|
|
* @throws \Exception |
187
|
|
|
*/ |
188
|
|
|
public function testFilterAndMin(): void |
189
|
|
|
{ |
190
|
|
|
$this->insertDocumentWithNumber(1, 77); |
191
|
|
|
$this->insertDocumentWithNumber(2, 99); |
192
|
|
|
|
193
|
|
|
/** @var ResponseInterface $res */ |
194
|
|
|
$res = $this->r() |
195
|
|
|
->table('tabletest') |
196
|
|
|
->filter(['description' => 'A document description.']) |
197
|
|
|
->min('number') |
198
|
|
|
->run(); |
199
|
|
|
|
200
|
|
|
$this->assertArraySubset(['number' => 77], $res->getData()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return void |
205
|
|
|
* @throws \Exception |
206
|
|
|
*/ |
207
|
|
|
public function testFilterAndMax(): void |
208
|
|
|
{ |
209
|
|
|
$this->insertDocumentWithNumber(1, 77); |
210
|
|
|
$this->insertDocumentWithNumber(2, 99); |
211
|
|
|
|
212
|
|
|
/** @var ResponseInterface $res */ |
213
|
|
|
$res = $this->r() |
214
|
|
|
->table('tabletest') |
215
|
|
|
->filter(['description' => 'A document description.']) |
216
|
|
|
->max('number') |
217
|
|
|
->run(); |
218
|
|
|
|
219
|
|
|
$this->assertArraySubset(['number' => 99], $res->getData()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @throws \Exception |
224
|
|
|
*/ |
225
|
|
|
public function testFilterWithDateGreaterThanLogic(): void |
226
|
|
|
{ |
227
|
|
|
$this->insertDocumentWithDate(1, new \DateTime('-1 days')); |
228
|
|
|
$this->insertDocumentWithDate(2, new \DateTime('+1 days')); |
229
|
|
|
|
230
|
|
|
/** @var ResponseInterface $res */ |
231
|
|
|
$row = $this->r()->row('date')->gt((new \DateTime('now'))->format(\DateTime::ATOM)); |
232
|
|
|
$cursor = $this->r() |
233
|
|
|
->table('tabletest') |
234
|
|
|
->filter($row) |
235
|
|
|
->run(); |
236
|
|
|
|
237
|
|
|
$this->assertCount(1, $cursor); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @throws \Exception |
242
|
|
|
*/ |
243
|
|
|
public function testFilterWithDateLowerThanLogic(): void |
244
|
|
|
{ |
245
|
|
|
$this->insertDocumentWithDate(1, new \DateTime('-1 days')); |
246
|
|
|
$this->insertDocumentWithDate(2, new \DateTime('+1 days')); |
247
|
|
|
|
248
|
|
|
/** @var ResponseInterface $res */ |
249
|
|
|
$row = $this->r()->row('date')->lt((new \DateTime('now'))->format(\DateTime::ATOM)); |
250
|
|
|
$cursor = $this->r() |
251
|
|
|
->table('tabletest') |
252
|
|
|
->filter($row) |
253
|
|
|
->run(); |
254
|
|
|
|
255
|
|
|
$this->assertCount(1, $cursor); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @throws \Exception |
260
|
|
|
*/ |
261
|
|
|
public function testFilterGreaterOrEqualThanLogic(): void |
262
|
|
|
{ |
263
|
|
|
$this->insertDocumentWithNumber(1, 11); |
264
|
|
|
$this->insertDocumentWithNumber(2, 22); |
265
|
|
|
$this->insertDocumentWithNumber(3, 33); |
266
|
|
|
|
267
|
|
|
/** @var ResponseInterface $res */ |
268
|
|
|
$row = $this->r()->row('number')->ge(22); |
269
|
|
|
$cursor = $this->r() |
270
|
|
|
->table('tabletest') |
271
|
|
|
->filter($row) |
272
|
|
|
->run(); |
273
|
|
|
|
274
|
|
|
$this->assertCount(2, $cursor); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @throws \Exception |
279
|
|
|
*/ |
280
|
|
|
public function testFilterLowerOrEqualThanLogic(): void |
281
|
|
|
{ |
282
|
|
|
$this->insertDocumentWithNumber(1, 11); |
283
|
|
|
$this->insertDocumentWithNumber(2, 22); |
284
|
|
|
$this->insertDocumentWithNumber(3, 33); |
285
|
|
|
|
286
|
|
|
/** @var ResponseInterface $res */ |
287
|
|
|
$row = $this->r()->row('number')->le(22); |
288
|
|
|
$cursor = $this->r() |
289
|
|
|
->table('tabletest') |
290
|
|
|
->filter($row) |
291
|
|
|
->run(); |
292
|
|
|
|
293
|
|
|
$this->assertCount(2, $cursor); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @throws \Exception |
298
|
|
|
*/ |
299
|
|
|
public function testFilterWithDateEqualLogic(): void |
300
|
|
|
{ |
301
|
|
|
$this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days')); |
302
|
|
|
$this->insertDocumentWithDate(2, new \DateTime('+1 days')); |
303
|
|
|
|
304
|
|
|
/** @var ResponseInterface $res */ |
305
|
|
|
$row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM)); |
306
|
|
|
$cursor = $this->r() |
307
|
|
|
->table('tabletest') |
308
|
|
|
->filter($row) |
309
|
|
|
->run(); |
310
|
|
|
|
311
|
|
|
$this->assertCount(1, $cursor); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @throws \Exception |
316
|
|
|
*/ |
317
|
|
|
public function testFilterWithDateNotEqualLogic(): void |
318
|
|
|
{ |
319
|
|
|
$this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days')); |
320
|
|
|
$this->insertDocumentWithDate(2, new \DateTime('+1 days')); |
321
|
|
|
|
322
|
|
|
$row = $this->r()->row('date')->ne($yesterday->format(\DateTime::ATOM)); |
323
|
|
|
$cursor = $this->r() |
324
|
|
|
->table('tabletest') |
325
|
|
|
->filter($row) |
326
|
|
|
->run(); |
327
|
|
|
|
328
|
|
|
$this->assertCount(1, $cursor); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @throws \Exception |
333
|
|
|
*/ |
334
|
|
|
public function testFilterWithDateMultipleAndLogic(): void |
335
|
|
|
{ |
336
|
|
|
$this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days')); |
337
|
|
|
$this->insertDocumentWithDate(2, $tomorrow = new \DateTime('+1 days')); |
338
|
|
|
|
339
|
|
|
/** @var ResponseInterface $res */ |
340
|
|
|
$row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM))->and( |
341
|
|
|
$this->r()->row('date')->ne($tomorrow->format(\DateTime::ATOM))->and( |
342
|
|
|
$this->r()->row('id')->eq(1)->and( |
343
|
|
|
$this->r()->row('id')->ne(2) |
344
|
|
|
) |
345
|
|
|
) |
346
|
|
|
); |
347
|
|
|
|
348
|
|
|
$cursor = $this->r() |
349
|
|
|
->table('tabletest') |
350
|
|
|
->filter($row) |
351
|
|
|
->run(); |
352
|
|
|
|
353
|
|
|
$this->assertCount(1, $cursor); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @throws \Exception |
358
|
|
|
*/ |
359
|
|
|
public function testFilterWithDateMultipleOrLogic(): void |
360
|
|
|
{ |
361
|
|
|
$this->insertDocumentWithDate(1, $yesterday = new \DateTime('-1 days')); |
362
|
|
|
$this->insertDocumentWithDate(2, $tomorrow = new \DateTime('+1 days')); |
363
|
|
|
|
364
|
|
|
/** @var ResponseInterface $res */ |
365
|
|
|
$row = $this->r()->row('date')->eq($yesterday->format(\DateTime::ATOM))->or( |
366
|
|
|
$this->r()->row('date')->eq($tomorrow->format(\DateTime::ATOM))->and( |
367
|
|
|
$this->r()->row('id')->gt(0)->or( |
368
|
|
|
$this->r()->row('id')->lt(3) |
369
|
|
|
) |
370
|
|
|
) |
371
|
|
|
); |
372
|
|
|
|
373
|
|
|
$cursor = $this->r() |
374
|
|
|
->table('tabletest') |
375
|
|
|
->filter($row) |
376
|
|
|
->run(); |
377
|
|
|
|
378
|
|
|
$this->assertCount(2, $cursor); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @return void |
383
|
|
|
* @throws \Exception |
384
|
|
|
*/ |
385
|
|
|
public function testNotLogic(): void |
386
|
|
|
{ |
387
|
|
|
$this->insertDocumentWithNumber(1, 55); |
388
|
|
|
$this->insertDocumentWithNumber(2, 77); |
389
|
|
|
$this->insertDocumentWithNumber(3, 99); |
390
|
|
|
$this->insertDocumentWithNumber(4, 111); |
391
|
|
|
|
392
|
|
|
$row = $this->r()->row('number')->eq(77)->not(); |
393
|
|
|
$cursor = $this->r() |
394
|
|
|
->table('tabletest') |
395
|
|
|
->filter($row) |
396
|
|
|
->run(); |
397
|
|
|
|
398
|
|
|
$this->assertCount(3, $cursor); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|