Skip to content

porting tests from hand-rolled artisinal test-suite to mocha #793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ sudo: true
node_js:
- "0.10"
- "0.12"
- "iojs"
before_install:
- 'printf ''bind ::1 127.0.0.1\nunixsocket /tmp/redis.sock\ndaemonize yes\nunixsocketperm 777'' >> /tmp/redis.conf'
before_script:
- sudo redis-server /tmp/redis.conf
- "iojs-v2"
after_success: npm run coverage
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
"main": "./index.js",
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test": "nyc ./test/run.sh"
"test": "nyc ./node_modules/.bin/_mocha ./test/*.js ./test/commands/*.js ./test/parser/*.js --timeout=8000"
},
"devDependencies": {
"async": "^1.3.0",
"colors": "~0.6.0-1",
"coveralls": "^2.11.2",
"hiredis": "^0.4.0",
"metrics": ">=0.1.5",
"mocha": "^2.2.5",
"nyc": "^3.0.0",
"underscore": "~1.4.4"
"tcp-port-used": "^0.1.2",
"underscore": "~1.4.4",
"uuid": "^2.0.1"
},
"repository": {
"type": "git",
Expand Down
90 changes: 90 additions & 0 deletions test/auth.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var assert = require("assert");
var config = require("./lib/config");
var helper = require('./helper')
var path = require('path');
var redis = config.redis;

describe("client authentication", function () {
before(function (done) {
helper.stopRedis(function () {
helper.startRedis('./conf/password.conf', done);
});
});

function allTests(parser, ip) {
describe("using " + parser + " and " + ip, function () {
var args = config.configureClient(parser, ip);
var auth = 'porkchopsandwiches';
var client = null;

afterEach(function () {
client.end();
});

it("allows auth to be provided with 'auth' method", function (done) {
client = redis.createClient.apply(redis.createClient, args);
client.auth(auth, function (err, res) {
assert.strictEqual(null, err);
assert.strictEqual("OK", res.toString());
return done(err);
});
});

it("raises error when auth is bad", function (done) {
client = redis.createClient.apply(redis.createClient, args);

client.once('error', function (error) {
assert.ok(/ERR invalid password/.test(error))
return done();
});

client.auth(auth + 'bad');
});

if (ip === 'IPv4')
it('allows auth to be provided as config option for client', function (done) {
client = redis.createClient('redis://foo:' + auth + '@' + config.HOST[ip] + ':' + config.PORT);
client.on("ready", function () {
return done();
});
});

it('allows auth to be provided as part of redis url', function (done) {
var args = config.configureClient(parser, ip, {
auth_pass: auth
});
client = redis.createClient.apply(redis.createClient, args);
client.on("ready", function () {
return done();
});
});

it('reconnects with appropriate authentication', function (done) {
var readyCount = 0;
client = redis.createClient.apply(redis.createClient, args);
client.auth(auth);
client.on("ready", function () {
readyCount++;
if (readyCount === 1) {
client.stream.destroy();
} else {
return done();
}
});
});
});
}

['javascript', 'hiredis'].forEach(function (parser) {
allTests(parser, "/tmp/redis.sock");
['IPv4', 'IPv6'].forEach(function (ip) {
allTests(parser, ip);
})
});

after(function (done) {
helper.stopRedis(function () {
helper.startRedis('./conf/redis.conf', done);
});
});
});
61 changes: 61 additions & 0 deletions test/commands/client.spec.js
Original file line number Diff line number Diff line change
@@ -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);
})
});
});
106 changes: 106 additions & 0 deletions test/commands/dbsize.spec.js
Original file line number Diff line number Diff line change
@@ -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 changes: 51 additions & 0 deletions test/commands/del.spec.js
Original file line number Diff line number Diff line change
@@ -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);
})
});
});
Loading