forked from redis/node-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_buffers.spec.js
268 lines (244 loc) · 12.8 KB
/
detect_buffers.spec.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
'use strict';
var assert = require('assert');
var config = require('./lib/config');
var helper = require('./helper');
var redis = config.redis;
describe('detect_buffers', function () {
var client;
var args = config.configureClient('localhost', {
detect_buffers: true
});
beforeEach(function (done) {
client = redis.createClient.apply(null, args);
client.once('error', done);
client.once('connect', function () {
client.flushdb(function (err) {
client.hmset('hash key 2', 'key 1', 'val 1', 'key 2', 'val 2');
client.set('string key 1', 'string value');
return done(err);
});
});
});
afterEach(function () {
client.end(true);
});
describe('get', function () {
describe('first argument is a string', function () {
it('returns a string', function (done) {
client.get('string key 1', helper.isString('string value', done));
});
it('returns a string when executed as part of transaction', function (done) {
client.multi().get('string key 1').exec(function (err, res) {
helper.isString('string value', done)(err, res[0]);
});
});
});
describe('first argument is a buffer', function () {
it('returns a buffer', function (done) {
client.get(new Buffer('string key 1'), function (err, reply) {
assert.strictEqual(true, Buffer.isBuffer(reply));
assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply.inspect());
return done(err);
});
});
it('returns a bufffer when executed as part of transaction', function (done) {
client.multi().get(new Buffer('string key 1')).exec(function (err, reply) {
assert.strictEqual(1, reply.length);
assert.strictEqual(true, Buffer.isBuffer(reply[0]));
assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply[0].inspect());
return done(err);
});
});
});
});
describe('multi.hget', function () {
it('can interleave string and buffer results', function (done) {
client.multi()
.hget('hash key 2', 'key 1')
.hget(new Buffer('hash key 2'), 'key 1')
.hget('hash key 2', new Buffer('key 2'))
.hget('hash key 2', 'key 2')
.exec(function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(4, reply.length);
assert.strictEqual('val 1', reply[0]);
assert.strictEqual(true, Buffer.isBuffer(reply[1]));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[1].inspect());
assert.strictEqual(true, Buffer.isBuffer(reply[2]));
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect());
assert.strictEqual('val 2', reply[3]);
return done(err);
});
});
});
describe('batch.hget', function () {
it('can interleave string and buffer results', function (done) {
client.batch()
.hget('hash key 2', 'key 1')
.hget(new Buffer('hash key 2'), 'key 1')
.hget('hash key 2', new Buffer('key 2'))
.hget('hash key 2', 'key 2')
.exec(function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(4, reply.length);
assert.strictEqual('val 1', reply[0]);
assert.strictEqual(true, Buffer.isBuffer(reply[1]));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[1].inspect());
assert.strictEqual(true, Buffer.isBuffer(reply[2]));
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect());
assert.strictEqual('val 2', reply[3]);
return done(err);
});
});
});
describe('hmget', function () {
describe('first argument is a string', function () {
it('returns strings for keys requested', function (done) {
client.hmget('hash key 2', 'key 1', 'key 2', function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(2, reply.length);
assert.strictEqual('val 1', reply[0]);
assert.strictEqual('val 2', reply[1]);
return done(err);
});
});
it('returns strings for keys requested in transaction', function (done) {
client.multi().hmget('hash key 2', 'key 1', 'key 2').exec(function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(1, reply.length);
assert.strictEqual(2, reply[0].length);
assert.strictEqual('val 1', reply[0][0]);
assert.strictEqual('val 2', reply[0][1]);
return done(err);
});
});
it('handles array of strings with undefined values (repro #344)', function (done) {
client.hmget('hash key 2', 'key 3', 'key 4', function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(2, reply.length);
assert.equal(null, reply[0]);
assert.equal(null, reply[1]);
return done(err);
});
});
it('handles array of strings with undefined values in transaction (repro #344)', function (done) {
client.multi().hmget('hash key 2', 'key 3', 'key 4').exec(function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(1, reply.length);
assert.strictEqual(2, reply[0].length);
assert.equal(null, reply[0][0]);
assert.equal(null, reply[0][1]);
return done(err);
});
});
});
describe('first argument is a buffer', function () {
it('returns buffers for keys requested', function (done) {
client.hmget(new Buffer('hash key 2'), 'key 1', 'key 2', function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(2, reply.length);
assert.strictEqual(true, Buffer.isBuffer(reply[0]));
assert.strictEqual(true, Buffer.isBuffer(reply[1]));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect());
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[1].inspect());
return done(err);
});
});
it('returns buffers for keys requested in transaction', function (done) {
client.multi().hmget(new Buffer('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(1, reply.length);
assert.strictEqual(2, reply[0].length);
assert.strictEqual(true, Buffer.isBuffer(reply[0][0]));
assert.strictEqual(true, Buffer.isBuffer(reply[0][1]));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0][0].inspect());
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0][1].inspect());
return done(err);
});
});
it('returns buffers for keys requested in .batch', function (done) {
client.batch().hmget(new Buffer('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) {
assert.strictEqual(true, Array.isArray(reply));
assert.strictEqual(1, reply.length);
assert.strictEqual(2, reply[0].length);
assert.strictEqual(true, Buffer.isBuffer(reply[0][0]));
assert.strictEqual(true, Buffer.isBuffer(reply[0][1]));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0][0].inspect());
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0][1].inspect());
return done(err);
});
});
});
});
describe('hgetall', function (done) {
describe('first argument is a string', function () {
it('returns string values', function (done) {
client.hgetall('hash key 2', function (err, reply) {
assert.strictEqual('object', typeof reply);
assert.strictEqual(2, Object.keys(reply).length);
assert.strictEqual('val 1', reply['key 1']);
assert.strictEqual('val 2', reply['key 2']);
return done(err);
});
});
it('returns string values when executed in transaction', function (done) {
client.multi().hgetall('hash key 2').exec(function (err, reply) {
assert.strictEqual(1, reply.length);
assert.strictEqual('object', typeof reply[0]);
assert.strictEqual(2, Object.keys(reply[0]).length);
assert.strictEqual('val 1', reply[0]['key 1']);
assert.strictEqual('val 2', reply[0]['key 2']);
return done(err);
});
});
it('returns string values when executed in .batch', function (done) {
client.batch().hgetall('hash key 2').exec(function (err, reply) {
assert.strictEqual(1, reply.length);
assert.strictEqual('object', typeof reply[0]);
assert.strictEqual(2, Object.keys(reply[0]).length);
assert.strictEqual('val 1', reply[0]['key 1']);
assert.strictEqual('val 2', reply[0]['key 2']);
return done(err);
});
});
});
describe('first argument is a buffer', function () {
it('returns buffer values', function (done) {
client.hgetall(new Buffer('hash key 2'), function (err, reply) {
assert.strictEqual(null, err);
assert.strictEqual('object', typeof reply);
assert.strictEqual(2, Object.keys(reply).length);
assert.strictEqual(true, Buffer.isBuffer(reply['key 1']));
assert.strictEqual(true, Buffer.isBuffer(reply['key 2']));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply['key 1'].inspect());
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply['key 2'].inspect());
return done(err);
});
});
it('returns buffer values when executed in transaction', function (done) {
client.multi().hgetall(new Buffer('hash key 2')).exec(function (err, reply) {
assert.strictEqual(1, reply.length);
assert.strictEqual('object', typeof reply[0]);
assert.strictEqual(2, Object.keys(reply[0]).length);
assert.strictEqual(true, Buffer.isBuffer(reply[0]['key 1']));
assert.strictEqual(true, Buffer.isBuffer(reply[0]['key 2']));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect());
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect());
return done(err);
});
});
it('returns buffer values when executed in .batch', function (done) {
client.batch().hgetall(new Buffer('hash key 2')).exec(function (err, reply) {
assert.strictEqual(1, reply.length);
assert.strictEqual('object', typeof reply[0]);
assert.strictEqual(2, Object.keys(reply[0]).length);
assert.strictEqual(true, Buffer.isBuffer(reply[0]['key 1']));
assert.strictEqual(true, Buffer.isBuffer(reply[0]['key 2']));
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect());
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect());
return done(err);
});
});
});
});
});