From 14c3bb7d63e6d77de93029386f2a9c79aa5ced02 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Wed, 30 Oct 2013 00:54:15 +0200 Subject: [PATCH 1/5] redismock.js - wrapper for using test db instead of production --- mocks/redismock.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 mocks/redismock.js diff --git a/mocks/redismock.js b/mocks/redismock.js new file mode 100644 index 0000000000..89b5386726 --- /dev/null +++ b/mocks/redismock.js @@ -0,0 +1,57 @@ +/** + * 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.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)); From ef5548a749529fc7827e8444ac365eee0347ec09 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Wed, 30 Oct 2013 01:03:45 +0200 Subject: [PATCH 2/5] tests: categories.js is using testing db now --- tests/categories.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/categories.js b/tests/categories.js index 5d03ebe167..032cd694c7 100644 --- a/tests/categories.js +++ b/tests/categories.js @@ -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 From db2239497655f42c6d0ec0cc6f5eeb5a11d07214 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Wed, 30 Oct 2013 01:17:05 +0200 Subject: [PATCH 3/5] redis.js - cleanup --- src/redis.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/redis.js b/src/redis.js index b5852f92b3..3738ae6d12 100644 --- a/src/redis.js +++ b/src/redis.js @@ -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)); \ No newline at end of file From 8ff656430dd8bc845bf4e8135d7cee3c23f99cc5 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Wed, 30 Oct 2013 01:47:27 +0200 Subject: [PATCH 4/5] tests: check if we are able to use test db wrapper at all --- tests/redis.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/redis.js diff --git a/tests/redis.js b/tests/redis.js new file mode 100644 index 0000000000..0af4f1a120 --- /dev/null +++ b/tests/redis.js @@ -0,0 +1,10 @@ +var assert = require('assert'); + + +describe('Test database', function() { + it('should work', function(){ + assert.doesNotThrow(function(){ + var RDB = require('../mocks/redismock'); + }); + }); +}); From 89ec677d54f3a8a55e7f8dbf2b8c8a2131774c50 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Fri, 1 Nov 2013 02:48:13 +0200 Subject: [PATCH 5/5] redismock: notify developer about test db config --- mocks/redismock.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mocks/redismock.js b/mocks/redismock.js index 89b5386726..6eec84e37d 100644 --- a/mocks/redismock.js +++ b/mocks/redismock.js @@ -21,6 +21,18 @@ 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); }