Merge pull request #469 from deniswolf/redis-refactoring

Use separate db for testing
v1.18.x
Julian Lam 11 years ago
commit e8c4bda984

@ -0,0 +1,69 @@
/**
* Redis Mock - wrapper for redis.js, makes system use separate test db, instead of production
* ATTENTION: testing db is flushed before every use!
*/
(function(module) {
'use strict';
var RedisDB,
redis = require('redis'),
utils = require('./../public/src/utils.js'),
path = require('path'),
nconf = require('nconf'),
winston = require('winston'),
errorText;
nconf.file({ file: path.join(__dirname, '../config.json') });
var testDbConfig = nconf.get('redis_test'),
productionDbConfig = nconf.get('redis');
if(!testDbConfig){
errorText = 'redis_test database is not defined';
winston.info(
"\n===========================================================\n"+
"Please, add parameters for test database in config.js\n"+
"For example:\n"+
'"redis_test": {' + '\n' +
' "host": "127.0.0.1",' + '\n' +
' "port": "6379",' + '\n' +
' "password": "",' + '\n' +
' "database": "1"' + '\n' +
'}\n'+
"==========================================================="
);
winston.error(errorText);
throw new Error(errorText);
}
if( testDbConfig.database === productionDbConfig.database &&
testDbConfig.host === productionDbConfig.host &&
testDbConfig.port === productionDbConfig.port
){
errorText = 'redis_test database has the same config as production db';
winston.error(errorText);
throw new Error(errorText);
}
nconf.set('redis',testDbConfig);
RedisDB = require('../src/redis.js');
//Clean up
RedisDB.send_command('flushdb', [], function(error){
if(error){
winston.error(error);
throw new Error(error);
} else {
winston.info('redis_test db flushed');
}
});
//TODO: data seeding, if needed at all
module.exports = RedisDB;
}(module));

@ -1,5 +1,8 @@
(function(RedisDB) {
var redis = require('redis'),
(function(module) {
'use strict';
var RedisDB,
redis = require('redis'),
utils = require('./../public/src/utils.js'),
winston = require('winston'),
nconf = require('nconf'),
@ -7,45 +10,46 @@
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) {
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
RedisDB.exports = redis.createClient(nconf.get('redis:host'))
RedisDB = redis.createClient(nconf.get('redis:host'));
} else {
/* Else, connect over tcp/ip */
RedisDB.exports = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
RedisDB = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
}
if (nconf.get('redis:password')) {
RedisDB.exports.auth(nconf.get('redis:password'));
RedisDB.auth(nconf.get('redis:password'));
}
if( (db = nconf.get('redis:database')) ){
RedisDB.exports.select(db, function(error){
var db = nconf.get('redis:database');
if (db){
RedisDB.select(db, function(error){
if(error !== null){
winston.err(error);
if (global.env !== 'production') {
throw new Error(error);
}
}
});
});
}
RedisDB.exports.handle = function(error) {
RedisDB.handle = function(error) {
if (error !== null) {
winston.err(error);
if (global.env !== 'production') {
throw new Error(error);
}
}
}
};
/*
* A possibly more efficient way of doing multiple sismember calls
*/
RedisDB.exports.sismembers = function(key, needles, callback) {
RedisDB.sismembers = function(key, needles, callback) {
var tempkey = key + ':temp:' + utils.generateUUID();
RedisDB.exports.sadd(tempkey, needles, function() {
RedisDB.exports.sinter(key, tempkey, function(err, data) {
RedisDB.exports.del(tempkey);
RedisDB.sadd(tempkey, needles, function() {
RedisDB.sinter(key, tempkey, function(err, data) {
RedisDB.del(tempkey);
callback(err, data);
});
});
@ -54,8 +58,8 @@
/*
* gets fields of a hash as an object instead of an array
*/
RedisDB.exports.hmgetObject = function(key, fields, callback) {
RedisDB.exports.hmget(key, fields, function(err, data) {
RedisDB.hmgetObject = function(key, fields, callback) {
RedisDB.hmget(key, fields, function(err, data) {
if (err === null) {
var returnData = {};
@ -69,8 +73,8 @@
callback(err, null);
}
});
}
};
module.exports = RedisDB;
}(module));
}(module));

@ -1,16 +1,13 @@
// this test currently needs to talk to the redis database.
// get the redis config info from root directory's config.json:
var path = require('path'),
nconf = require('nconf'),
winston = require('winston');
nconf.file({ file: path.join(__dirname, '../config.json') });
var winston = require('winston');
process.on('uncaughtException', function (err) {
winston.error('Encountered error while running test suite: ' + err.message);
});
var assert = require('assert'),
RDB = require('../src/redis');
RDB = require('../mocks/redismock');
// Reds is not technically used in this test suite, but its invocation is required to stop the included
// libraries from trying to connect to the default Redis host/port

@ -0,0 +1,10 @@
var assert = require('assert');
describe('Test database', function() {
it('should work', function(){
assert.doesNotThrow(function(){
var RDB = require('../mocks/redismock');
});
});
});
Loading…
Cancel
Save