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) { (function(module) {
var redis = require('redis'), 'use strict';
var RedisDB,
redis = require('redis'),
utils = require('./../public/src/utils.js'), utils = require('./../public/src/utils.js'),
winston = require('winston'), winston = require('winston'),
nconf = require('nconf'), nconf = require('nconf'),
@ -7,18 +10,19 @@
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) { 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 */ /* 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 {
/* Else, connect over tcp/ip */ /* 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')) { if (nconf.get('redis:password')) {
RedisDB.exports.auth(nconf.get('redis:password')); RedisDB.auth(nconf.get('redis:password'));
} }
if( (db = nconf.get('redis:database')) ){ var db = nconf.get('redis:database');
RedisDB.exports.select(db, function(error){ if (db){
RedisDB.select(db, function(error){
if(error !== null){ if(error !== null){
winston.err(error); winston.err(error);
if (global.env !== 'production') { if (global.env !== 'production') {
@ -28,24 +32,24 @@
}); });
} }
RedisDB.exports.handle = function(error) { RedisDB.handle = function(error) {
if (error !== null) { if (error !== null) {
winston.err(error); winston.err(error);
if (global.env !== 'production') { if (global.env !== 'production') {
throw new Error(error); throw new Error(error);
} }
} }
} };
/* /*
* A possibly more efficient way of doing multiple sismember calls * 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(); var tempkey = key + ':temp:' + utils.generateUUID();
RedisDB.exports.sadd(tempkey, needles, function() { RedisDB.sadd(tempkey, needles, function() {
RedisDB.exports.sinter(key, tempkey, function(err, data) { RedisDB.sinter(key, tempkey, function(err, data) {
RedisDB.exports.del(tempkey); RedisDB.del(tempkey);
callback(err, data); callback(err, data);
}); });
}); });
@ -54,8 +58,8 @@
/* /*
* gets fields of a hash as an object instead of an array * gets fields of a hash as an object instead of an array
*/ */
RedisDB.exports.hmgetObject = function(key, fields, callback) { RedisDB.hmgetObject = function(key, fields, callback) {
RedisDB.exports.hmget(key, fields, function(err, data) { RedisDB.hmget(key, fields, function(err, data) {
if (err === null) { if (err === null) {
var returnData = {}; var returnData = {};
@ -69,8 +73,8 @@
callback(err, null); callback(err, null);
} }
}); });
} };
module.exports = RedisDB;
}(module)); }(module));

@ -1,16 +1,13 @@
// this test currently needs to talk to the redis database. // this test currently needs to talk to the redis database.
// get the redis config info from root directory's config.json: // get the redis config info from root directory's config.json:
var path = require('path'), var winston = require('winston');
nconf = require('nconf'),
winston = require('winston');
nconf.file({ file: path.join(__dirname, '../config.json') });
process.on('uncaughtException', function (err) { process.on('uncaughtException', function (err) {
winston.error('Encountered error while running test suite: ' + err.message); winston.error('Encountered error while running test suite: ' + err.message);
}); });
var assert = require('assert'), 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 // 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 // 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