-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathfulltextSpec.js
More file actions
138 lines (120 loc) · 4.49 KB
/
Copy pathfulltextSpec.js
File metadata and controls
138 lines (120 loc) · 4.49 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
import assert from 'node:assert';
import { Fulltext } from '../src/fulltext.js';
describe('fulltext', function () {
const items = [
{
name: 'Godfather',
tags: ['mafia', 'crime'],
},
{
name: 'Fight club',
tags: ['dark humor', 'anti establishment'],
},
{
name: 'Forrest Gump',
tags: ['running', 'vietnam'],
},
];
const items_with_ids = [
{
id: 10,
name: 'Godfather',
tags: ['mafia', 'crime'],
},
{
id: 20,
name: 'Fight club',
tags: ['dark humor', 'anti establishment'],
},
{
id: 30,
name: 'Forrest Gump',
tags: ['running', 'vietnam'],
},
];
const specialItems = [
{ name: 'elation' },
{ name: 'source' },
{ name: 'headless' },
];
it('checks search', function test(done) {
const fulltext = new Fulltext(items);
assert.equal(fulltext.search_full('club').length, 1);
assert.equal(fulltext.search_full('gump').length, 1);
assert.equal(fulltext.search_full('forrest gump').length, 1);
assert.equal(fulltext.search_full('forrest GUMP').length, 1);
assert.equal(fulltext.search_full('gump')[0].name, 'Forrest Gump');
assert.equal(fulltext.search_full('gump')[0]._id, 3);
assert.equal(fulltext.search_full('gump')[0].id, undefined);
assert.equal(fulltext.search_full('titanic').length, 0);
assert.equal(fulltext.search_full().length, 3);
done();
});
it("checks search with defined id's", function test(done) {
const fulltext = new Fulltext(items_with_ids);
assert.equal(fulltext.search_full('club').length, 1);
assert.equal(fulltext.search_full('gump').length, 1);
assert.equal(fulltext.search_full('forrest gump').length, 1);
assert.equal(fulltext.search_full('forrest GUMP').length, 1);
assert.equal(fulltext.search_full('gump')[0].name, 'Forrest Gump');
assert.equal(fulltext.search_full('gump')[0]._id, 3);
assert.equal(fulltext.search_full('gump')[0].id, 30);
assert.equal(fulltext.search_full('titanic').length, 0);
assert.equal(fulltext.search_full().length, 3);
done();
});
it('checks search on another fields', function test(done) {
const fulltext = new Fulltext(items, {
searchableFields: ['name', 'tags'],
});
assert.equal(fulltext.search('vietnam').length, 1);
assert.equal(fulltext.search('dark').length, 1);
assert.equal(fulltext.search('anti').length, 1);
done();
});
it('makes search stepping through characters', function test(done) {
const fulltext = new Fulltext(specialItems, {
searchableFields: ['name'],
isExactSearch: true,
});
assert.equal(fulltext.search('e').length, 1);
assert.equal(fulltext.search('el').length, 1);
assert.equal(fulltext.search('ela').length, 1);
assert.equal(fulltext.search('elat').length, 1);
assert.equal(fulltext.search('elati').length, 1); // Does not appear when stemmer is present
assert.equal(fulltext.search('elatio').length, 1);
assert.equal(fulltext.search('elation').length, 1);
assert.equal(fulltext.search('s').length, 1);
assert.equal(fulltext.search('so').length, 1); // Filtered by stopWordFilter
assert.equal(fulltext.search('sou').length, 1);
assert.equal(fulltext.search('sour').length, 1);
assert.equal(fulltext.search('sourc').length, 1);
assert.equal(fulltext.search('source').length, 1);
done();
});
it('makes search stepping through characters', function test(done) {
const stopwordfilter = new Fulltext(specialItems, {
searchableFields: ['name'],
});
const withoutstopwordfilter = new Fulltext(specialItems, {
searchableFields: ['name'],
removeStopWordFilter: true,
});
assert.equal(stopwordfilter.search('h').length, 1);
assert.equal(stopwordfilter.search('he').length, 0); // The stopwordfilter filters out "he"
assert.equal(stopwordfilter.search('hea').length, 1);
assert.equal(stopwordfilter.search('head').length, 1);
assert.equal(withoutstopwordfilter.search('h').length, 1);
assert.equal(withoutstopwordfilter.search('he').length, 1);
assert.equal(withoutstopwordfilter.search('hea').length, 1);
assert.equal(withoutstopwordfilter.search('head').length, 1);
done();
});
xit('returns internal ids', function test(done) {
const fulltext = new Fulltext(items);
assert.deepEqual(fulltext.internal_ids(), [1, 2, 3]);
assert.deepEqual(fulltext.bits_ids().array(), [1, 2, 3]);
assert.deepEqual(fulltext.get_item(1).name, 'Godfather');
done();
});
});