removed redismock, added database mocked, fixed tests to work with dbal

v1.18.x
Baris Usakli 11 years ago
parent 6313a5eeb1
commit 0da141e7bc

@ -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));

@ -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));

@ -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);
}
}
});
});

@ -1152,8 +1152,10 @@ websockets.init = function(io) {
return io.sockets.in(room);
};
}
websockets.getConnectedClients = function() {
return userSockets;
}
}
})(module.exports);
})(module.exports);

@ -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');
});
});

@ -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');
});
});
});

@ -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();
});
});

@ -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();
});
});
Loading…
Cancel
Save