Skip to content

Commit cb6c26e

Browse files
committed
Update and fix tests
1 parent 9df09b8 commit cb6c26e

4 files changed

Lines changed: 38 additions & 9 deletions

File tree

docker-compose.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3'
2-
31
services:
42
mariadb:
53
image: mariadb:10.7
@@ -16,6 +14,24 @@ services:
1614
- MYSQL_DATABASE=utopiaTests
1715
- MYSQL_USER=user
1816
- MYSQL_PASSWORD=password
17+
clickhouse:
18+
image: clickhouse/clickhouse-server:25.11-alpine
19+
environment:
20+
- CLICKHOUSE_DB=default
21+
- CLICKHOUSE_USER=default
22+
- CLICKHOUSE_PASSWORD=clickhouse
23+
- CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1
24+
networks:
25+
- usage
26+
ports:
27+
- "8123:8123"
28+
- "9000:9000"
29+
healthcheck:
30+
test: ["CMD", "clickhouse-client", "--host=localhost", "--port=9000", "-q", "SELECT 1"]
31+
interval: 5s
32+
timeout: 3s
33+
retries: 10
34+
start_period: 15s
1935

2036
usage:
2137
container_name: utopia-usage

src/Usage/Adapter/Database.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ public function log(string $metric, int $value, string $period = '1h', array $ta
144144
}
145145

146146
$now = new \DateTime();
147-
$time = $now->format(self::PERIODS[$period]);
147+
$time = $period === 'inf'
148+
? '1000-01-01 00:00:00'
149+
: $now->format(self::PERIODS[$period]);
148150

149151
$this->db->getAuthorization()->skip(function () use ($metric, $value, $period, $time, $tags) {
150152
$this->db->createDocument(self::COLLECTION, new Document([
@@ -171,7 +173,9 @@ public function logBatch(array $metrics): bool
171173
}
172174

173175
$now = new \DateTime();
174-
$time = $now->format(self::PERIODS[$period]);
176+
$time = $period === 'inf'
177+
? '1000-01-01 00:00:00'
178+
: $now->format(self::PERIODS[$period]);
175179

176180
return new Document([
177181
'$permissions' => [],

tests/Usage/Adapter/ClickHouseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ protected function initializeUsage(): void
1515
{
1616
$host = getenv('CLICKHOUSE_HOST') ?: 'clickhouse';
1717
$username = getenv('CLICKHOUSE_USER') ?: 'default';
18-
$password = getenv('CLICKHOUSE_PASSWORD') ?: '';
18+
$password = getenv('CLICKHOUSE_PASSWORD') ?: 'clickhouse';
1919
$port = (int) (getenv('CLICKHOUSE_PORT') ?: 8123);
2020
$secure = (bool) (getenv('CLICKHOUSE_SECURE') ?: false);
2121

22-
if ($host === null) {
22+
if ($host === null || $host === '') {
2323
$this->markTestSkipped('CLICKHOUSE_HOST not set; skipping ClickHouse adapter tests.');
2424
}
2525

tests/Usage/Adapter/DatabaseTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Utopia\Database\Adapter\MariaDB;
1010
use Utopia\Database\Database;
1111
use Utopia\Database\DateTime;
12+
use Utopia\Database\Exception\Duplicate;
1213
use Utopia\Database\Query;
1314
use Utopia\Tests\Usage\UsageBase;
1415
use Utopia\Usage\Adapter\Database as AdapterDatabase;
@@ -34,9 +35,17 @@ protected function initializeUsage(): void
3435

3536
$this->usage = new Usage(new AdapterDatabase($this->database));
3637

37-
// Create database and collection if needed
38-
$this->database->create();
38+
// Create database if missing
39+
if (! $this->database->exists($this->database->getDatabase())) {
40+
$this->database->create();
41+
}
42+
3943
// Always run setup to ensure collection exists
40-
$this->usage->setup();
44+
try {
45+
46+
$this->usage->setup();
47+
} catch (Duplicate $ex) {
48+
// ignore duplicate exception
49+
}
4150
}
4251
}

0 commit comments

Comments
 (0)