Skip to content

Commit 5ef24a9

Browse files
author
Ruben Bridgewater
committed
Add tests and improve older tests
1 parent 2cd3818 commit 5ef24a9

File tree

12 files changed

+199
-98
lines changed

12 files changed

+199
-98
lines changed

index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ RedisClient.prototype.on_ready = function () {
308308
if (this.old_state !== null) {
309309
this.monitoring = this.old_state.monitoring;
310310
this.pub_sub_mode = this.old_state.pub_sub_mode;
311-
this.selected_db = this.old_state.selected_db;
312311
this.old_state = null;
313312
}
314313

@@ -456,13 +455,11 @@ RedisClient.prototype.connection_gone = function (why) {
456455
if (this.old_state === null) {
457456
var state = {
458457
monitoring: this.monitoring,
459-
pub_sub_mode: this.pub_sub_mode,
460-
selected_db: this.selected_db
458+
pub_sub_mode: this.pub_sub_mode
461459
};
462460
this.old_state = state;
463461
this.monitoring = false;
464462
this.pub_sub_mode = false;
465-
this.selected_db = undefined;
466463
}
467464

468465
// since we are collapsing end and close, users don't expect to be called twice
@@ -940,7 +937,7 @@ RedisClient.prototype.select = RedisClient.prototype.SELECT = function (db, call
940937
});
941938
};
942939

943-
// Store db in this.select_db to restore it on reconnect
940+
// Store info in this.server_info after each call
944941
RedisClient.prototype.info = RedisClient.prototype.INFO = function (callback) {
945942
var self = this;
946943
this.send_anyway = true;

test/auth.spec.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,38 @@ describe("client authentication", function () {
6767
});
6868

6969
if (ip === 'IPv4') {
70-
it('allows auth to be provided as part of redis url', function (done) {
70+
it('allows auth to be provided as part of redis url and do not fire commands before auth is done', function (done) {
7171
if (helper.redisProcess().spawnFailed()) this.skip();
7272

73-
client = redis.createClient('redis://foo:' + auth + '@' + config.HOST[ip] + ':' + config.PORT);
73+
var end = helper.callFuncAfter(done, 2);
74+
client = redis.createClient('redis://:' + auth + '@' + config.HOST[ip] + ':' + config.PORT);
7475
client.on("ready", function () {
75-
return done();
76+
end();
77+
});
78+
// The info command may be used while loading but not if not yet authenticated
79+
client.info(function (err, res) {
80+
assert(!err);
81+
end();
82+
});
83+
});
84+
85+
it('allows auth and database to be provided as part of redis url query parameter', function (done) {
86+
if (helper.redisProcess().spawnFailed()) this.skip();
87+
88+
client = redis.createClient('redis://' + config.HOST[ip] + ':' + config.PORT + '?db=2&password=' + auth);
89+
assert.strictEqual(client.options.db, '2');
90+
assert.strictEqual(client.options.password, auth);
91+
assert.strictEqual(client.auth_pass, auth);
92+
client.on("ready", function () {
93+
// Set a key so the used database is returned in the info command
94+
client.set('foo', 'bar');
95+
client.get('foo');
96+
assert.strictEqual(client.server_info.db2, undefined);
97+
// Using the info command should update the server_info
98+
client.info(function (err, res) {
99+
assert(typeof client.server_info.db2 === 'object');
100+
});
101+
client.flushdb(done);
76102
});
77103
});
78104
}
@@ -93,7 +119,7 @@ describe("client authentication", function () {
93119
if (helper.redisProcess().spawnFailed()) this.skip();
94120

95121
var args = config.configureClient(parser, ip, {
96-
auth_pass: auth,
122+
password: auth,
97123
no_ready_check: true
98124
});
99125
client = redis.createClient.apply(redis.createClient, args);
@@ -195,6 +221,18 @@ describe("client authentication", function () {
195221
done();
196222
});
197223
});
224+
225+
it('should emit an error if the provided password is faulty', function (done) {
226+
if (helper.redisProcess().spawnFailed()) this.skip();
227+
client = redis.createClient({
228+
password: 'wrong_password',
229+
parser: parser
230+
});
231+
client.once("error", function (err) {
232+
assert.strictEqual(err.message, 'ERR invalid password');
233+
done();
234+
});
235+
});
198236
});
199237
});
200238

test/batch.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ describe("The 'batch' method", function () {
179179
client.BATCH([
180180
["smembers", ["some set"]],
181181
["del", "some set"],
182-
["smembers", "some set"]
182+
["smembers", "some set", undefined] // The explicit undefined is handled as a callback that is undefined
183183
])
184184
.scard("some set")
185185
.exec(function (err, replies) {

test/commands/hset.spec.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,40 @@ describe("The 'hset' method", function () {
4545
});
4646
});
4747

48-
it('does not error when a buffer and array are set as fields on the same hash', function (done) {
48+
it('throws a error if someone passed a array either as field or as value', function (done) {
49+
var hash = "test hash";
50+
var field = "array";
51+
// This would be converted to "array contents" but if you use more than one entry,
52+
// it'll result in e.g. "array contents,second content" and this is not supported and considered harmful
53+
var value = ["array contents"];
54+
try {
55+
client.HMSET(hash, field, value);
56+
throw new Error('test failed');
57+
} catch (err) {
58+
if (/invalid data/.test(err.message)) {
59+
done();
60+
} else {
61+
done(err);
62+
}
63+
}
64+
});
65+
66+
it('does not error when a buffer and date are set as values on the same hash', function (done) {
4967
var hash = "test hash";
5068
var field1 = "buffer";
5169
var value1 = new Buffer("abcdefghij");
52-
var field2 = "array";
53-
var value2 = ["array contents"];
70+
var field2 = "date";
71+
var value2 = new Date();
72+
73+
client.HMSET(hash, field1, value1, field2, value2, helper.isString("OK", done));
74+
});
75+
76+
it('does not error when a buffer and date are set as fields on the same hash', function (done) {
77+
var hash = "test hash";
78+
var value1 = "buffer";
79+
var field1 = new Buffer("abcdefghij");
80+
var value2 = "date";
81+
var field2 = new Date();
5482

5583
client.HMSET(hash, field1, value1, field2, value2, helper.isString("OK", done));
5684
});

test/commands/info.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
var assert = require('assert');
4+
var config = require("../lib/config");
5+
var helper = require('../helper');
6+
var redis = config.redis;
7+
8+
describe("The 'info' method", function () {
9+
10+
helper.allTests(function(parser, ip, args) {
11+
12+
describe("using " + parser + " and " + ip, function () {
13+
var client;
14+
15+
before(function (done) {
16+
client = redis.createClient.apply(redis.createClient, args);
17+
client.once("ready", function () {
18+
client.flushall(done);
19+
});
20+
});
21+
22+
after(function () {
23+
client.end(true);
24+
});
25+
26+
it("update server_info after a info command", function (done) {
27+
client.set('foo', 'bar');
28+
client.info();
29+
client.select(2, function () {
30+
assert.strictEqual(client.server_info.db2, undefined);
31+
});
32+
client.set('foo', 'bar');
33+
client.info();
34+
setTimeout(function () {
35+
assert.strictEqual(typeof client.server_info.db2, 'object');
36+
done();
37+
}, 150);
38+
});
39+
40+
it("emit error after a failure", function (done) {
41+
client.info();
42+
client.once('error', function (err) {
43+
assert.strictEqual(err.code, 'UNCERTAIN_STATE');
44+
assert.strictEqual(err.command, 'INFO');
45+
done();
46+
});
47+
client.stream.destroy();
48+
});
49+
});
50+
});
51+
});

test/commands/keys.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ describe("The 'keys' method", function () {
1414
var client;
1515

1616
beforeEach(function (done) {
17-
args = args || {};
1817
client = redis.createClient.apply(redis.createClient, args);
1918
client.once("ready", function () {
20-
client.flushdb(done);
19+
client.flushall(done);
2120
});
2221
});
2322

test/commands/select.spec.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ describe("The 'select' method", function () {
3737

3838
beforeEach(function (done) {
3939
client = redis.createClient.apply(redis.createClient, args);
40-
client.once("ready", function () { done(); });
40+
client.once("ready", function () {
41+
client.flushdb(done);
42+
});
4143
});
4244

4345
afterEach(function () {
@@ -46,7 +48,7 @@ describe("The 'select' method", function () {
4648

4749
it("changes the database and calls the callback", function (done) {
4850
// default value of null means database 0 will be used.
49-
assert.strictEqual(client.selected_db, null, "default db should be null");
51+
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
5052
var buffering = client.SELECT(1, function (err, res) {
5153
helper.isNotError()(err, res);
5254
assert.strictEqual(client.selected_db, 1, "db should be 1 after select");
@@ -58,7 +60,7 @@ describe("The 'select' method", function () {
5860
describe("and a callback is specified", function () {
5961
describe("with a valid db index", function () {
6062
it("selects the appropriate database", function (done) {
61-
assert.strictEqual(client.selected_db, null, "default db should be null");
63+
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
6264
client.select(1, function (err) {
6365
assert.equal(err, null);
6466
assert.equal(client.selected_db, 1, "we should have selected the new valid DB");
@@ -69,7 +71,7 @@ describe("The 'select' method", function () {
6971

7072
describe("with an invalid db index", function () {
7173
it("returns an error", function (done) {
72-
assert.strictEqual(client.selected_db, null, "default db should be null");
74+
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
7375
client.select(9999, function (err) {
7476
assert.equal(err.code, 'ERR');
7577
assert.equal(err.message, 'ERR invalid DB index');
@@ -82,7 +84,7 @@ describe("The 'select' method", function () {
8284
describe("and no callback is specified", function () {
8385
describe("with a valid db index", function () {
8486
it("selects the appropriate database", function (done) {
85-
assert.strictEqual(client.selected_db, null, "default db should be null");
87+
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
8688
client.select(1);
8789
setTimeout(function () {
8890
assert.equal(client.selected_db, 1, "we should have selected the new valid DB");
@@ -93,7 +95,7 @@ describe("The 'select' method", function () {
9395

9496
describe("with an invalid db index", function () {
9597
it("emits an error when callback not provided", function (done) {
96-
assert.strictEqual(client.selected_db, null, "default db should be null");
98+
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
9799

98100
client.on('error', function (err) {
99101
assert.strictEqual(err.command, 'SELECT');
@@ -105,6 +107,21 @@ describe("The 'select' method", function () {
105107
});
106108
});
107109
});
110+
111+
describe("reconnection occurs", function () {
112+
it("selects the appropriate database after a reconnect", function (done) {
113+
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
114+
client.select(3);
115+
client.set('foo', 'bar', function () {
116+
client.stream.destroy();
117+
});
118+
client.once('ready', function () {
119+
assert.strictEqual(client.selected_db, 3);
120+
assert(typeof client.server_info.db3 === 'object');
121+
done();
122+
});
123+
});
124+
});
108125
});
109126
});
110127
});

test/commands/set.spec.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,17 @@ describe("The 'set' method", function () {
8181
describe("with valid parameters", function () {
8282
it("sets the value correctly", function (done) {
8383
client.set(key, value);
84-
setTimeout(function () {
85-
client.get(key, function (err, res) {
86-
helper.isString(value)(err, res);
87-
done();
88-
});
89-
}, 100);
84+
client.get(key, helper.isString(value, done));
9085
});
9186

9287
it("sets the value correctly even if the callback is explicitly set to undefined", function (done) {
9388
client.set(key, value, undefined);
94-
setTimeout(function () {
95-
client.get(key, function (err, res) {
96-
helper.isString(value)(err, res);
97-
done();
98-
});
99-
}, 100);
89+
client.get(key, helper.isString(value, done));
10090
});
10191

10292
it("sets the value correctly with the array syntax", function (done) {
10393
client.set([key, value]);
104-
setTimeout(function () {
105-
client.get(key, function (err, res) {
106-
helper.isString(value)(err, res);
107-
done();
108-
});
109-
}, 100);
94+
client.get(key, helper.isString(value, done));
11095
});
11196
});
11297

@@ -121,6 +106,12 @@ describe("The 'set' method", function () {
121106
});
122107
});
123108

109+
// TODO: This test has to be refactored from v.3.0 on to expect an error instead
110+
it("converts null to 'null'", function (done) {
111+
client.set('foo', null);
112+
client.get('foo', helper.isString('null', done));
113+
});
114+
124115
it("emit an error with only the key set", function (done) {
125116
client.on('error', function (err) {
126117
assert.equal(err.message, "ERR wrong number of arguments for 'set' command");

test/commands/sync.spec.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)