-
Notifications
You must be signed in to change notification settings - Fork 10
/
basic-usage.test.ts
89 lines (67 loc) · 2.49 KB
/
basic-usage.test.ts
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
import { createHandyClient } from "../src";
import { createClient, Multi } from "redis";
import * as redisMock from "redis-mock";
it("creates client", async () => {
const client = createHandyClient();
expect(client).toBeTruthy();
expect(await client.ping()).toBeTruthy();
});
it("creates client from node_redis", async () => {
const nodeRedisClient = createClient();
const client = createHandyClient(nodeRedisClient);
expect(await client.ping()).toBeTruthy();
});
it("can use set and keys", async () => {
const client = createHandyClient();
await client.set("x:foo", "123");
await client.set("x:bar", "456");
await client.set("y:baz", "789");
const keys = await client.keys("x:*");
expect(keys.sort()).toEqual(["x:foo", "x:bar"].sort());
});
it("can use hset with multiple fields", async () => {
const client = createHandyClient();
await client.hset("myhash", ["field1", "Hello"], ["field2", "Goodbye"]);
await client.hset("myhash", "field3", "foo");
expect(await client.hgetall("myhash")).toMatchObject({
field1: "Hello",
field2: "Goodbye",
field3: "foo",
});
});
it("can use setbit with string or number", async () => {
const client = createHandyClient();
await client.setbit("mykey", 7, 1);
expect(await client.getbit("mykey", 7)).toEqual(1);
await client.setbit("mykey", 7, "0");
expect(await client.getbit("mykey", 7)).toEqual(0);
});
it("can use multi", async () => {
const client = createHandyClient();
const multi = client
.multi()
.set("z:foo", "987")
.keys("z:*")
.get("z:foo");
const result = await client.execMulti(multi);
expect(result).toEqual(["OK", ["z:foo"], "987"]);
});
it("multi rejects correctly", async () => {
const client = createHandyClient();
const fakeMulti: Multi = {
exec: (callback: Function) => callback(new Error("foo"))
} as any;
await expect(client.execMulti(fakeMulti)).rejects.toEqual(new Error("foo"));
});
it("set with expiry", async () => {
const client = createHandyClient();
await client.set("a:foo", "123", ["EX", 60]);
const ttl = await client.ttl("a:foo");
expect(ttl).toBeLessThanOrEqual(60);
expect(ttl).toBeGreaterThan(55); // hopefully it didn't take over 5s to run a command...
});
it("works with redis-mock", async () => {
const mockClient: any = redisMock.createClient();
const client = createHandyClient(mockClient);
expect(client.redis).toBe(mockClient);
});