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