Skip to content

Commit 68ce6c4

Browse files
committed
Add cache key tests
1 parent f9df197 commit 68ce6c4

2 files changed

Lines changed: 131 additions & 1 deletion

File tree

src/Database/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ public function skipFilters(callable $callback, ?array $filters = null): mixed
11411141
/**
11421142
* Get instance filters
11431143
*
1144-
* @return array<string, array{encode: callable, decode: callable}>
1144+
* @return array<string, array{encode: callable, decode: callable, signature: string}>
11451145
*/
11461146
public function getInstanceFilters(): array
11471147
{

tests/unit/CacheKeyTest.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Utopia\Cache\Adapter\None;
7+
use Utopia\Cache\Cache;
8+
use Utopia\Database\Adapter;
9+
use Utopia\Database\Database;
10+
11+
class CacheKeyTest extends TestCase
12+
{
13+
private function createDatabase(array $instanceFilters = []): Database
14+
{
15+
$adapter = $this->createMock(Adapter::class);
16+
$adapter->method('getSupportForHostname')->willReturn(false);
17+
$adapter->method('getTenant')->willReturn(null);
18+
$adapter->method('getNamespace')->willReturn('test');
19+
20+
return new Database($adapter, new Cache(new None()), $instanceFilters);
21+
}
22+
23+
private function getHashKey(Database $db, string $collection = 'col', string $docId = 'doc1'): string
24+
{
25+
[, , $hashKey] = $db->getCacheKeys($collection, $docId);
26+
return $hashKey;
27+
}
28+
29+
public function testSameConfigProducesSameCacheKey(): void
30+
{
31+
$db1 = $this->createDatabase();
32+
$db2 = $this->createDatabase();
33+
34+
$this->assertEquals($this->getHashKey($db1), $this->getHashKey($db2));
35+
}
36+
37+
public function testDifferentSelectsProduceDifferentCacheKeys(): void
38+
{
39+
$db = $this->createDatabase();
40+
41+
[, , $hashA] = $db->getCacheKeys('col', 'doc1', ['name']);
42+
[, , $hashB] = $db->getCacheKeys('col', 'doc1', ['email']);
43+
44+
$this->assertNotEquals($hashA, $hashB);
45+
}
46+
47+
public function testSelectOrderDoesNotAffectCacheKey(): void
48+
{
49+
$db = $this->createDatabase();
50+
51+
[, , $hashA] = $db->getCacheKeys('col', 'doc1', ['name', 'email']);
52+
[, , $hashB] = $db->getCacheKeys('col', 'doc1', ['email', 'name']);
53+
54+
$this->assertEquals($hashA, $hashB);
55+
}
56+
57+
public function testInstanceFilterOverrideProducesDifferentCacheKey(): void
58+
{
59+
$noop = function (mixed $value) {
60+
return $value;
61+
};
62+
63+
$dbDefault = $this->createDatabase();
64+
$dbOverride = $this->createDatabase([
65+
'json' => [
66+
'encode' => $noop,
67+
'decode' => $noop,
68+
],
69+
]);
70+
71+
$this->assertNotEquals(
72+
$this->getHashKey($dbDefault),
73+
$this->getHashKey($dbOverride)
74+
);
75+
}
76+
77+
public function testDifferentInstanceFilterCallablesProduceDifferentCacheKeys(): void
78+
{
79+
$noopA = function (mixed $value) {
80+
return $value;
81+
};
82+
$noopB = function (mixed $value) {
83+
return $value;
84+
};
85+
86+
$dbA = $this->createDatabase([
87+
'myFilter' => [
88+
'encode' => $noopA,
89+
'decode' => $noopA,
90+
],
91+
]);
92+
$dbB = $this->createDatabase([
93+
'myFilter' => [
94+
'encode' => $noopB,
95+
'decode' => $noopB,
96+
],
97+
]);
98+
99+
$this->assertNotEquals(
100+
$this->getHashKey($dbA),
101+
$this->getHashKey($dbB)
102+
);
103+
}
104+
105+
public function testDisabledFiltersProduceDifferentCacheKey(): void
106+
{
107+
$db = $this->createDatabase();
108+
109+
$hashEnabled = $this->getHashKey($db);
110+
111+
$hashDisabled = $db->skipFilters(function () use ($db) {
112+
return $this->getHashKey($db);
113+
}, ['json']);
114+
115+
$this->assertNotEquals($hashEnabled, $hashDisabled);
116+
}
117+
118+
public function testFiltersDisabledEntirelyProducesDifferentCacheKey(): void
119+
{
120+
$db = $this->createDatabase();
121+
122+
$hashEnabled = $this->getHashKey($db);
123+
124+
$db->disableFilters();
125+
$hashDisabled = $this->getHashKey($db);
126+
$db->enableFilters();
127+
128+
$this->assertNotEquals($hashEnabled, $hashDisabled);
129+
}
130+
}

0 commit comments

Comments
 (0)