-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathFuzzySearchTest.php
More file actions
229 lines (185 loc) · 7.35 KB
/
FuzzySearchTest.php
File metadata and controls
229 lines (185 loc) · 7.35 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
use Fuse\Fuse;
beforeEach(function () {
$this->setupFuse = function ($itemList = null, $overwriteOptions = []) {
$list = $itemList ?? ['Apple', 'Orange', 'Banana'];
$options = array_merge([], $overwriteOptions);
return new Fuse($list, $options);
};
});
describe('Flat list of strings: ["Apple", "Orange", "Banana"]', function () {
beforeEach(function () {
$this->fuse = ($this->setupFuse)();
});
describe('When searching for the term "Apple"', function () {
beforeEach(function () {
$this->result = $this->fuse->search('Apple');
});
test('we get a list of exactly 1 item', function () {
expect($this->result)->toHaveCount(1);
});
test('whose value is the index 0, representing ["Apple"]', function () {
expect($this->result[0]['refIndex'])->toBe(0);
});
});
describe('When performing a fuzzy search for the term "ran"', function () {
beforeEach(function () {
$this->result = $this->fuse->search('ran');
});
test('we get a list of containing 2 items', function () {
expect($this->result)->toHaveCount(2);
});
test('whose values represent the indices of ["Orange", "Banana"]', function () {
expect($this->result[0]['refIndex'])->toBe(1);
expect($this->result[1]['refIndex'])->toBe(2);
});
});
describe('When performing a fuzzy search for the term "nan"', function () {
beforeEach(function () {
$this->result = $this->fuse->search('nan');
});
test('we get a list of containing 2 items', function () {
expect($this->result)->toHaveCount(2);
});
test('whose values represent the indices of ["Banana", "Orange"]', function () {
expect($this->result[0]['refIndex'])->toBe(2);
expect($this->result[1]['refIndex'])->toBe(1);
});
});
describe(
'When performing a fuzzy search for the term "nan" with a limit of 1 result',
function () {
beforeEach(function () {
$this->result = $this->fuse->search('nan', ['limit' => 1]);
});
test('we get a list of containing 1 item: [2]', function () {
expect($this->result)->toHaveCount(1);
});
test('whose values represent the indices of ["Banana", "Orange"]', function () {
expect($this->result[0]['refIndex'])->toBe(2);
});
},
);
});
$customBookList = [
[
'title' => "Old Man's War",
'author' => ['firstName' => 'John', 'lastName' => 'Scalzi'],
],
[
'title' => 'The Lock Artist',
'author' => ['firstName' => 'Steve', 'lastName' => 'Hamilton'],
],
['title' => 'HTML5'],
[
'title' => 'A History of England',
'author' => ['firstName' => 1066, 'lastName' => 'Hastings'],
],
];
describe('Deep key search, with ["title", "author.firstName"]', function () use ($customBookList) {
beforeEach(function () use ($customBookList) {
$this->fuse = ($this->setupFuse)($customBookList, [
'keys' => ['title', 'author.firstName'],
]);
});
describe('When searching for the term "Stve"', function () {
beforeEach(function () {
$this->result = $this->fuse->search('Stve');
});
it('we get a list containing at least 1 item', function () {
expect(count($this->result))->toBeGreaterThanOrEqual(1);
});
it('and the first item has the matching key/value pairs', function () {
expect($this->result[0]['item']['title'])->toEqual('The Lock Artist');
expect($this->result[0]['item']['author']['firstName'])->toEqual('Steve');
expect($this->result[0]['item']['author']['lastName'])->toEqual('Hamilton');
});
});
describe('When searching for the term "106"', function () {
beforeEach(function () {
$this->result = $this->fuse->search('106');
});
it('we get a list of exactly 1 item', function () {
expect(count($this->result))->toEqual(1);
});
it('whose value matches', function () {
expect($this->result[0]['item']['title'])->toEqual('A History of England');
expect($this->result[0]['item']['author']['firstName'])->toEqual(1066);
expect($this->result[0]['item']['author']['lastName'])->toEqual('Hastings');
});
});
});
describe('Searching ignoring diactrictics', function () {
$list = [['text' => 'déjà'], ['text' => 'cafe']];
$options = [
'ignoreDiacritics' => true,
'threshold' => 0,
'keys' => ['text'],
];
$fuse = new Fuse($list, $options);
test('Search: query with diacritics, list with diacritics', function () use ($fuse) {
$result = $fuse->search('déjà');
expect($result)->toHaveCount(1);
expect($result[0]['refIndex'])->toBe(0);
});
test('Search: query without diacritics, list with diacritics', function () use ($fuse) {
$result = $fuse->search('deja');
expect($result)->toHaveCount(1);
expect($result[0]['refIndex'])->toBe(0);
});
test('Search: query with diacritics, list without diacritics', function () use ($fuse) {
$result = $fuse->search('café');
expect($result)->toHaveCount(1);
expect($result[0]['refIndex'])->toBe(1);
});
test('Search: query without diacritics, list without diacritics', function () use ($fuse) {
$result = $fuse->search('cafe');
expect($result)->toHaveCount(1);
expect($result[0]['refIndex'])->toBe(1);
});
});
describe('Threshold filtering', function () {
$list = [
['name' => 'Simple Storage Service'],
['name' => 'Elastic File System'],
];
test('results with score > threshold are excluded (ignoreLocation: true)', function () use ($list) {
$fuse = new Fuse($list, [
'keys' => ['name'],
'includeScore' => true,
'threshold' => 0.3,
'ignoreLocation' => true,
]);
$results = $fuse->search('Simple Storage Service');
foreach ($results as $r) {
expect($r['score'])->toBeLessThanOrEqual(0.3);
}
expect($results)->toHaveCount(1);
expect($results[0]['item']['name'])->toBe('Simple Storage Service');
});
test('results with score > threshold are excluded (distance: 500)', function () use ($list) {
$fuse = new Fuse($list, [
'keys' => ['name'],
'includeScore' => true,
'threshold' => 0.3,
'distance' => 500,
]);
$results = $fuse->search('Simple Storage Service');
foreach ($results as $r) {
expect($r['score'])->toBeLessThanOrEqual(0.3);
}
expect($results)->toHaveCount(1);
expect($results[0]['item']['name'])->toBe('Simple Storage Service');
});
test('threshold=0 only returns exact matches', function () {
$fuse = new Fuse(['apple', 'application', 'apply'], [
'includeScore' => true,
'threshold' => 0,
]);
$results = $fuse->search('apple');
expect($results)->toHaveCount(1);
foreach ($results as $r) {
expect($r['score'])->toBeLessThanOrEqual(0.001);
}
});
});