From 0da141e7bcb2eee49927060ec28cccf207ec70b5 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 6 Dec 2013 13:21:21 -0500 Subject: [PATCH] removed redismock, added database mocked, fixed tests to work with dbal --- mocks/databasemock.js | 81 +++++++++++++++++++++++++++++++++ mocks/redismock.js | 69 ---------------------------- src/database/mongo.js | 37 +++++++++++---- src/websockets.js | 6 ++- tests/categories.js | 18 ++------ tests/{redis.js => database.js} | 2 +- tests/topics.js | 17 ++----- tests/user.js | 10 +--- 8 files changed, 124 insertions(+), 116 deletions(-) create mode 100644 mocks/databasemock.js delete mode 100644 mocks/redismock.js rename tests/{redis.js => database.js} (76%) diff --git a/mocks/databasemock.js b/mocks/databasemock.js new file mode 100644 index 0000000000..89e5f850e7 --- /dev/null +++ b/mocks/databasemock.js @@ -0,0 +1,81 @@ +/** + * Database Mock - wrapper for database.js, makes system use separate test db, instead of production + * ATTENTION: testing db is flushed before every use! + */ + +(function(module) { + 'use strict'; + + var 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 dbType = nconf.get('database'), + testDbConfig = nconf.get('test_database'), + productionDbConfig = nconf.get(dbType); + + if(!testDbConfig){ + errorText = 'test_database is not defined'; + winston.info( + "\n===========================================================\n"+ + "Please, add parameters for test database in config.json\n"+ + "For example (redis):\n"+ + '"test_database": {' + '\n' + + ' "host": "127.0.0.1",' + '\n' + + ' "port": "6379",' + '\n' + + ' "password": "",' + '\n' + + ' "database": "1"' + '\n' + + '}\n'+ + " or (mongo):\n" + + '"test_database": {' + '\n' + + ' "host": "127.0.0.1",' + '\n' + + ' "port": "27017",' + '\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 = 'test_database has the same config as production db'; + winston.error(errorText); + throw new Error(errorText); + } + + nconf.set(dbType, testDbConfig); + + db = require('../src/database'); + before(function(done) { + + db.init(function(err) { + //Clean up + db.flushdb(function(err) { + if(err){ + winston.error(err); + throw new Error(err); + } else { + winston.info('test_database flushed'); + done(); + } + + //TODO: data seeding, if needed at all + + + }); + }); + }); + + module.exports = db; + +}(module)); diff --git a/mocks/redismock.js b/mocks/redismock.js deleted file mode 100644 index cfdbb4d9fe..0000000000 --- a/mocks/redismock.js +++ /dev/null @@ -1,69 +0,0 @@ -/** - * 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.json\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)); diff --git a/src/database/mongo.js b/src/database/mongo.js index 45df048646..0d5387ff93 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -118,9 +118,14 @@ db.dropDatabase(function(err, result) { if(err){ winston.error(error); - return callback(err); + if(callback) { + return callback(err); + } + } + + if(callback) { + callback(null); } - callback(null); }); } @@ -379,7 +384,10 @@ // sets module.setAdd = function(key, value, callback) { - db.collection('objects').update({_key:key}, {$addToSet: { members: value.toString() }}, {upsert:true, w: 1}, function(err, result) { + if(value !== null && value !== undefined) { + value = value.toString(); + } + db.collection('objects').update({_key:key}, {$addToSet: { members: value }}, {upsert:true, w: 1}, function(err, result) { if(callback) { callback(err, result); } @@ -387,7 +395,10 @@ } module.setRemove = function(key, value, callback) { - db.collection('objects').update({_key:key, members: value.toString()}, {$pull : {members: value}}, function(err, result) { + if(value !== null && value !== undefined) { + value = value.toString(); + } + db.collection('objects').update({_key:key, members: value}, {$pull : {members: value}}, function(err, result) { if(callback) { callback(err, result); } @@ -395,7 +406,10 @@ } module.isSetMember = function(key, value, callback) { - db.collection('objects').findOne({_key:key, members: value.toString()}, function(err, item) { + if(value !== null && value !== undefined) { + value = value.toString(); + } + db.collection('objects').findOne({_key:key, members: value}, function(err, item) { callback(err, item !== null && item !== undefined); }); } @@ -650,13 +664,20 @@ db.collection('objects').update({_key: key }, { $pop: { array: 1 } }, function(err, result) { if(err) { - return callback(err); + if(callback) { + return callback(err); + } + return; } if(value && value.length) { - callback(err, value[0]); + if(callback) { + callback(err, value[0]); + } } else { - callback(err, null); + if(callback) { + callback(err, null); + } } }); }); diff --git a/src/websockets.js b/src/websockets.js index 992a8fcba0..8a76ca3402 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -1152,8 +1152,10 @@ websockets.init = function(io) { return io.sockets.in(room); }; +} + websockets.getConnectedClients = function() { return userSockets; } -} -})(module.exports); + +})(module.exports); diff --git a/tests/categories.js b/tests/categories.js index 36a4d63925..070a71e884 100644 --- a/tests/categories.js +++ b/tests/categories.js @@ -7,14 +7,7 @@ process.on('uncaughtException', function (err) { }); var assert = require('assert'), - 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 -var reds = require('reds'); -reds.createClient = function () { - return reds.client || (reds.client = RDB); -}; + db = require('../mocks/databasemock'); var Categories = require('../src/categories'); @@ -23,6 +16,7 @@ describe('Categories', function() { describe('.create', function() { it('should create a new category', function(done) { + Categories.create({ name: 'Test Category', description: 'Test category created by testing script', @@ -62,11 +56,7 @@ describe('Categories', function() { }); after(function() { - // TODO : replace with dbal - RDB.multi() - .del('category:'+categoryObj.cid) - .rpop('categories:cid') - .exec(); - + db.delete('category:' + categoryObj.cid); + db.listRemoveLast('categories:cid'); }); }); \ No newline at end of file diff --git a/tests/redis.js b/tests/database.js similarity index 76% rename from tests/redis.js rename to tests/database.js index 0af4f1a120..c857233c53 100644 --- a/tests/redis.js +++ b/tests/database.js @@ -4,7 +4,7 @@ var assert = require('assert'); describe('Test database', function() { it('should work', function(){ assert.doesNotThrow(function(){ - var RDB = require('../mocks/redismock'); + var db = require('../mocks/databasemock'); }); }); }); diff --git a/tests/topics.js b/tests/topics.js index 063451cc3e..9d27db8cf7 100644 --- a/tests/topics.js +++ b/tests/topics.js @@ -5,14 +5,8 @@ process.on('uncaughtException', function (err) { }); var assert = require('assert'), - RDB = require('../mocks/redismock'); + db = require('../mocks/databasemock'); -// 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 -var reds = require('reds'); -reds.createClient = function () { - return reds.client || (reds.client = RDB); -}; var Topics = require('../src/topics'); @@ -43,7 +37,7 @@ describe('Topic\'s', function() { topic.userId = null; Topics.post(topic.userId, topic.title, topic.content, topic.categoryId, function(err, result) { - assert.equal(err.message, 'not-logged-in'); + assert.equal(err.message, 'invalid-user'); done(); }); }); @@ -75,11 +69,6 @@ describe('Topic\'s', function() { }); after(function() { - RDB.send_command('flushdb', [], function(error){ - if(error){ - winston.error(error); - throw new Error(error); - } - }); + db.flushdb(); }); }); \ No newline at end of file diff --git a/tests/user.js b/tests/user.js index cb89ac1fc9..7adfca61ee 100644 --- a/tests/user.js +++ b/tests/user.js @@ -7,7 +7,7 @@ process.on('uncaughtException', function (err) { }); var assert = require('assert'), - RDB = require('../mocks/redismock'); + db = require('../mocks/databasemock'); var User = require('../src/user'); @@ -43,12 +43,6 @@ describe('User', function() { }); after(function() { - //Clean up - RDB.send_command('flushdb', [], function(error){ - if(error){ - winston.error(error); - throw new Error(error); - } - }); + db.flushdb(); }); }); \ No newline at end of file