1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

slight refactor from code review

smoke test large list of commands

ported more tests to mocha, some slight cleanup in tests

move sinon and uuid to dev dependencies

finished porting eval tests over to mocha

rebased mocha testing branch with master

ported client and script tests

ported watch tests

ported detect_buffers tests

ported unref tests

ported auth tests over to mocha

ported idle and no_delay tests

ported hlen, hset

continuing marching forward

ported hincrby, sinter, sort, pubsub tests. improved logic in redis-process, I was still occasionally having issues where redis failed to exit.

switch back to default test command

ported del, exists, hlen, keys, randomkey, type

cleanup based on what I've learned so far from refactor. we now start and stop redis less often. moved tests to their final resting place

finished porting node_redis client tests

ported hgetall, mget, msetnx, rename, renamenx, setex, setnx

ported hgetall, mget, msetnx, rename, renamenx, setex, setnx

ported queue tests to mocha

amalgamated some of the helper logic

ported sadd, scard, sismember, srem, utf-8
This commit is contained in:
Benjamin Coe
2015-07-22 20:59:08 -07:00
parent 04fe11b0f5
commit a0832c3744
54 changed files with 3013 additions and 2593 deletions

View File

@@ -0,0 +1,61 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'client' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
var pattern = /addr=/;
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err) {
if (!helper.serverVersionAtLeast(client, [2, 4, 0])) {
err = Error('script not supported in redis <= 2.4.0')
}
return done(err);
})
});
});
afterEach(function () {
client.end();
});
describe('list', function () {
it('lists connected clients', function (done) {
client.client("list", helper.match(pattern, done));
});
it("lists connected clients when invoked with multi's chaining syntax", function (done) {
client.multi().client("list").exec(function(err, results) {
assert(pattern.test(results[0]), "expected string '" + results + "' to match " + pattern.toString());
return done()
})
});
it("lists connected clients when invoked with multi's array syntax", function (done) {
client.multi().client("list").exec(function(err, results) {
assert(pattern.test(results[0]), "expected string '" + results + "' to match " + pattern.toString());
return done()
})
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,106 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'dbsize' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key, value;
beforeEach(function () {
key = uuid.v4();
value = uuid.v4();
});
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("reports an error", function (done) {
client.dbsize([], function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err, res) {
helper.isString("OK")(err, res);
done();
});
});
});
afterEach(function () {
client.end();
});
it("returns a zero db size", function (done) {
client.dbsize([], function (err, res) {
helper.isNotError()(err, res);
helper.isType.number()(err, res);
assert.strictEqual(res, 0, "Initial db size should be 0");
done();
});
});
describe("when more data is added to Redis", function () {
var oldSize;
beforeEach(function (done) {
client.dbsize([], function (err, res) {
helper.isType.number()(err, res);
assert.strictEqual(res, 0, "Initial db size should be 0");
oldSize = res;
client.set(key, value, function (err, res) {
helper.isNotError()(err, res);
done();
});
});
});
it("returns a larger db size", function (done) {
client.dbsize([], function (err, res) {
helper.isNotError()(err, res);
helper.isType.positiveNumber()(err, res);
assert.strictEqual(true, (oldSize < res), "Adding data should increase db size.");
done();
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

51
test/commands/del.spec.js Normal file
View File

@@ -0,0 +1,51 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'del' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('allows a single key to be deleted', function (done) {
client.set('foo', 'bar');
client.del('foo', helper.isNumber(1));
client.get('foo', helper.isNull(done));
});
it('allows del to be called on a key that does not exist', function (done) {
client.del('foo', helper.isNumber(0, done));
});
it('allows multiple keys to be deleted', function (done) {
client.mset('foo', 'bar', 'apple', 'banana');
client.del('foo', 'apple', helper.isNumber(2));
client.get('foo', helper.isNull());
client.get('apple', helper.isNull(done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

199
test/commands/eval.spec.js Normal file
View File

@@ -0,0 +1,199 @@
var assert = require("assert");
var config = require("../lib/config");
var crypto = require("crypto");
var helper = require("../helper");
var redis = config.redis;
describe("The 'eval' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err) {
if (!helper.serverVersionAtLeast(client, [2, 5, 0])) {
err = Error('exec not supported in redis <= 2.5.0')
}
return done(err);
})
});
});
afterEach(function () {
client.end();
});
it('converts a float to an integer when evaluated', function (done) {
client.eval("return 100.5", 0, helper.isNumber(100, done));
});
it('returns a string', function (done) {
client.eval("return 'hello world'", 0, helper.isString('hello world', done));
});
it('converts boolean true to integer 1', function (done) {
client.eval("return true", 0, helper.isNumber(1, done));
});
it('converts boolean false to null', function (done) {
client.eval("return false", 0, helper.isNull(done));
});
it('converts lua status code to string representation', function (done) {
client.eval("return {ok='fine'}", 0, helper.isString('fine', done));
});
it('converts lua error to an error response', function (done) {
client.eval("return {err='this is an error'}", 0, helper.isError(done));
});
it('represents a lua table appropritely', function (done) {
client.eval("return {1,2,3,'ciao',{1,2}}", 0, function (err, res) {
assert.strictEqual(5, res.length);
assert.strictEqual(1, res[0]);
assert.strictEqual(2, res[1]);
assert.strictEqual(3, res[2]);
assert.strictEqual("ciao", res[3]);
assert.strictEqual(2, res[4].length);
assert.strictEqual(1, res[4][0]);
assert.strictEqual(2, res[4][1]);
return done();
});
});
it('populates keys and argv correctly', function (done) {
client.eval("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "a", "b", "c", "d", function (err, res) {
assert.strictEqual(4, res.length);
assert.strictEqual("a", res[0]);
assert.strictEqual("b", res[1]);
assert.strictEqual("c", res[2]);
assert.strictEqual("d", res[3]);
return done();
});
});
it('allows arguments to be provided in array rather than as multiple parameters', function (done) {
client.eval(["return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "a", "b", "c", "d"], function (err, res) {
assert.strictEqual(4, res.length);
assert.strictEqual("a", res[0]);
assert.strictEqual("b", res[1]);
assert.strictEqual("c", res[2]);
assert.strictEqual("d", res[3]);
return done();
});
});
describe('evalsha', function () {
var source = "return redis.call('get', 'sha test')";
var sha = crypto.createHash('sha1').update(source).digest('hex');
beforeEach(function (done) {
client.set("sha test", "eval get sha test", function (err, res) {
return done(err);
});
});
it('allows a script to be executed that accesses the redis API', function (done) {
client.eval(source, 0, helper.isString('eval get sha test', done));
});
it('can execute a script if the SHA exists', function (done) {
client.evalsha(sha, 0, helper.isString('eval get sha test', done));
});
it('throws an error if SHA does not exist', function (done) {
client.evalsha('ffffffffffffffffffffffffffffffffffffffff', 0, helper.isError(done));
});
});
it('allows a key to be incremented, and performs appropriate conversion from LUA type', function (done) {
client.set("incr key", 0, function (err, reply) {
if (err) return done(err);
client.eval("local foo = redis.call('incr','incr key')\n" + "return {type(foo),foo}", 0, function (err, res) {
assert.strictEqual(2, res.length);
assert.strictEqual("number", res[0]);
assert.strictEqual(1, res[1]);
return done(err);
});
});
});
it('allows a bulk operation to be performed, and performs appropriate conversion from LUA type', function (done) {
client.set("bulk reply key", "bulk reply value", function (err, res) {
client.eval("local foo = redis.call('get','bulk reply key'); return {type(foo),foo}", 0, function (err, res) {
assert.strictEqual(2, res.length);
assert.strictEqual("string", res[0]);
assert.strictEqual("bulk reply value", res[1]);
return done(err);
});
});
});
it('allows a multi mulk operation to be performed, with the appropriate type conversion', function (done) {
client.multi()
.del("mylist")
.rpush("mylist", "a")
.rpush("mylist", "b")
.rpush("mylist", "c")
.exec(function (err, replies) {
if (err) return done(err);
client.eval("local foo = redis.call('lrange','mylist',0,-1); return {type(foo),foo[1],foo[2],foo[3],# foo}", 0, function (err, res) {
assert.strictEqual(5, res.length);
assert.strictEqual("table", res[0]);
assert.strictEqual("a", res[1]);
assert.strictEqual("b", res[2]);
assert.strictEqual("c", res[3]);
assert.strictEqual(3, res[4]);
return done(err);
});
});
});
it('returns an appropriate representation of Lua status reply', function (done) {
client.eval("local foo = redis.call('set','mykey','myval'); return {type(foo),foo['ok']}", 0, function (err, res) {
assert.strictEqual(2, res.length);
assert.strictEqual("table", res[0]);
assert.strictEqual("OK", res[1]);
return done(err);
});
});
it('returns an appropriate representation of a Lua error reply', function (done) {
client.set("error reply key", "error reply value", function (err, res) {
if (err) return done(err);
client.eval("local foo = redis.pcall('incr','error reply key'); return {type(foo),foo['err']}", 0, function (err, res) {
assert.strictEqual(2, res.length);
assert.strictEqual("table", res[0]);
assert.strictEqual("ERR value is not an integer or out of range", res[1]);
return done(err);
});
});
});
it('returns an appropriate representation of a Lua nil reply', function (done) {
client.del("nil reply key", function (err, res) {
if (err) return done(err);
client.eval("local foo = redis.call('get','nil reply key'); return {type(foo),foo == false}", 0, function (err, res) {
if (err) throw err;
assert.strictEqual(2, res.length);
assert.strictEqual("boolean", res[0]);
assert.strictEqual(1, res[1]);
return done(err);
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,43 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'exits' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('returns 1 if the key exists', function (done) {
client.set('foo', 'bar');
client.exists('foo', helper.isNumber(1, done));
});
it('returns 0 if the key does not exist', function (done) {
client.exists('bar', helper.isNumber(0, done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,115 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'flushdb' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key, key2;
beforeEach(function () {
key = uuid.v4();
key2 = uuid.v4();
});
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("reports an error", function (done) {
client.flushdb(function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
done();
});
});
afterEach(function () {
client.end();
});
describe("when there is data in Redis", function () {
var oldSize;
beforeEach(function (done) {
async.parallel([function (next) {
client.mset(key, uuid.v4(), key2, uuid.v4(), function (err, res) {
helper.isNotError()(err, res);
next(err);
});
}, function (next) {
client.dbsize([], function (err, res) {
helper.isType.positiveNumber()(err, res);
oldSize = res;
next(err);
});
}], function (err) {
if (err) {
return done(err);
}
client.flushdb(function (err, res) {
helper.isString("OK")(err, res);
done(err);
});
});
});
it("deletes all the keys", function (done) {
client.mget(key, key2, function (err, res) {
assert.strictEqual(null, err, "Unexpected error returned");
assert.strictEqual(true, Array.isArray(res), "Results object should be an array.");
assert.strictEqual(2, res.length, "Results array should have length 2.");
assert.strictEqual(null, res[0], "Redis key should have been flushed.");
assert.strictEqual(null, res[1], "Redis key should have been flushed.");
done(err);
});
});
it("results in a db size of zero", function (done) {
client.dbsize([], function (err, res) {
helper.isNotError()(err, res);
helper.isType.number()(err, res);
assert.strictEqual(0, res, "Flushing db should result in db size 0");
done();
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

92
test/commands/get.spec.js Normal file
View File

@@ -0,0 +1,92 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'get' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key, value;
beforeEach(function () {
key = uuid.v4();
value = uuid.v4();
});
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("reports an error", function (done) {
client.get(key, function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
done();
});
});
afterEach(function () {
client.end();
});
describe("when the key exists in Redis", function () {
beforeEach(function (done) {
client.set(key, value, function (err, res) {
helper.isNotError()(err, res);
done();
});
});
it("gets the value correctly", function (done) {
client.get(key, function (err, res) {
helper.isString(value)(err, res);
done(err);
});
});
});
describe("when the key does not exist in Redis", function () {
it("gets a null value", function (done) {
client.get(key, function (err, res) {
helper.isNull()(err, res);
done(err);
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,96 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'getset' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key, value, value2;
beforeEach(function () {
key = uuid.v4();
value = uuid.v4();
value2 = uuid.v4();
});
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("reports an error", function (done) {
client.get(key, function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
done();
});
});
afterEach(function () {
client.end();
});
describe("when the key exists in Redis", function () {
beforeEach(function (done) {
client.set(key, value, function (err, res) {
helper.isNotError()(err, res);
done();
});
});
it("gets the value correctly", function (done) {
client.getset(key, value2, function (err, res) {
helper.isString(value)(err, res);
client.get(key, function (err, res) {
helper.isString(value2)(err, res);
done(err);
});
});
});
});
describe("when the key does not exist in Redis", function () {
it("gets a null value", function (done) {
client.getset(key, value, function (err, res) {
helper.isNull()(err, res);
done(err);
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,91 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'hgetall' method", function () {
function allTests(parser, ip) {
describe("using " + parser + " and " + ip, function () {
var client;
describe('regular client', function () {
var args = config.configureClient(parser, ip);
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('handles simple keys and values', function (done) {
client.hmset(["hosts", "mjr", "1", "another", "23", "home", "1234"], helper.isString("OK"));
client.HGETALL(["hosts"], function (err, obj) {
assert.strictEqual(3, Object.keys(obj).length);
assert.strictEqual("1", obj.mjr.toString());
assert.strictEqual("23", obj.another.toString());
assert.strictEqual("1234", obj.home.toString());
return done(err);
});
});
it('handles fetching keys set using an object', function (done) {
client.hmset("msg_test", {message: "hello"}, helper.isString("OK"));
client.hgetall("msg_test", function (err, obj) {
assert.strictEqual(1, Object.keys(obj).length);
assert.strictEqual(obj.message, "hello");
return done(err);
});
});
it('handles fetching a messing key', function (done) {
client.hgetall("missing", function (err, obj) {
assert.strictEqual(null, obj);
return done(err);
});
});
});
describe('binary client', function () {
var client;
var args = config.configureClient(parser, ip, {
return_buffers: true
});
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('returns binary results', function (done) {
client.hmset(["bhosts", "mjr", "1", "another", "23", "home", "1234", new Buffer([0xAA, 0xBB, 0x00, 0xF0]), new Buffer([0xCC, 0xDD, 0x00, 0xF0])], helper.isString("OK"));
client.HGETALL(["bhosts"], function (err, obj) {
assert.strictEqual(4, Object.keys(obj).length);
assert.strictEqual("1", obj.mjr.toString());
assert.strictEqual("23", obj.another.toString());
assert.strictEqual("1234", obj.home.toString());
assert.strictEqual((new Buffer([0xAA, 0xBB, 0x00, 0xF0])).toString('binary'), Object.keys(obj)[3]);
assert.strictEqual((new Buffer([0xCC, 0xDD, 0x00, 0xF0])).toString('binary'), obj[(new Buffer([0xAA, 0xBB, 0x00, 0xF0])).toString('binary')].toString('binary'));
return done(err);
});
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,48 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'hincrby' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
var hash = "test hash";
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('increments a key that has already been set', function (done) {
var field = "field 1";
client.HSET(hash, field, 33);
client.HINCRBY(hash, field, 10, helper.isNumber(43, done));
});
it('increments a key that has not been set', function (done) {
var field = "field 2";
client.HINCRBY(hash, field, 10, helper.isNumber(10, done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,46 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'hlen' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('reports the count of keys', function (done) {
var hash = "test hash";
var field1 = new Buffer("0123456789");
var value1 = new Buffer("abcdefghij");
var field2 = new Buffer(0);
var value2 = new Buffer(0);
client.HSET(hash, field1, value1, helper.isNumber(1));
client.HSET(hash, field2, value2, helper.isNumber(1));
client.HLEN(hash, helper.isNumber(2, done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,68 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'hmget' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
var hash = 'test hash';
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb();
client.HMSET(hash, {"0123456789": "abcdefghij", "some manner of key": "a type of value"}, helper.isString('OK'));
return done();
});
});
it('allows keys to be specified using multiple arguments', function (done) {
client.HMGET(hash, "0123456789", "some manner of key", function (err, reply) {
assert.strictEqual("abcdefghij", reply[0].toString());
assert.strictEqual("a type of value", reply[1].toString());
return done(err);
});
});
it('allows keys to be specified by passing an array', function (done) {
client.HMGET(hash, ["0123456789", "some manner of key"], function (err, reply) {
assert.strictEqual("abcdefghij", reply[0].toString());
assert.strictEqual("a type of value", reply[1].toString());
return done(err);
});
});
it('allows a single key to be specified in an array', function (done) {
client.HMGET(hash, ["0123456789"], function (err, reply) {
assert.strictEqual("abcdefghij", reply[0].toString());
return done(err);
});
});
it('allows keys to be specified that have not yet been set', function (done) {
client.HMGET(hash, "missing thing", "another missing thing", function (err, reply) {
assert.strictEqual(null, reply[0]);
assert.strictEqual(null, reply[1]);
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,61 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'hmset' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
var hash = 'test hash';
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('handles redis-style syntax', function (done) {
client.HMSET(hash, "0123456789", "abcdefghij", "some manner of key", "a type of value", helper.isString('OK'));
client.HGETALL(hash, function (err, obj) {
assert.equal(obj['0123456789'], 'abcdefghij');
assert.equal(obj['some manner of key'], 'a type of value');
return done(err);
})
});
it('handles object-style syntax', function (done) {
client.HMSET(hash, {"0123456789": "abcdefghij", "some manner of key": "a type of value"}, helper.isString('OK'));
client.HGETALL(hash, function (err, obj) {
assert.equal(obj['0123456789'], 'abcdefghij');
assert.equal(obj['some manner of key'], 'a type of value');
return done(err);
})
});
it('allows a numeric key', function (done) {
client.HMSET(hash, 99, 'banana', helper.isString('OK'));
client.HGETALL(hash, function (err, obj) {
assert.equal(obj['99'], 'banana');
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,70 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'hset' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
var hash = 'test hash';
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('allows a value to be set in a hash', function (done) {
var field = new Buffer("0123456789");
var value = new Buffer("abcdefghij");
client.HSET(hash, field, value, helper.isNumber(1));
client.HGET(hash, field, helper.isString(value.toString(), done));
});
it('handles an empty value', function (done) {
var field = new Buffer("0123456789");
var value = new Buffer(0);
client.HSET(hash, field, value, helper.isNumber(1));
client.HGET([hash, field], helper.isString("", done));
});
it('handles empty key and value', function (done) {
var field = new Buffer(0);
var value = new Buffer(0);
client.HSET([hash, field, value], function (err, res) {
assert.strictEqual(res, 1);
client.HSET(hash, field, value, helper.isNumber(0, done));
});
});
it('does not error when a buffer and array are set as fields on the same hash', function (done) {
var hash = "test hash"
var field1 = "buffer"
var value1 = new Buffer("abcdefghij")
var field2 = "array"
var value2 = ["array contents"]
client.HMSET(hash, field1, value1, field2, value2, helper.isString("OK", done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

124
test/commands/incr.spec.js Normal file
View File

@@ -0,0 +1,124 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'incr' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key = "sequence";
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.set(key, "9007199254740992", function (err, res) {
helper.isNotError()(err, res);
client.quit();
});
});
client.on('end', function () {
return done();
});
});
afterEach(function () {
client.end();
});
it("reports an error", function (done) {
client.incr(function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected and a value in Redis", function () {
var client;
// Also, why tf were these disabled for hiredis? They work just fine.
before(function (done) {
/*
9007199254740992 -> 9007199254740992
9007199254740993 -> 9007199254740992
9007199254740994 -> 9007199254740994
9007199254740995 -> 9007199254740996
9007199254740996 -> 9007199254740996
9007199254740997 -> 9007199254740996
*/
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.set(key, "9007199254740992", function (err, res) {
helper.isNotError()(err, res);
done();
});
});
});
after(function () {
client.end();
});
it("changes the last digit from 2 to 3", function (done) {
client.incr(key, function (err, res) {
helper.isString("9007199254740993")(err, res);
done(err);
});
});
describe("and we call it again", function () {
it("changes the last digit from 3 to 4", function (done) {
client.incr(key, function (err, res) {
helper.isString("9007199254740994")(err, res);
done(err);
});
});
describe("and again", function () {
it("changes the last digit from 4 to 5", function (done) {
client.incr(key, function (err, res) {
helper.isString("9007199254740995")(err, res);
done(err);
});
});
describe("and again", function () {
it("changes the last digit from 5 to 6", function (done) {
client.incr(key, function (err, res) {
helper.isString("9007199254740996")(err, res);
done(err);
});
});
describe("and again", function () {
it("changes the last digit from 6 to 7", function (done) {
client.incr(key, function (err, res) {
helper.isString("9007199254740997")(err, res);
done(err);
});
});
});
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,76 @@
var assert = require("assert");
var config = require("../lib/config");
var crypto = require("crypto");
var helper = require("../helper");
var redis = config.redis;
describe("The 'keys' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('returns matching keys', function (done) {
client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], helper.isString("OK"));
client.KEYS(["test keys*"], function (err, results) {
assert.strictEqual(2, results.length);
assert.ok(~results.indexOf("test keys 1"));
assert.ok(~results.indexOf("test keys 2"));
return done(err);
});
});
it('handles a large packet size', function (done) {
var keys_values = [];
for (var i = 0; i < 200; i++) {
var key_value = [
"multibulk:" + crypto.randomBytes(256).toString("hex"), // use long strings as keys to ensure generation of large packet
"test val " + i
];
keys_values.push(key_value);
}
client.mset(keys_values.reduce(function(a, b) {
return a.concat(b);
}), helper.isString("OK"));
client.KEYS("multibulk:*", function(err, results) {
assert.deepEqual(keys_values.map(function(val) {
return val[0];
}).sort(), results.sort());
return done(err);
});
});
it('handles an empty response', function (done) {
client.KEYS(['users:*'], function (err, results) {
assert.strictEqual(results.length, 0);
assert.ok(Array.isArray(results));
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,66 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'mget' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb();
client.mset(["mget keys 1", "mget val 1", "mget keys 2", "mget val 2", "mget keys 3", "mget val 3"], done);
});
});
it('handles fetching multiple keys in argument form', function (done) {
client.mset(["mget keys 1", "mget val 1", "mget keys 2", "mget val 2", "mget keys 3", "mget val 3"], helper.isString("OK"));
client.MGET("mget keys 1", "mget keys 2", "mget keys 3", function (err, results) {
assert.strictEqual(3, results.length);
assert.strictEqual("mget val 1", results[0].toString());
assert.strictEqual("mget val 2", results[1].toString());
assert.strictEqual("mget val 3", results[2].toString());
return done(err);
});
});
it('handles fetching multiple keys via an array', function (done) {
client.MGET(["mget keys 1", "mget keys 2", "mget keys 3"], function (err, results) {
assert.strictEqual("mget val 1", results[0].toString());
assert.strictEqual("mget val 2", results[1].toString());
assert.strictEqual("mget val 3", results[2].toString());
return done(err);
});
});
it('handles fetching multiple keys, when some keys do not exist', function (done) {
client.MGET(["mget keys 1", "some random shit", "mget keys 2", "mget keys 3"], function (err, results) {
assert.strictEqual(4, results.length);
assert.strictEqual("mget val 1", results[0].toString());
assert.strictEqual(null, results[1]);
assert.strictEqual("mget val 2", results[2].toString());
assert.strictEqual("mget val 3", results[3].toString());
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

168
test/commands/mset.spec.js Normal file
View File

@@ -0,0 +1,168 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'mset' method", function () {
function removeMochaListener () {
var mochaListener = process.listeners('uncaughtException').pop();
process.removeListener('uncaughtException', mochaListener);
return mochaListener;
}
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key, value, key2, value2;
beforeEach(function () {
key = uuid.v4();
value = uuid.v4();
key2 = uuid.v4();
value2 = uuid.v4();
});
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("reports an error", function (done) {
client.mset(key, value, key2, value2, function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
done();
});
});
afterEach(function () {
client.end();
});
describe("and a callback is specified", function () {
describe("with valid parameters", function () {
it("sets the value correctly", function (done) {
client.mset(key, value, key2, value2, function (err, res) {
helper.isNotError()(err, res);
async.parallel([function (next) {
client.get(key, function (err, res) {
helper.isString(value)(err, res);
next();
});
}, function (next) {
client.get(key2, function (err, res) {
helper.isString(value2)(err, res);
next();
});
}], function (err) {
done(err);
});
});
});
});
describe("with undefined 'key' parameter and missing 'value' parameter", function () {
it("reports an error", function (done) {
client.mset(undefined, function (err, res) {
helper.isError()(err, null);
done();
});
});
});
describe("with undefined 'key' and defined 'value' parameters", function () {
it("reports an error", function () {
client.mset(undefined, value, undefined, value2, function (err, res) {
helper.isError()(err, null);
done();
});
});
});
});
describe("and no callback is specified", function () {
describe("with valid parameters", function () {
it("sets the value correctly", function (done) {
client.mset(key, value, key2, value2);
setTimeout(function () {
async.parallel([function (next) {
client.get(key, function (err, res) {
helper.isString(value)(err, res);
next();
});
}, function (next) {
client.get(key2, function (err, res) {
helper.isString(value2)(err, res);
next();
});
}], function (err) {
done(err);
});
}, 100);
});
});
describe("with undefined 'key' and missing 'value' parameter", function () {
// this behavior is different from the 'set' behavior.
it("throws an error", function (done) {
var mochaListener = removeMochaListener();
process.once('uncaughtException', function (err) {
process.on('uncaughtException', mochaListener);
helper.isError()(err, null);
return done();
});
client.mset();
});
});
describe("with undefined 'key' and defined 'value' parameters", function () {
it("throws an error", function () {
var mochaListener = removeMochaListener();
process.once('uncaughtException', function (err) {
process.on('uncaughtException', mochaListener);
helper.isError()(err, null);
});
client.mset(undefined, value, undefined, value2);
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,46 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'msetnx' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('if any keys exist entire operation fails', function (done) {
client.mset(["mset1", "val1", "mset2", "val2", "mset3", "val3"], helper.isString("OK"));
client.MSETNX(["mset3", "val3", "mset4", "val4"], helper.isNumber(0));
client.exists(["mset4"], helper.isNumber(0, done));
});
it('sets multiple keys if all keys are not set', function (done) {
client.MSETNX(["mset3", "val3", "mset4", "val4"], helper.isNumber(1));
client.exists(["mset3"], helper.isNumber(1));
client.exists(["mset3"], helper.isNumber(1, done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

250
test/commands/multi.spec.js Normal file
View File

@@ -0,0 +1,250 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'multi' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key, value;
beforeEach(function () {
key = uuid.v4();
value = uuid.v4();
});
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("reports an error", function (done) {
client.multi();
client.exec(function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err) {
return done(err);
})
});
});
afterEach(function () {
client.end();
});
it('roles back a transaction when one command in a sequence of commands fails', function (done) {
var name = "MULTI_1", multi1, multi2;
// Provoke an error at queue time
multi1 = client.multi();
multi1.mset("multifoo", "10", "multibar", "20", helper.isString("OK"));
multi1.set("foo2", helper.isError());
multi1.incr("multifoo", helper.isNumber(11));
multi1.incr("multibar", helper.isNumber(21));
multi1.exec(function () {
// Redis 2.6.5+ will abort transactions with errors
// see: http://redis.io/topics/transactions
var multibar_expected = 22;
var multifoo_expected = 12;
if (helper.serverVersionAtLeast(client, [2, 6, 5])) {
multibar_expected = 1;
multifoo_expected = 1;
}
// Confirm that the previous command, while containing an error, still worked.
multi2 = client.multi();
multi2.incr("multibar", helper.isNumber(multibar_expected));
multi2.incr("multifoo", helper.isNumber(multifoo_expected));
multi2.exec(function (err, replies) {
assert.strictEqual(multibar_expected, replies[0]);
assert.strictEqual(multifoo_expected, replies[1]);
return done();
});
});
});
// I'm unclear as to the difference between this test in the test above,
// perhaps @mranney can clarify?
it('roles back a transaction when an error was provoked at queue time', function (done) {
multi1 = client.multi();
multi1.mset("multifoo_8", "10", "multibar_8", "20", helper.isString("OK"));
multi1.set("foo2", helper.isError());
multi1.set("foo3", helper.isError());
multi1.incr("multifoo_8", helper.isNumber(11));
multi1.incr("multibar_8", helper.isNumber(21));
multi1.exec(function () {
// Redis 2.6.5+ will abort transactions with errors
// see: http://redis.io/topics/transactions
var multibar_expected = 22;
var multifoo_expected = 12;
if (helper.serverVersionAtLeast(client, [2, 6, 5])) {
multibar_expected = 1;
multifoo_expected = 1;
}
// Confirm that the previous command, while containing an error, still worked.
multi2 = client.multi();
multi2.incr("multibar_8", helper.isNumber(multibar_expected));
multi2.incr("multifoo_8", helper.isNumber(multifoo_expected));
multi2.exec(function (err, replies) {
assert.strictEqual(multibar_expected, replies[0]);
assert.strictEqual(multifoo_expected, replies[1]);
return done();
});
});
});
it('roles back a transaction when one command in an array of commands fails', function (done) {
// test nested multi-bulk replies
client.multi([
["mget", "multifoo", "multibar", function (err, res) {
assert.strictEqual(2, res.length);
assert.strictEqual("0", res[0].toString());
assert.strictEqual("0", res[1].toString());
}],
["set", "foo2", helper.isError()],
["incr", "multifoo", helper.isNumber(1)],
["incr", "multibar", helper.isNumber(1)]
]).exec(function (err, replies) {
if (helper.serverVersionAtLeast(client, [2, 6, 5])) {
assert.notEqual(err, null);
assert.equal(replies, undefined);
} else {
assert.strictEqual(2, replies[0].length);
assert.strictEqual("0", replies[0][0].toString());
assert.strictEqual("0", replies[0][1].toString());
assert.strictEqual("1", replies[1].toString());
assert.strictEqual("1", replies[2].toString());
}
return done();
});
});
it('handles multiple operations being applied to a set', function (done) {
client.sadd("some set", "mem 1");
client.sadd("some set", "mem 2");
client.sadd("some set", "mem 3");
client.sadd("some set", "mem 4");
// make sure empty mb reply works
client.del("some missing set");
client.smembers("some missing set", function (err, reply) {
// make sure empty mb reply works
assert.strictEqual(0, reply.length);
});
// test nested multi-bulk replies with empty mb elements.
client.multi([
["smembers", "some set"],
["del", "some set"],
["smembers", "some set"]
])
.scard("some set")
.exec(function (err, replies) {
assert.strictEqual(4, replies[0].length);
assert.strictEqual(0, replies[2].length);
return done();
});
});
it('allows multiple operations to be performed using a chaining API', function (done) {
client.multi()
.mset('some', '10', 'keys', '20')
.incr('some')
.incr('keys')
.mget('some', 'keys')
.exec(function (err, replies) {
assert.strictEqual(null, err);
assert.equal('OK', replies[0]);
assert.equal(11, replies[1]);
assert.equal(21, replies[2]);
assert.equal(11, replies[3][0].toString());
assert.equal(21, replies[3][1].toString());
return done();
});
});
it('allows an array to be provided indicating multiple operations to perform', function (done) {
// test nested multi-bulk replies with nulls.
client.multi([
["mget", ["multifoo", "some", "random value", "keys"]],
["incr", "multifoo"]
])
.exec(function (err, replies) {
assert.strictEqual(replies.length, 2);
assert.strictEqual(replies[0].length, 4);
return done();
});
});
it('allows multiple operations to be performed on a hash', function (done) {
client.multi()
.hmset("multihash", "a", "foo", "b", 1)
.hmset("multihash", {
extra: "fancy",
things: "here"
})
.hgetall("multihash")
.exec(function (err, replies) {
assert.strictEqual(null, err);
assert.equal("OK", replies[0]);
assert.equal(Object.keys(replies[2]).length, 4);
assert.equal("foo", replies[2].a);
assert.equal("1", replies[2].b);
assert.equal("fancy", replies[2].extra);
assert.equal("here", replies[2].things);
return done();
});
});
it('reports multiple exceptions when they occur', function (done) {
if (!helper.serverVersionAtLeast(client, [2, 6, 5])) return done();
client.multi().set("foo").exec(function (err, reply) {
assert(Array.isArray(err), "err should be an array");
assert.equal(2, err.length, "err should have 2 items");
assert(err[0].message.match(/ERR/), "First error message should contain ERR");
assert(err[1].message.match(/EXECABORT/), "First error message should contain EXECABORT");
return done();
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,42 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'randomkey' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('returns a random key', function (done) {
client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], helper.isString('OK'));
client.RANDOMKEY([], function (err, results) {
assert.strictEqual(true, /test keys.+/.test(results));
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,46 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'rename' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('populates the new key', function (done) {
client.set(['foo', 'bar'], helper.isString("OK"));
client.RENAME(["foo", "new foo"], helper.isString("OK"));
client.exists(["new foo"], helper.isNumber(1, done));
});
it('removes the old key', function (done) {
client.set(['foo', 'bar'], helper.isString("OK"));
client.RENAME(["foo", "new foo"], helper.isString("OK"));
client.exists(["foo"], helper.isNumber(0, done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,49 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'renamenx' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('renames the key if target does not yet exist', function (done) {
client.set('foo', 'bar', helper.isString('OK'));
client.renamenx('foo', 'foo2', helper.isNumber(1));
client.exists('foo', helper.isNumber(0));
client.exists(['foo2'], helper.isNumber(1, done));
});
it('does not rename the key if the target exists', function (done) {
client.set('foo', 'bar', helper.isString('OK'));
client.set('foo2', 'apple', helper.isString('OK'));
client.renamenx('foo', 'foo2', helper.isNumber(0));
client.exists('foo', helper.isNumber(1));
client.exists(['foo2'], helper.isNumber(1, done));
})
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,58 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'sadd' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('allows a single value to be added to the set', function (done) {
client.SADD('set0', 'member0', helper.isNumber(1));
client.smembers('set0', function (err, res) {
assert.ok(~res.indexOf('member0'));
return done(err);
});
});
it('does not add the same value to the set twice', function (done) {
client.sadd('set0', 'member0', helper.isNumber(1));
client.SADD('set0', 'member0', helper.isNumber(0, done));
});
it('allows multiple values to be added to the set', function (done) {
client.sadd("set0", ["member0", "member1", "member2"], helper.isNumber(3));
client.smembers("set0", function (err, res) {
assert.strictEqual(res.length, 3);
assert.ok(~res.indexOf("member0"));
assert.ok(~res.indexOf("member1"));
assert.ok(~res.indexOf("member2"));
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,39 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'scard' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('returns the number of values in a set', function (done) {
client.sadd('foo', [1, 2, 3], helper.isNumber(3));
client.scard('foo', helper.isNumber(3, done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,68 @@
var assert = require("assert");
var config = require("../lib/config");
var crypto = require("crypto");
var helper = require("../helper");
var redis = config.redis;
describe("The 'script' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
var command = "return 99";
var commandSha = crypto.createHash('sha1').update(command).digest('hex');
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err) {
if (!helper.serverVersionAtLeast(client, [2, 6, 0])) {
err = Error('script not supported in redis <= 2.6.0')
}
return done(err);
})
});
});
afterEach(function () {
client.end();
});
it("loads script with client.script('load')", function (done) {
client.script("load", command, function(err, result) {
assert.strictEqual(result, commandSha);
return done();
});
});
it('allows a loaded script to be evaluated', function (done) {
client.evalsha(commandSha, 0, helper.isString('99', done));
})
it('allows a script to be loaded as part of a chained transaction', function (done) {
client.multi().script("load", command).exec(function(err, result) {
assert.strictEqual(result[0], commandSha);
return done()
})
})
it("allows a script to be loaded using a transaction's array syntax", function (done) {
client.multi([['script', 'load', command]]).exec(function(err, result) {
assert.strictEqual(result[0], commandSha);
return done()
})
})
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,123 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
describe("The 'select' method", function () {
function removeMochaListener () {
var mochaListener = process.listeners('uncaughtException').pop();
process.removeListener('uncaughtException', mochaListener);
return mochaListener;
}
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("throws an error if redis is not connected", function (done) {
client.select(1, function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () { done(); });
});
afterEach(function () {
client.end();
});
it("changes the database and calls the callback", function (done) {
// default value of null means database 0 will be used.
assert.strictEqual(client.selected_db, null, "default db should be null");
client.select(1, function (err, res) {
helper.isNotError()(err, res);
assert.strictEqual(client.selected_db, 1, "db should be 1 after select");
done();
});
});
describe("and a callback is specified", function () {
describe("with a valid db index", function () {
it("selects the appropriate database", function (done) {
assert.strictEqual(client.selected_db, null, "default db should be null");
client.select(1, function () {
assert.equal(client.selected_db, 1, "we should have selected the new valid DB");
return done();
});
});
});
describe("with an invalid db index", function () {
it("emits an error", function (done) {
assert.strictEqual(client.selected_db, null, "default db should be null");
client.select(9999, function (err) {
assert.equal(err.message, 'ERR invalid DB index')
return done();
});
});
});
});
describe("and no callback is specified", function () {
describe("with a valid db index", function () {
it("selects the appropriate database", function (done) {
assert.strictEqual(client.selected_db, null, "default db should be null");
client.select(1);
setTimeout(function () {
assert.equal(client.selected_db, 1, "we should have selected the new valid DB");
return done();
}, 100);
});
});
describe("with an invalid db index", function () {
it("throws an error when callback not provided", function (done) {
var mochaListener = removeMochaListener();
assert.strictEqual(client.selected_db, null, "default db should be null");
process.once('uncaughtException', function (err) {
process.on('uncaughtException', mochaListener);
assert.equal(err.message, 'ERR invalid DB index');
return done();
});
client.select(9999);
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

165
test/commands/set.spec.js Normal file
View File

@@ -0,0 +1,165 @@
var async = require('async');
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
var uuid = require('uuid');
describe("The 'set' method", function () {
function removeMochaListener () {
var mochaListener = process.listeners('uncaughtException').pop();
process.removeListener('uncaughtException', mochaListener);
return mochaListener;
}
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var key, value;
beforeEach(function () {
key = uuid.v4();
value = uuid.v4();
});
describe("when not connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.quit();
});
client.on('end', function () {
return done();
});
});
it("reports an error", function (done) {
client.set(key, value, function (err, res) {
assert.equal(err.message, 'Redis connection gone from end event.');
done();
});
});
});
describe("when connected", function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
done();
});
});
afterEach(function () {
client.end();
});
describe("and a callback is specified", function () {
describe("with valid parameters", function () {
it("sets the value correctly", function (done) {
client.set(key, value, function (err, res) {
helper.isNotError()(err, res);
client.get(key, function (err, res) {
helper.isString(value)(err, res);
done();
});
});
});
});
describe("with undefined 'key' and missing 'value' parameter", function () {
it("reports an error", function (done) {
client.set(undefined, function (err, res) {
helper.isError()(err, null);
done();
});
});
});
describe("with undefined 'key' and defined 'value' parameters", function () {
it("reports an error", function () {
client.set(undefined, value, function (err, res) {
helper.isError()(err, null);
done();
});
});
});
});
describe("and no callback is specified", function () {
describe("with valid parameters", function () {
it("sets the value correctly", function (done) {
client.set(key, value);
setTimeout(function () {
client.get(key, function (err, res) {
helper.isString(value)(err, res);
done();
});
}, 100);
});
});
describe("with undefined 'key' and missing 'value' parameter", function () {
it("does not emit an error", function (done) {
this.timeout(200);
client.once("error", function (err) {
helper.isError()(err, null);
return done(err);
});
client.set();
setTimeout(function () {
done();
}, 100);
});
it("does not throw an error", function (done) {
this.timeout(200);
var mochaListener = removeMochaListener();
process.once('uncaughtException', function (err) {
process.on('uncaughtException', mochaListener);
return done(err);
});
client.set();
setTimeout(function () {
done();
}, 100);
});
});
describe("with undefined 'key' and defined 'value' parameters", function () {
it("throws an error", function () {
var mochaListener = removeMochaListener();
process.once('uncaughtException', function (err) {
process.on('uncaughtException', mochaListener);
helper.isError()(err, null);
});
client.set(undefined, value);
});
});
});
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,47 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'setex' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('sets a key with an expiry', function (done) {
client.SETEX(["setex key", "100", "setex val"], helper.isString("OK"));
client.exists(["setex key"], helper.isNumber(1));
client.ttl(['setex key'], function (err, ttl) {
assert.ok(ttl > 0);
return done();
});
});
it('returns an error if no value is provided', function (done) {
client.SETEX(["setex key", "100", undefined], helper.isError(done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,46 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'setnx' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('sets key if it does not have a value', function (done) {
client.setnx('foo', 'banana', helper.isNumber(1));
client.get('foo', helper.isString('banana', done));
});
it('does not set key if it already has a value', function (done) {
client.set('foo', 'bar', helper.isString('OK'));
client.setnx('foo', 'banana', helper.isNumber(0));
client.get('foo', helper.isString('bar', done));
return done();
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,70 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'sinter' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('handles two sets being intersected', function (done) {
client.sadd('sa', 'a', helper.isNumber(1));
client.sadd('sa', 'b', helper.isNumber(1));
client.sadd('sa', 'c', helper.isNumber(1));
client.sadd('sb', 'b', helper.isNumber(1));
client.sadd('sb', 'c', helper.isNumber(1));
client.sadd('sb', 'd', helper.isNumber(1));
client.sinter('sa', 'sb', function (err, intersection) {
assert.equal(intersection.length, 2);
assert.deepEqual(intersection.sort(), [ 'b', 'c' ]);
return done(err);
});
});
it('handles three sets being intersected', function (done) {
client.sadd('sa', 'a', helper.isNumber(1));
client.sadd('sa', 'b', helper.isNumber(1));
client.sadd('sa', 'c', helper.isNumber(1));
client.sadd('sb', 'b', helper.isNumber(1));
client.sadd('sb', 'c', helper.isNumber(1));
client.sadd('sb', 'd', helper.isNumber(1));
client.sadd('sc', 'c', helper.isNumber(1));
client.sadd('sc', 'd', helper.isNumber(1));
client.sadd('sc', 'e', helper.isNumber(1));
client.sinter('sa', 'sb', 'sc', function (err, intersection) {
assert.equal(intersection.length, 1);
assert.equal(intersection[0], 'c');
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,43 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'sismember' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('returns 0 if the value is not in the set', function (done) {
client.sismember('foo', 'banana', helper.isNumber(0, done));
});
it('returns 1 if the value is in the set', function (done) {
client.sadd('foo', 'banana', helper.isNumber(1));
client.sismember('foo', 'banana', helper.isNumber(1, done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

128
test/commands/sort.spec.js Normal file
View File

@@ -0,0 +1,128 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'sort' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb();
setupData(client, done)
});
});
describe('alphabetical', function () {
it('sorts in ascending alphabetical order', function (done) {
client.sort('y', 'asc', 'alpha', function (err, sorted) {
assert.deepEqual(sorted, ['a', 'b', 'c', 'd']);
return done(err);
});
});
it('sorts in descending alphabetical order', function (done) {
client.sort('y', 'desc', 'alpha', function (err, sorted) {
assert.deepEqual(sorted, ['d', 'c', 'b', 'a']);
return done(err);
});
});
});
describe('numeric', function () {
it('sorts in ascending numeric order', function (done) {
client.sort('x', 'asc', function (err, sorted) {
assert.deepEqual(sorted, [2, 3, 4, 9]);
return done(err);
});
});
it('sorts in descending numeric order', function (done) {
client.sort('x', 'desc', function (err, sorted) {
assert.deepEqual(sorted, [9, 4, 3, 2]);
return done(err);
});
});
});
describe('pattern', function () {
it('handles sorting with a pattern', function (done) {
client.sort('x', 'by', 'w*', 'asc', function (err, sorted) {
assert.deepEqual(sorted, [3, 9, 4, 2]);
return done(err);
});
});
it("handles sorting with a 'by' pattern and 1 'get' pattern", function (done) {
client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', function (err, sorted) {
assert.deepEqual(sorted, ['foo', 'bar', 'baz', 'buz']);
return done(err);
});
});
it("handles sorting with a 'by' pattern and 2 'get' patterns", function (done) {
client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', 'get', 'p*', function (err, sorted) {
assert.deepEqual(sorted, ['foo', 'bux', 'bar', 'tux', 'baz', 'lux', 'buz', 'qux']);
return done(err);
});
});
it("sorting with a 'by' pattern and 2 'get' patterns and stores results", function (done) {
client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', 'get', 'p*', 'store', 'bacon', function (err) {
if (err) return done(err);
});
client.lrange('bacon', 0, -1, function (err, values) {
assert.deepEqual(values, ['foo', 'bux', 'bar', 'tux', 'baz', 'lux', 'buz', 'qux']);
return done(err);
});
});
});
afterEach(function () {
client.end();
});
});
}
function setupData(client, done) {
client.rpush('y', 'd');
client.rpush('y', 'b');
client.rpush('y', 'a');
client.rpush('y', 'c');
client.rpush('x', '3');
client.rpush('x', '9');
client.rpush('x', '2');
client.rpush('x', '4');
client.set('w3', '4');
client.set('w9', '5');
client.set('w2', '12');
client.set('w4', '6');
client.set('o2', 'buz');
client.set('o3', 'foo');
client.set('o4', 'baz');
client.set('o9', 'bar');
client.set('p2', 'qux');
client.set('p3', 'bux');
client.set('p4', 'lux');
client.set('p9', 'tux', done);
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,66 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'srem' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('removes a value', function (done) {
client.sadd('set0', 'member0', helper.isNumber(1));
client.srem('set0', 'member0', helper.isNumber(1));
client.scard('set0', helper.isNumber(0, done));
});
it('handles attempting to remove a missing value', function (done) {
client.srem('set0', 'member0', helper.isNumber(0, done));
});
it('allows multiple values to be removed', function (done) {
client.sadd("set0", ["member0", "member1", "member2"], helper.isNumber(3));
client.SREM("set0", ["member1", "member2"], helper.isNumber(2));
client.smembers("set0", function (err, res) {
assert.strictEqual(res.length, 1);
assert.ok(~res.indexOf("member0"));
return done(err);
});
});
it('handles a value missing from the set of values being removed', function (done) {
client.sadd("set0", ["member0", "member1", "member2"], helper.isNumber(3));
client.SREM("set0", ["member3", "member4"], helper.isNumber(0));
client.smembers("set0", function (err, res) {
assert.strictEqual(res.length, 3);
assert.ok(~res.indexOf("member0"));
assert.ok(~res.indexOf("member1"));
assert.ok(~res.indexOf("member2"));
return done(err);
});
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,63 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'type' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(done);
});
});
it('reports string type', function (done) {
client.set(["string key", "should be a string"], helper.isString("OK"));
client.TYPE(["string key"], helper.isString("string", done));
});
it('reports list type', function (done) {
client.rpush(["list key", "should be a list"], helper.isNumber(1));
client.TYPE(["list key"], helper.isString("list", done));
});
it('reports set type', function (done) {
client.sadd(["set key", "should be a set"], helper.isNumber(1));
client.TYPE(["set key"], helper.isString("set", done));
});
it('reports zset type', function (done) {
client.zadd(["zset key", "10.0", "should be a zset"], helper.isNumber(1));
client.TYPE(["zset key"], helper.isString("zset", done));
});
it('reports hash type', function (done) {
client.hset(["hash key", "hashtest", "should be a hash"], helper.isNumber(1));
client.TYPE(["hash key"], helper.isString("hash", done));
});
it('reports none for null key', function (done) {
client.TYPE("not here yet", helper.isString("none", done));
});
afterEach(function () {
client.end();
});
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});

View File

@@ -0,0 +1,68 @@
var assert = require("assert");
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe("The 'watch' method", function () {
function allTests(parser, ip) {
var args = config.configureClient(parser, ip);
var watched = 'foobar'
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("error", done);
client.once("connect", function () {
client.flushdb(function (err) {
if (!helper.serverVersionAtLeast(client, [2, 2, 0])) {
err = Error('some watch commands not supported in redis <= 2.2.0')
}
return done(err);
})
});
});
afterEach(function () {
client.end();
});
it('does not execute transaction if watched key was modified prior to execution', function (done) {
client.watch(watched);
client.incr(watched);
multi = client.multi();
multi.incr(watched);
multi.exec(helper.isNull(done));
})
it('successfully modifies other keys independently of transaction', function (done) {
client.set("unwatched", 200);
client.set(watched, 0);
client.watch(watched);
client.incr(watched);
var multi = client.multi()
.incr(watched)
.exec(function (err, replies) {
assert.strictEqual(replies, null, "Aborted transaction multi-bulk reply should be null.");
client.get("unwatched", function (err, reply) {
assert.equal(reply, 200, "Expected 200, got " + reply);
return done(err)
});
});
})
});
}
['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});
});