From 14c3bb7d63e6d77de93029386f2a9c79aa5ced02 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Wed, 30 Oct 2013 00:54:15 +0200 Subject: [PATCH] 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));