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

Add tests and improve older tests

This commit is contained in:
Ruben Bridgewater
2015-12-30 16:14:23 +01:00
parent 2cd3818ea9
commit 5ef24a90b6
12 changed files with 199 additions and 98 deletions

View File

@@ -45,12 +45,40 @@ describe("The 'hset' method", function () {
});
});
it('does not error when a buffer and array are set as fields on the same hash', function (done) {
it('throws a error if someone passed a array either as field or as value', function (done) {
var hash = "test hash";
var field = "array";
// This would be converted to "array contents" but if you use more than one entry,
// it'll result in e.g. "array contents,second content" and this is not supported and considered harmful
var value = ["array contents"];
try {
client.HMSET(hash, field, value);
throw new Error('test failed');
} catch (err) {
if (/invalid data/.test(err.message)) {
done();
} else {
done(err);
}
}
});
it('does not error when a buffer and date are set as values 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"];
var field2 = "date";
var value2 = new Date();
client.HMSET(hash, field1, value1, field2, value2, helper.isString("OK", done));
});
it('does not error when a buffer and date are set as fields on the same hash', function (done) {
var hash = "test hash";
var value1 = "buffer";
var field1 = new Buffer("abcdefghij");
var value2 = "date";
var field2 = new Date();
client.HMSET(hash, field1, value1, field2, value2, helper.isString("OK", done));
});

View File

@@ -0,0 +1,51 @@
'use strict';
var assert = require('assert');
var config = require("../lib/config");
var helper = require('../helper');
var redis = config.redis;
describe("The 'info' method", function () {
helper.allTests(function(parser, ip, args) {
describe("using " + parser + " and " + ip, function () {
var client;
before(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("ready", function () {
client.flushall(done);
});
});
after(function () {
client.end(true);
});
it("update server_info after a info command", function (done) {
client.set('foo', 'bar');
client.info();
client.select(2, function () {
assert.strictEqual(client.server_info.db2, undefined);
});
client.set('foo', 'bar');
client.info();
setTimeout(function () {
assert.strictEqual(typeof client.server_info.db2, 'object');
done();
}, 150);
});
it("emit error after a failure", function (done) {
client.info();
client.once('error', function (err) {
assert.strictEqual(err.code, 'UNCERTAIN_STATE');
assert.strictEqual(err.command, 'INFO');
done();
});
client.stream.destroy();
});
});
});
});

View File

@@ -14,10 +14,9 @@ describe("The 'keys' method", function () {
var client;
beforeEach(function (done) {
args = args || {};
client = redis.createClient.apply(redis.createClient, args);
client.once("ready", function () {
client.flushdb(done);
client.flushall(done);
});
});

View File

@@ -37,7 +37,9 @@ describe("The 'select' method", function () {
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("ready", function () { done(); });
client.once("ready", function () {
client.flushdb(done);
});
});
afterEach(function () {
@@ -46,7 +48,7 @@ describe("The 'select' method", function () {
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");
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
var buffering = client.SELECT(1, function (err, res) {
helper.isNotError()(err, res);
assert.strictEqual(client.selected_db, 1, "db should be 1 after select");
@@ -58,7 +60,7 @@ describe("The 'select' method", function () {
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");
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
client.select(1, function (err) {
assert.equal(err, null);
assert.equal(client.selected_db, 1, "we should have selected the new valid DB");
@@ -69,7 +71,7 @@ describe("The 'select' method", function () {
describe("with an invalid db index", function () {
it("returns an error", function (done) {
assert.strictEqual(client.selected_db, null, "default db should be null");
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
client.select(9999, function (err) {
assert.equal(err.code, 'ERR');
assert.equal(err.message, 'ERR invalid DB index');
@@ -82,7 +84,7 @@ describe("The 'select' method", function () {
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");
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
client.select(1);
setTimeout(function () {
assert.equal(client.selected_db, 1, "we should have selected the new valid DB");
@@ -93,7 +95,7 @@ describe("The 'select' method", function () {
describe("with an invalid db index", function () {
it("emits an error when callback not provided", function (done) {
assert.strictEqual(client.selected_db, null, "default db should be null");
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
client.on('error', function (err) {
assert.strictEqual(err.command, 'SELECT');
@@ -105,6 +107,21 @@ describe("The 'select' method", function () {
});
});
});
describe("reconnection occurs", function () {
it("selects the appropriate database after a reconnect", function (done) {
assert.strictEqual(client.selected_db, undefined, "default db should be undefined");
client.select(3);
client.set('foo', 'bar', function () {
client.stream.destroy();
});
client.once('ready', function () {
assert.strictEqual(client.selected_db, 3);
assert(typeof client.server_info.db3 === 'object');
done();
});
});
});
});
});
});

View File

@@ -81,32 +81,17 @@ describe("The 'set' method", 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);
client.get(key, helper.isString(value, done));
});
it("sets the value correctly even if the callback is explicitly set to undefined", function (done) {
client.set(key, value, undefined);
setTimeout(function () {
client.get(key, function (err, res) {
helper.isString(value)(err, res);
done();
});
}, 100);
client.get(key, helper.isString(value, done));
});
it("sets the value correctly with the array syntax", function (done) {
client.set([key, value]);
setTimeout(function () {
client.get(key, function (err, res) {
helper.isString(value)(err, res);
done();
});
}, 100);
client.get(key, helper.isString(value, done));
});
});
@@ -121,6 +106,12 @@ describe("The 'set' method", function () {
});
});
// TODO: This test has to be refactored from v.3.0 on to expect an error instead
it("converts null to 'null'", function (done) {
client.set('foo', null);
client.get('foo', helper.isString('null', done));
});
it("emit an error with only the key set", function (done) {
client.on('error', function (err) {
assert.equal(err.message, "ERR wrong number of arguments for 'set' command");

View File

@@ -1,44 +0,0 @@
'use strict';
var assert = require('assert');
var config = require("../lib/config");
var helper = require("../helper");
var redis = config.redis;
describe.skip("The 'sync' method", function () {
helper.allTests(function(parser, ip, args) {
describe("using " + parser + " and " + ip, function () {
var client;
beforeEach(function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.once("ready", function () {
client.flushdb(done);
});
});
// This produces a parser error
// "Protocol error, got "K" as reply type byte"
// I'm uncertain if this is correct behavior or not
// TODO: Fix the command queue state error occuring
it('try to sync with the server and fail other commands', function (done) {
client.on('error', function(err) {
assert.equal(err.message, 'Protocol error, got "K" as reply type byte');
assert.equal(err.command, 'SET');
done();
});
client.sync(function(err, res) {
assert.equal(err, null);
assert(!!res);
});
client.set('foo', 'bar');
});
afterEach(function () {
client.end(true);
});
});
});
});