Node.js based forum software built for the modern web
removed redismock, added database mocked, fixed tests to work with dbal
Baris Usakli0da141e7bcb2
diff --git a/mocks/databasemock.js b/mocks/databasemock.jsnew file mode 100644index 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.jsdeleted file mode 100644index 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.jsindex 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 @@// setsmodule.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.jsindex 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.jsindex 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 filediff --git a/tests/redis.js b/tests/database.jssimilarity index 76%rename from tests/redis.jsrename to tests/database.jsindex 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.jsindex 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 filediff --git a/tests/user.js b/tests/user.jsindex 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