-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
transformer.ts
204 lines (191 loc) 路 6.77 KB
/
transformer.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
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
import Redis from "../../lib/Redis";
import { getRedisVersion } from "../helpers/util";
import { expect } from "chai";
describe("transformer", () => {
describe("default transformer", () => {
describe("hmset", () => {
it("should support object", async () => {
const redis = new Redis();
expect(await redis.hmset("foo", { a: 1, b: "2" })).to.eql("OK");
expect(await redis.hget("foo", "b")).to.eql("2");
});
it("should support Map with string keys", async () => {
const redis = new Redis();
const map = new Map();
map.set("a", 1);
map.set("b", "2");
map.set(42, true);
map.set(Buffer.from("buffer"), "utf8");
map.set(Buffer.from([0xff]), "binary");
expect(await redis.hmset("foo", map)).to.eql("OK");
expect(await redis.hget("foo", "a")).to.eql("1");
expect(await redis.hget("foo", "b")).to.eql("2");
expect(await redis.hget("foo", "42")).to.eql("true");
expect(await redis.hget("foo", "buffer")).to.eql("utf8");
expect(await redis.hget("foo", Buffer.from([0xff]))).to.eql("binary");
});
it("should not affect the old way", async () => {
const redis = new Redis();
expect(await redis.hmset("foo", "a", 1, "b", "2")).to.eql("OK");
expect(await redis.hget("foo", "b")).to.eql("2");
});
});
describe("mset", () => {
it("should support object", async () => {
const redis = new Redis();
expect(await redis.mset({ a: 1, b: "2" })).to.eql("OK");
expect(await redis.mget("a", "b")).to.eql(["1", "2"]);
});
it("should support Map", async () => {
const redis = new Redis();
const map = new Map();
map.set("a", 1);
map.set("b", "2");
expect(await redis.mset(map)).to.eql("OK");
expect(await redis.mget("a", "b")).to.eql(["1", "2"]);
});
it("should not affect the old way", async () => {
const redis = new Redis();
expect(await redis.mset("a", 1, "b", "2")).to.eql("OK");
expect(await redis.mget("a", "b")).to.eql(["1", "2"]);
});
it("should work with keyPrefix option", async () => {
const redis = new Redis({ keyPrefix: "foo:" });
expect(await redis.mset({ a: 1, b: "2" })).to.eql("OK");
const otherRedis = new Redis();
expect(await otherRedis.mget("foo:a", "foo:b")).to.eql(["1", "2"]);
});
});
describe("msetnx", () => {
it("should support object", (done) => {
const redis = new Redis();
redis.msetnx({ a: 1, b: "2" }, function (err, result) {
expect(result).to.eql(1);
redis.mget("a", "b", function (err, result) {
expect(result).to.eql(["1", "2"]);
done();
});
});
});
it("should support Map", (done) => {
const redis = new Redis();
const map = new Map();
map.set("a", 1);
map.set("b", "2");
redis.msetnx(map, function (err, result) {
expect(result).to.eql(1);
redis.mget("a", "b", function (err, result) {
expect(result).to.eql(["1", "2"]);
done();
});
});
});
it("should not affect the old way", (done) => {
const redis = new Redis();
redis.msetnx("a", 1, "b", "2", function (err, result) {
expect(result).to.eql(1);
redis.mget("a", "b", function (err, result) {
expect(result).to.eql(["1", "2"]);
done();
});
});
});
it("should work with keyPrefix option", (done) => {
const redis = new Redis({ keyPrefix: "foo:" });
redis.msetnx({ a: 1, b: "2" }, function (err, result) {
expect(result).to.eql(1);
const otherRedis = new Redis();
otherRedis.mget("foo:a", "foo:b", function (err, result) {
expect(result).to.eql(["1", "2"]);
done();
});
});
});
});
describe("hgetall", () => {
it("should return an object", (done) => {
const redis = new Redis();
redis.hmset("foo", "k1", "v1", "k2", "v2", () => {
redis.hgetall("foo", function (err, result) {
expect(result).to.eql({ k1: "v1", k2: "v2" });
done();
});
});
});
it("should return {} when key not exists", (done) => {
const redis = new Redis();
redis.hgetall("foo", function (err, result) {
expect(result).to.eql({});
done();
});
});
});
describe("hset", () => {
it("should support object", async () => {
const redis = new Redis();
const [major] = await getRedisVersion(redis);
if (major < 4) {
// Skip if redis is too outdated to hset multiple pairs at once.
return;
}
// NOTE: could simplify these tests using await redis.hset instead,
// but not sure if this is deliberately testing the transformers with callbacks
return new Promise((resolve, reject) => {
redis.hset("foo", { a: 1, b: "e", c: 123 }, function (err, result) {
if (err) {
return reject(err);
}
expect(result).to.eql(3);
redis.hget("foo", "b", function (err, result) {
expect(result).to.eql("e");
resolve();
});
});
});
});
it("should support Map", async () => {
const redis = new Redis();
const [major] = await getRedisVersion(redis);
if (major < 4) {
// Skip if redis is too outdated to hset multiple pairs at once.
return;
}
const map = new Map();
map.set("a", 1);
map.set("b", "e");
return new Promise((resolve, reject) => {
redis.hset("foo", map, function (err, result) {
if (err) {
return reject(err);
}
expect(result).to.eql(2);
redis.hget("foo", "b", function (err, result) {
if (err) {
return reject(err);
}
expect(result).to.eql("e");
resolve();
});
});
});
});
it("should affect the old way", async () => {
const redis = new Redis();
const [major] = await getRedisVersion(redis);
if (major < 4) {
// Skip if redis is too outdated to hset multiple pairs at once.
return;
}
return new Promise((resolve) => {
redis.hset("foo", "a", 1, "b", "e", function (err, result) {
expect(result).to.eql(2);
redis.hget("foo", "b", function (err, result) {
expect(result).to.eql("e");
resolve();
});
});
});
});
});
});
});