Skip to content

Commit 8ecb2b8

Browse files
committed
(test): Add tests for aggregations, joins, distinct, union, raw, and query helpers
1 parent d0094ba commit 8ecb2b8

6 files changed

Lines changed: 935 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Tests\Query;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Utopia\Query\Query;
7+
8+
class AggregationQueryTest extends TestCase
9+
{
10+
public function testCountDefaultAttribute(): void
11+
{
12+
$query = Query::count();
13+
$this->assertEquals(Query::TYPE_COUNT, $query->getMethod());
14+
$this->assertEquals('*', $query->getAttribute());
15+
$this->assertEquals([], $query->getValues());
16+
}
17+
18+
public function testCountWithAttribute(): void
19+
{
20+
$query = Query::count('id');
21+
$this->assertEquals(Query::TYPE_COUNT, $query->getMethod());
22+
$this->assertEquals('id', $query->getAttribute());
23+
$this->assertEquals([], $query->getValues());
24+
}
25+
26+
public function testCountWithAlias(): void
27+
{
28+
$query = Query::count('*', 'total');
29+
$this->assertEquals('*', $query->getAttribute());
30+
$this->assertEquals(['total'], $query->getValues());
31+
$this->assertEquals('total', $query->getValue());
32+
}
33+
34+
public function testSum(): void
35+
{
36+
$query = Query::sum('price');
37+
$this->assertEquals(Query::TYPE_SUM, $query->getMethod());
38+
$this->assertEquals('price', $query->getAttribute());
39+
$this->assertEquals([], $query->getValues());
40+
}
41+
42+
public function testSumWithAlias(): void
43+
{
44+
$query = Query::sum('price', 'total_price');
45+
$this->assertEquals(['total_price'], $query->getValues());
46+
}
47+
48+
public function testAvg(): void
49+
{
50+
$query = Query::avg('score');
51+
$this->assertEquals(Query::TYPE_AVG, $query->getMethod());
52+
$this->assertEquals('score', $query->getAttribute());
53+
}
54+
55+
public function testMin(): void
56+
{
57+
$query = Query::min('price');
58+
$this->assertEquals(Query::TYPE_MIN, $query->getMethod());
59+
$this->assertEquals('price', $query->getAttribute());
60+
}
61+
62+
public function testMax(): void
63+
{
64+
$query = Query::max('price');
65+
$this->assertEquals(Query::TYPE_MAX, $query->getMethod());
66+
$this->assertEquals('price', $query->getAttribute());
67+
}
68+
69+
public function testGroupBy(): void
70+
{
71+
$query = Query::groupBy(['status', 'country']);
72+
$this->assertEquals(Query::TYPE_GROUP_BY, $query->getMethod());
73+
$this->assertEquals('', $query->getAttribute());
74+
$this->assertEquals(['status', 'country'], $query->getValues());
75+
}
76+
77+
public function testHaving(): void
78+
{
79+
$inner = [
80+
Query::greaterThan('count', 5),
81+
];
82+
$query = Query::having($inner);
83+
$this->assertEquals(Query::TYPE_HAVING, $query->getMethod());
84+
$this->assertCount(1, $query->getValues());
85+
$this->assertInstanceOf(Query::class, $query->getValues()[0]);
86+
}
87+
88+
public function testAggregateTypesConstant(): void
89+
{
90+
$this->assertContains(Query::TYPE_COUNT, Query::AGGREGATE_TYPES);
91+
$this->assertContains(Query::TYPE_SUM, Query::AGGREGATE_TYPES);
92+
$this->assertContains(Query::TYPE_AVG, Query::AGGREGATE_TYPES);
93+
$this->assertContains(Query::TYPE_MIN, Query::AGGREGATE_TYPES);
94+
$this->assertContains(Query::TYPE_MAX, Query::AGGREGATE_TYPES);
95+
$this->assertCount(5, Query::AGGREGATE_TYPES);
96+
}
97+
}

0 commit comments

Comments
 (0)