1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace TBolier\RethinkQL\IntegrationTest\Aggregation; |
5
|
|
|
|
6
|
|
|
use TBolier\RethinkQL\IntegrationTest\Query\AbstractTableTest; |
7
|
|
|
use TBolier\RethinkQL\Response\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class GroupTest extends AbstractTableTest |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @return void |
13
|
|
|
* @throws \Exception |
14
|
|
|
*/ |
15
|
|
|
public function testGroupByDescription(): void |
16
|
|
|
{ |
17
|
|
|
$this->insertDocumentWithNumber(5, 10); |
18
|
|
|
$this->insertDocumentWithNumber(4, 10); |
19
|
|
|
$this->insertDocumentWithNumber(3, 20); |
20
|
|
|
$this->insertDocumentWithNumber(2, 20); |
21
|
|
|
$this->insertDocumentWithNumber(1, 30); |
22
|
|
|
|
23
|
|
|
/** @var ResponseInterface $res */ |
24
|
|
|
$res = $this->r() |
25
|
|
|
->table('tabletest') |
26
|
|
|
->group('description') |
27
|
|
|
->run(); |
28
|
|
|
|
29
|
|
|
$this->assertCount(1, $res->getData()); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$this->assertEquals('A document description.', $res->getData()[0]['group']); |
32
|
|
|
$this->assertCount(1, $res->getData()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return void |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
public function testGroupByNumber(): void |
40
|
|
|
{ |
41
|
|
|
$this->insertDocumentWithNumber(5, 10); |
42
|
|
|
$this->insertDocumentWithNumber(4, 10); |
43
|
|
|
$this->insertDocumentWithNumber(3, 20); |
44
|
|
|
$this->insertDocumentWithNumber(2, 20); |
45
|
|
|
$this->insertDocumentWithNumber(1, 30); |
46
|
|
|
|
47
|
|
|
/** @var ResponseInterface $res */ |
48
|
|
|
$res = $this->r() |
49
|
|
|
->table('tabletest') |
50
|
|
|
->group('number') |
51
|
|
|
->run(); |
52
|
|
|
|
53
|
|
|
$this->assertCount(3, $res->getData()); |
|
|
|
|
54
|
|
|
|
55
|
|
|
$this->assertEquals(10, $res->getData()[0]['group']); |
56
|
|
|
$this->assertCount(2, $res->getData()[0]['reduction']); |
57
|
|
|
|
58
|
|
|
$this->assertEquals(20, $res->getData()[1]['group']); |
59
|
|
|
$this->assertCount(2, $res->getData()[1]['reduction']); |
60
|
|
|
|
61
|
|
|
$this->assertEquals(30, $res->getData()[2]['group']); |
62
|
|
|
$this->assertCount(1, $res->getData()[2]['reduction']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return void |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function testGroupAndMaxByNumberReductionByNumberAndUngroupOrderByNumber(): void |
70
|
|
|
{ |
71
|
|
|
$this->insertDocumentWithNumber('Bob', 10); |
72
|
|
|
$this->insertDocumentWithNumber('Alice', 20); |
73
|
|
|
$this->insertDocumentWithNumber('Bob', 30); |
74
|
|
|
$this->insertDocumentWithNumber('Alice', 40); |
75
|
|
|
$this->insertDocumentWithNumber('Harry', 50); |
76
|
|
|
$this->insertDocumentWithNumber('Harry', 45); |
77
|
|
|
|
78
|
|
|
/** @var ResponseInterface $res */ |
79
|
|
|
$res = $this->r() |
80
|
|
|
->table('tabletest') |
81
|
|
|
->group('id')->max('number')->getField('number') |
82
|
|
|
->ungroup() |
83
|
|
|
->orderBy($this->r()->desc('number')) |
84
|
|
|
->run(); |
85
|
|
|
|
86
|
|
|
$this->assertCount(3, $res->getData()); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$this->assertEquals('Alice', $res->getData()[0]['group']); |
89
|
|
|
$this->assertEquals(20, $res->getData()[0]['reduction']); |
90
|
|
|
|
91
|
|
|
$this->assertEquals('Bob', $res->getData()[1]['group']); |
92
|
|
|
$this->assertEquals(10, $res->getData()[1]['reduction']); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('Harry', $res->getData()[2]['group']); |
95
|
|
|
$this->assertEquals(50, $res->getData()[2]['reduction']); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|