-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
123 lines (103 loc) · 3.42 KB
/
test.js
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
var RedisIndex = require('./index.js'),
fakeRedis = require('fakeredis').createClient(),
test = require('tape');
var indexer = new RedisIndex({ redisClient: fakeRedis });
test('Index a doc', function (t) {
t.plan(4);
indexer.index('001', 'hello world!', function() {
indexer.search('hello', function(err, docIds) {
t.deepEqual(docIds, ['001'], 'matched query');
});
indexer.search('halo', function(err, docIds) {
t.deepEqual(docIds, [], 'unmatched query');
});
indexer.search('', function(err, docIds) {
t.deepEqual(docIds, [], 'empty query');
});
indexer.search(' ', function(err, docIds) {
t.deepEqual(docIds, [], 'white space query');
});
});
});
test('Re-index a doc', function (t) {
t.plan(2);
indexer.index('001', 'halo world!', function() {
indexer.search('halo', function (err, docIds) {
t.deepEqual(docIds, ['001'], 'matched query');
});
indexer.search('hello', function (err, docIds) {
t.deepEqual(docIds, [], 'unmatched query');
});
});
});
test('Index another doc', function (t) {
t.plan(2);
indexer.index('002', 'hooray world!', function() {
indexer.search('world', function (err, docIds) {
t.deepEqual(docIds, ['001', '002'], 'query matches 2 docs');
});
indexer.search('hooray', function (err, docIds) {
t.deepEqual(docIds, ['002'], 'query matches 1 doc');
});
});
});
test('Remove a doc', function (t) {
t.plan(1);
indexer.remove('001', function() {
indexer.search('halo', function (err, docIds) {
t.deepEqual(docIds, [], 'unmatched query');
});
});
});
test('Remove a non-existing doc', function (t) {
indexer.remove('005', function() {
t.end();
});
});
test('Add a new doc', function (t) {
t.plan(1);
indexer.add('004', 'Awesome...world', function() {
indexer.search('awesome', function (err, docIds) {
t.deepEqual(docIds, ['004'], 'matched query');
});
});
});
test('Add to existing doc', function (t) {
t.plan(1);
indexer.add('004', 'something awful', function() {
indexer.search('awesome awful', function (err, docIds) {
t.deepEqual(docIds, ['004'], 'matched query');
});
});
});
test('中文搜索', function (t) {
t.plan(2);
indexer.index('003', '美好的 世界', function() {
indexer.search('世界', function (err, docIds) {
t.deepEqual(docIds, ['003'], 'matched query');
});
indexer.search('随便搜索', function (err, docIds) {
t.deepEqual(docIds, [], 'unmatched query');
});
});
});
test('Multi words query', function(t) {
t.plan(3);
indexer.search('hooray world', function (err, docIds) {
t.deepEqual(docIds, ['002'], 'exact match');
});
indexer.search('世界 美好的', function (err, docIds) {
t.deepEqual(docIds, ['003'], 'matched all keywords but in alternated order');
});
indexer.search('hooray world 世界', function (err, docIds) {
t.deepEqual(docIds, [], 'not all keywords matched');
});
});
test('Tokenizer', function (t) {
t.deepEqual(indexer.tokenize("I'm yours!"), ['i', 'm', 'yours']);
t.deepEqual(indexer.tokenize(" 美好,的(世界),哈哈。你好 "), ['美好', '的', '世界', '哈哈', '你好']);
t.deepEqual(indexer.tokenize("《美好》的“世界”‘哈 哈’【你好】"), ['美好', '的', '世界', '哈', '哈', '你好']);
t.deepEqual(indexer.tokenize(" "), []);
t.deepEqual(indexer.tokenize("! - , "), []);
t.end();
});