-
Notifications
You must be signed in to change notification settings - Fork 30
/
IndexingTest.php
204 lines (163 loc) Β· 5.81 KB
/
IndexingTest.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
declare(strict_types=1);
namespace Fuse\Test;
use PHPUnit\Framework\TestCase;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Fuse\Fuse;
use ReflectionObject;
class IndexingTest extends TestCase
{
use ArraySubsetAsserts;
private static array $options = [
'useExtendedSearch' => true,
'includeMatches' => true,
'includeScore' => true,
'threshold' => 0.3,
'keys' => ['title', 'author.firstName', 'author.lastName'],
];
private static array $books;
public static function setUpBeforeClass(): void
{
static::$books = require __DIR__ . '/fixtures/books.php';
}
private function idx(array $results): array
{
return array_map(fn(array $result) => $result['refIndex'], $results);
}
/**
* @return array[]
*
* @psalm-return array<array{0: mixed, 1: mixed}>
*/
private function idxMap(Fuse $fuse): array
{
return array_map(fn(array $item) => [$item['v'], $item['i']], $fuse->getIndex()->records);
}
public function testCreateIndexEnsurePropertiesExist(): void
{
$myIndex = Fuse::createIndex(static::$options['keys'], static::$books);
$this->assertNotNull($myIndex->records);
$this->assertNotNull($myIndex->keys);
}
public function testCreateIndexEnsureKeysCanBeCreatedWithObjects(): void
{
$myIndex = Fuse::createIndex(
[
[
'name' => 'title',
],
[
'name' => 'author.firstName',
],
],
static::$books,
);
$this->assertNotNull($myIndex->records);
$this->assertNotNull($myIndex->keys);
}
public function testCreateIndexEnsureKeysCanBeCreatedWithGetFn(): void
{
$myIndex = Fuse::createIndex(
[
[
'name' => 'title',
'getFn' => fn($book) => $book['title'],
],
[
'name' => 'author.firstName',
'getFn' => fn($book) => $book['author']['firstName'],
],
],
static::$books,
);
$data = json_decode(json_encode($myIndex), true);
$this->assertNotNull($data['records']);
$this->assertNotNull($data['keys']);
}
public function testParseIndexEnsureIndexCanBeExportedAndFuseCanBeInitialized(): void
{
$myIndex = Fuse::createIndex(static::$options['keys'], static::$books);
$this->assertSame(sizeof(static::$books), $myIndex->size());
$data = json_decode(json_encode($myIndex), true);
$this->assertNotNull($data['records']);
$this->assertNotNull($data['keys']);
$parsedIndex = Fuse::parseIndex($data);
$this->assertSame(sizeof(static::$books), $parsedIndex->size());
}
public function testParseIndexSearchWithGetFn(): void
{
$fuse = new Fuse(static::$books, [
'useExtendedSearch' => true,
'includeMatches' => true,
'includeScore' => true,
'threshold' => 0.3,
'keys' => [
[
'name' => 'bookTitle',
'getFn' => fn($book) => $book['title'],
],
[
'name' => 'authorName',
'getFn' => fn($book) => $book['author']['firstName'],
],
],
]);
$result = $fuse->search([
'bookTitle' => 'old man',
]);
$this->assertCount(1, $result);
$this->assertArraySubset([0], $this->idx($result));
}
public function testFuseCanBeInstantiatedWithAnIndex(): void
{
$myIndex = Fuse::createIndex(static::$options['keys'], static::$books);
$fuse = new Fuse(static::$books, static::$options, $myIndex);
$result = $fuse->search([
'title' => 'old man',
]);
$this->assertCount(1, $result);
$this->assertArraySubset([0], $this->idx($result));
$this->assertArraySubset([[0, 2], [4, 6]], $result[0]['matches'][0]['indices']);
}
public function testAddObjectToIndex(): void
{
$fuse = new Fuse(static::$books, static::$options);
$fuse->add([
'title' => 'book',
'author' => [
'firstName' => 'Kiro',
'lastName' => 'Risk',
],
]);
$result = $fuse->search('kiro');
$this->assertCount(1, $result);
$this->assertArraySubset([sizeof(static::$books)], $this->idx($result));
}
public function testAddStringToIndex(): void
{
$fuse = new Fuse(['apple', 'orange'], ['includeScore' => true]);
$fuse->add('banana');
$result = $fuse->search('banana');
$this->assertCount(1, $result);
$this->assertArraySubset([2], $this->idx($result));
}
public function testRemoveStringFromTheIndex(): void
{
$fuse = new Fuse(['apple', 'orange', 'banana', 'pear']);
$this->assertSame(4, $fuse->getIndex()->size());
$this->assertArraySubset(
[['apple', 0], ['orange', 1], ['banana', 2], ['pear', 3]],
$this->idxMap($fuse),
);
$fuse->removeAt(1);
$this->assertSame(3, $fuse->getIndex()->size());
$this->assertArraySubset([['apple', 0], ['banana', 1], ['pear', 2]], $this->idxMap($fuse));
$results = $fuse->remove(fn($doc) => $doc === 'banana' || $doc === 'pear');
$this->assertCount(2, $results);
$this->assertSame(1, $fuse->getIndex()->size());
$fuseReflection = new ReflectionObject($fuse);
$docsProperty = $fuseReflection->getProperty('docs');
$docsProperty->setAccessible(true);
$this->assertCount(1, $docsProperty->getValue($fuse));
}
}