Skip to content

Commit 04fe11b

Browse files
Erin SpicelandBenjamin Coe
authored andcommitted
Add simple tests for generated commands.
1 parent 071b3ff commit 04fe11b

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"url": "git://github.com/mranney/node_redis.git"
3131
},
3232
"dependencies": {
33+
"sinon": "^1.15.4",
3334
"uuid": "^2.0.1"
3435
}
3536
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
var async = require('async');
2+
var assert = require('assert');
3+
var config = require("../../lib/config");
4+
var nodeAssert = require('../../lib/nodeify-assertions');
5+
var redis = config.redis;
6+
var RedisProcess = require("../../lib/redis-process");
7+
var uuid = require('uuid');
8+
var commands = require("../../../lib/commands");
9+
var sinon = require('sinon').sandbox.create();
10+
11+
var overwritten = ['eval', 'hmset', 'multi', 'select'];
12+
13+
describe("The auto-generated methods", function () {
14+
15+
var rp;
16+
before(function (done) {
17+
RedisProcess.start(function (err, _rp) {
18+
rp = _rp;
19+
return done(err);
20+
});
21+
})
22+
23+
function removeMochaListener () {
24+
var mochaListener = process.listeners('uncaughtException').pop();
25+
process.removeListener('uncaughtException', mochaListener);
26+
return mochaListener;
27+
}
28+
29+
function allTests(parser, ip) {
30+
var args = config.configureClient(parser, ip);
31+
32+
describe("using " + parser + " and " + ip, function () {
33+
var key, value;
34+
var client;
35+
36+
beforeEach(function (done) {
37+
client = redis.createClient.apply(redis.createClient, args);
38+
client.once("error", function onError(err) {
39+
done(err);
40+
});
41+
client.once("ready", function onReady() {
42+
done();
43+
});
44+
});
45+
46+
afterEach(function () {
47+
client.end();
48+
});
49+
50+
commands.forEach(function (method) {
51+
if (overwritten.indexOf(method) > -1) {
52+
// these are in the list of generated commands but are later overwritten with
53+
// different behavior by node_redis
54+
return;
55+
}
56+
57+
describe("the " + method + " method", function () {
58+
var methodArgs;
59+
var noop = function () { };
60+
61+
it("calls sendCommand with whatever arguments it receives", function () {
62+
key = uuid.v4();
63+
value = uuid.v4();
64+
65+
var parts = method.split(' ');
66+
var argNum = 0;
67+
68+
client.send_command = sinon.spy();
69+
methodArgs = [key, value, noop];
70+
71+
client[parts[0]].apply(client, methodArgs);
72+
73+
assert.strictEqual(client.send_command.called, true,
74+
"Client.send_command should have been called.");
75+
assert.strictEqual(parts[0], client.send_command.args[0][argNum],
76+
"Command name '" + parts[0] + "' should be passed as arg " +
77+
argNum + " to send_command");
78+
argNum++;
79+
/*
80+
* Um, except this doesn't work? The second part of the command is never sent????
81+
if (parts[1]) {
82+
assert.strictEqual(parts[1], client.send_command.args[0][argNum],
83+
"Second command '" + parts[1] + "' should be passed as arg " +
84+
argNum + " to send_command");
85+
argNum++;
86+
}
87+
*/
88+
assert.strictEqual(methodArgs.length, client.send_command.args[0][argNum].length,
89+
"The rest of the args to " + method + " should be passed as arg an array to send_command");
90+
assert.strictEqual(methodArgs[0], client.send_command.args[0][argNum][0],
91+
"Arg " + argNum + " to " + method + " should be passed as arg " +
92+
argNum + " to send_command");
93+
assert.strictEqual(methodArgs[1], client.send_command.args[0][argNum][1],
94+
"Arg " + argNum + " to " + method + " should be passed as arg " +
95+
argNum + " to send_command");
96+
assert.strictEqual(methodArgs[2], client.send_command.args[0][argNum][2],
97+
"Arg " + argNum + " to " + method + " should be passed as arg " +
98+
argNum + " to send_command");
99+
});
100+
});
101+
});
102+
});
103+
}
104+
105+
['javascript', 'hiredis'].forEach(function (parser) {
106+
allTests(parser, "/tmp/redis.sock");
107+
['IPv4', 'IPv6'].forEach(function (ip) {
108+
allTests(parser, ip);
109+
})
110+
});
111+
112+
afterEach(function () {
113+
sinon.restore();
114+
});
115+
116+
after(function (done) {
117+
if (rp) rp.stop(done);
118+
});
119+
});

0 commit comments

Comments
 (0)