-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
95 lines (81 loc) · 1.94 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
import Redis from "ioredis";
import { SearchCollection } from "./src/SearchCollection";
import { RedisAdapter, MODES } from "./src/adapters/redis";
import { test } from "node:test";
import assert from "assert/strict";
await test("Redis", async (t) => {
let client;
t.before(async () => {
client = new Redis();
if (client.status === "connecting") return;
});
t.after(async () => {
await client.disconnect();
});
await t.test("Metaphone", async () => {
const collection = new SearchCollection({
adapter: new RedisAdapter("UserSearch", {
mode: MODES.phonetic,
client: client,
}),
});
await collection.set(1, "hello");
await collection.set(2, "what's up");
await collection.set(3, "foo bar");
const result = await collection.search("foo bar", {
type: "and",
between: {
from: 0,
to: -1,
},
});
assert.deepEqual(
result.map((x) => Number(x)),
[3]
);
});
await t.test("Typeahead", async () => {
const collection = new SearchCollection({
adapter: new RedisAdapter("UserSearch", {
mode: MODES.prefix,
client: client,
}),
});
await collection.set(1, "hello");
await collection.set(2, "what's up");
await collection.set(3, "foo bar");
const result = await collection.search("foo bar", {
type: "and",
between: {
from: 0,
to: -1,
},
});
const result2 = await collection.search("bar foo", {
type: "and",
between: {
from: 0,
to: -1,
},
});
const result3 = await collection.search("hell", {
type: "and",
between: {
from: 0,
to: -1,
},
});
assert.deepEqual(
result.map((x) => Number(x)),
[3]
);
assert.deepEqual(
result2.map((x) => Number(x)),
[3]
);
assert.deepEqual(
result3.map((x) => Number(x)),
[1]
);
});
});