-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathIndexingTest.php
More file actions
147 lines (111 loc) Β· 4.53 KB
/
IndexingTest.php
File metadata and controls
147 lines (111 loc) Β· 4.53 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
use Fuse\Fuse;
beforeEach(function () {
$books = require __DIR__ . '/fixtures/books.php';
$this->books = $books;
$this->fuseOptions = [
'useExtendedSearch' => true,
'includeMatches' => true,
'includeScore' => true,
'threshold' => 0.3,
'keys' => ['title', 'author.firstName', 'author.lastName'],
];
$this->idx = function (array $results): array {
return array_map(fn(array $result) => $result['refIndex'], $results);
};
$this->idxMap = function (Fuse $fuse): array {
return array_map(fn(array $item) => [$item['v'], $item['i']], $fuse->getIndex()->records);
};
});
it('creates index and ensures properties exist', function () {
$myIndex = Fuse::createIndex($this->fuseOptions['keys'], $this->books);
expect($myIndex->records)->not->toBeNull();
expect($myIndex->keys)->not->toBeNull();
});
it('creates index and ensures keys can be created with objects', function () {
$myIndex = Fuse::createIndex(
[['name' => 'title'], ['name' => 'author.firstName']],
$this->books,
);
expect($myIndex->records)->not->toBeNull();
expect($myIndex->keys)->not->toBeNull();
});
it('creates index and ensures keys can be created with getFn', function () {
$myIndex = Fuse::createIndex(
[
['name' => 'title', 'getFn' => fn($book) => $book['title']],
['name' => 'author.firstName', 'getFn' => fn($book) => $book['author']['firstName']],
],
$this->books,
);
$data = json_decode(json_encode($myIndex), true);
expect($data['records'])->not->toBeNull();
expect($data['keys'])->not->toBeNull();
});
it('parses index, exports it, and initializes Fuse', function () {
$myIndex = Fuse::createIndex($this->fuseOptions['keys'], $this->books);
expect($myIndex->size())->toBe(count($this->books));
$data = json_decode(json_encode($myIndex), true);
expect($data['records'])->not->toBeNull();
expect($data['keys'])->not->toBeNull();
$parsedIndex = Fuse::parseIndex($data);
expect($parsedIndex->size())->toBe(count($this->books));
});
it('searches parsed index using getFn', function () {
$fuse = new Fuse($this->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']);
expect($result)->toHaveCount(1);
expect(($this->idx)($result))->toContain(0);
});
it('instantiates Fuse with an index', function () {
$myIndex = Fuse::createIndex($this->fuseOptions['keys'], $this->books);
$fuse = new Fuse($this->books, $this->fuseOptions, $myIndex);
$result = $fuse->search(['title' => 'old man']);
expect($result)->toHaveCount(1);
expect(($this->idx)($result))->toContain(0);
expect($result[0]['matches'][0]['indices'])->toContain([0, 2], [4, 6]);
});
it('adds an object to the index', function () {
$fuse = new Fuse($this->books, $this->fuseOptions);
$fuse->add(['title' => 'book', 'author' => ['firstName' => 'Kiro', 'lastName' => 'Risk']]);
$result = $fuse->search('kiro');
expect($result)->toHaveCount(1);
expect(($this->idx)($result))->toContain(count($this->books));
});
it('adds a string to the index', function () {
$fuse = new Fuse(['apple', 'orange'], ['includeScore' => true]);
$fuse->add('banana');
$result = $fuse->search('banana');
expect($result)->toHaveCount(1);
expect(($this->idx)($result))->toContain(2);
});
it('removes a string from the index', function () {
$fuse = new Fuse(['apple', 'orange', 'banana', 'pear']);
expect($fuse->getIndex()->size())->toBe(4);
expect(($this->idxMap)($fuse))->toContain(
['apple', 0],
['orange', 1],
['banana', 2],
['pear', 3],
);
$fuse->removeAt(1);
expect($fuse->getIndex()->size())->toBe(3);
expect(($this->idxMap)($fuse))->toContain(['apple', 0], ['banana', 1], ['pear', 2]);
$results = $fuse->remove(fn($doc) => $doc === 'banana' || $doc === 'pear');
expect($results)->toHaveCount(2);
expect($fuse->getIndex()->size())->toBe(1);
$fuseReflection = new ReflectionObject($fuse);
$docsProperty = $fuseReflection->getProperty('docs');
$docsProperty->setAccessible(true);
expect($docsProperty->getValue($fuse))->toHaveCount(1);
});