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
|
|
|
|