added testing for db interface, still a lot to do...

This commit is contained in:
Dominic Bosch 2014-02-04 22:30:26 +01:00
parent 42e80e32e7
commit 5a13beadd0
7 changed files with 386 additions and 68 deletions

View file

@ -238,6 +238,18 @@ exports.getActionModule = ( amId, cb ) =>
log.print 'DB', "getActionModule: #{ amId }"
@db.get "action-module:#{ amId }", cb
exports.getSetMembers = ( setId, cb ) =>
@db.smembers setId, cb
###
Fetch all action module IDs and hand them to the callback(err, obj) function.
@public getActionModuleIds( *cb* )
@param {function} cb
###
exports.getActionModuleIds = ( cb ) =>
@db.smembers 'action-modules', cb
###
Fetch all action modules and hand them to the callback(err, obj) function.
@ -247,6 +259,16 @@ Fetch all action modules and hand them to the callback(err, obj) function.
exports.getActionModules = ( cb ) ->
getSetRecords 'action-modules', exports.getActionModule, cb
###
Fetch all action modules and hand them to the callback(err, obj) function.
@public getActionModules( *cb* )
@param {function} cb
###
exports.deleteActionModule = ( amId ) =>
@db.srem 'action-modules', amId, replyHandler "deleting action module key #{ amId }"
@db.del "action-module:#{ amId }", replyHandler "deleting action module #{ amId }"
###
Store user-specific action module parameters .

View file

@ -321,6 +321,22 @@ DB Interface
return _this.db.get("action-module:" + amId, cb);
};
exports.getSetMembers = function(setId, cb) {
return _this.db.smembers(setId, cb);
};
/*
Fetch all action module IDs and hand them to the callback(err, obj) function.
@public getActionModuleIds( *cb* )
@param {function} cb
*/
exports.getActionModuleIds = function(cb) {
return _this.db.smembers('action-modules', cb);
};
/*
Fetch all action modules and hand them to the callback(err, obj) function.
@ -333,6 +349,19 @@ DB Interface
return getSetRecords('action-modules', exports.getActionModule, cb);
};
/*
Fetch all action modules and hand them to the callback(err, obj) function.
@public getActionModules( *cb* )
@param {function} cb
*/
exports.deleteActionModule = function(amId) {
_this.db.srem('action-modules', amId, replyHandler("deleting action module key " + amId));
return _this.db.del("action-module:" + amId, replyHandler("deleting action module " + amId));
};
/*
Store user-specific action module parameters .

View file

@ -9,6 +9,7 @@
* - 1 file
* - 2 silent
*/
//TODO dynamic log file names (especially to track unit test logs)
var fs = require('fs'),
logTypes = [ flushToConsole, flushToFile, null],
logFile = require('path').resolve(__dirname, '..', 'server.log'),

View file

@ -37,18 +37,6 @@
"redis": "0.9.0",
"request": "2.27.0",
"coffee-script": "1.6.3"
},
"dependencies_new": {
"connect-redis": "1.4.6",
"crypto-js": "3.1.2",
"express": "3.4.8",
"groc": "0.6.1",
"mustache": "0.8.1",
"needle": "0.6.3",
"nodeunit": "0.8.4",
"redis": "0.10.0",
"request": "2.33.0",
"coffee-script": "1.6.3"
}
}
}

49
testing/js/test_config.js Normal file
View file

@ -0,0 +1,49 @@
// Generated by CoffeeScript 1.6.3
(function() {
var path,
_this = this;
path = require('path');
exports.setUp = function(cb) {
_this.conf = require(path.join('..', 'js-coffee', 'config'));
_this.conf({
logType: 2
});
return cb();
};
exports.testRequire = function(test) {
test.expect(1);
test.ok(_this.conf.isReady(), 'File does not exist!');
return test.done();
};
exports.testParameters = function(test) {
test.expect(4);
test.ok(_this.conf.getHttpPort(), 'HTTP port does not exist!');
test.ok(_this.conf.getDBPort(), 'DB port does not exist!');
test.ok(_this.conf.getCryptoKey(), 'Crypto key does not exist!');
test.ok(_this.conf.getSessionSecret(), 'Session Secret does not exist!');
return test.done();
};
exports.testDifferentConfigFile = function(test) {
test.expect(1);
_this.conf({
configPath: 'testing/jsonWrongConfig.json'
});
test.ok(_this.conf.isReady(), 'Different path not loaded!');
return test.done();
};
exports.testNoConfigFile = function(test) {
test.expect(1);
_this.conf({
configPath: 'wrongpath.file'
});
test.strictEqual(_this.conf.isReady(), false, 'Wrong path still loaded!');
return test.done();
};
}).call(this);

View file

@ -0,0 +1,201 @@
// Generated by CoffeeScript 1.6.3
(function() {
var _this = this,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
exports.setUp = function(cb) {
_this.log = require('../js-coffee/logging');
_this.db = require('../js-coffee/db_interface');
_this.db({
logType: 2
});
return cb();
};
exports.availability = {
testRequire: function(test) {
test.expect(1);
test.ok(_this.db, 'DB interface loaded');
return test.done();
},
testConnect: function(test) {
test.expect(1);
return _this.db.isConnected(function(err) {
test.ifError(err, 'Connection failed!');
return test.done();
});
},
testNoConfig: function(test) {
test.expect(1);
_this.db({
configPath: 'nonexistingconf.file'
});
return _this.db.isConnected(function(err) {
test.ok(err, 'Still connected!?');
return test.done();
});
},
testWrongConfig: function(test) {
test.expect(1);
_this.db({
configPath: 'testing/jsonWrongConfig.json'
});
return _this.db.isConnected(function(err) {
test.ok(err, 'Still connected!?');
return test.done();
});
},
testPurgeQueue: function(test) {
var evt;
evt = {
eventid: '1',
event: 'mail'
};
test.expect(2);
_this.db.pushEvent(evt);
_this.db.purgeEventQueue();
return _this.db.popEvent(function(err, obj) {
test.ifError(err, 'Error during pop after purging!');
test.strictEqual(obj, null, 'There was an event in the queue!?');
return test.done();
});
}
};
exports.events = {
setUp: function(cb) {
_this.evt1 = {
eventid: '1',
event: 'mail'
};
_this.evt2 = {
eventid: '2',
event: 'mail'
};
_this.db.purgeEventQueue();
return cb();
},
testEmptyPopping: function(test) {
test.expect(2);
return _this.db.popEvent(function(err, obj) {
test.ifError(err, 'Error during pop after purging!');
test.strictEqual(obj, null, 'There was an event in the queue!?');
return test.done();
});
},
testEmptyPushing: function(test) {
test.expect(2);
_this.db.pushEvent(null);
return _this.db.popEvent(function(err, obj) {
test.ifError(err, 'Error during non-empty pushing!');
test.strictEqual(obj, null, 'There was an event in the queue!?');
return test.done();
});
},
testNonEmptyPopping: function(test) {
test.expect(3);
_this.db.pushEvent(_this.evt1);
return _this.db.popEvent(function(err, obj) {
test.ifError(err, 'Error during non-empty popping!');
test.notStrictEqual(obj, null, 'There was no event in the queue!');
test.deepEqual(_this.evt1, obj, 'Wrong event in queue!');
return test.done();
});
},
testMultiplePushAndPops: function(test) {
var forkEnds, semaphore;
test.expect(6);
semaphore = 2;
forkEnds = function() {
if (--semaphore === 0) {
return test.done();
}
};
_this.db.pushEvent(_this.evt1);
_this.db.pushEvent(_this.evt2);
_this.db.popEvent(function(err, obj) {
test.ifError(err, 'Error during multiple push and pop!');
test.notStrictEqual(obj, null, 'There was no event in the queue!');
test.deepEqual(_this.evt1, obj, 'Wrong event in queue!');
return forkEnds();
});
return _this.db.popEvent(function(err, obj) {
test.ifError(err, 'Error during multiple push and pop!');
test.notStrictEqual(obj, null, 'There was no event in the queue!');
test.deepEqual(_this.evt2, obj, 'Wrong event in queue!');
return forkEnds();
});
}
};
exports.action_modules = {
testModule: function(test) {
var action1, action1name, fCheckModuleExists, fCheckModuleNotExists, fCheckModuleNotExistsInSet, fCheckSetEntry;
test.expect(4);
action1name = 'test-action-module_1';
action1 = 'unit-test action module 1 content';
fCheckSetEntry = function(err, obj) {
test.ok(__indexOf.call(obj, action1name) >= 0, 'Expected key not in action-modules set');
return _this.db.getActionModule(action1name, fCheckModuleExists);
};
fCheckModuleExists = function(err, obj) {
test.strictEqual(obj, action1, 'Retrieved Action Module is not what we expected');
_this.log.print('delete action module');
_this.db.deleteActionModule(action1name);
_this.log.print('tried to delete action module');
return _this.db.getActionModule(action1name, fCheckModuleNotExists);
};
fCheckModuleNotExists = function(err, obj) {
_this.log.print('got action module');
test.strictEqual(obj, null, 'Action module still exists');
_this.log.print('compared action module');
return _this.db.getActionModuleIds(fCheckModuleNotExistsInSet);
};
fCheckModuleNotExistsInSet = function(err, obj) {
test.ok(__indexOf.call(obj, action1name) < 0, 'Action module key still exists in set');
return test.done();
};
_this.db.storeActionModule(action1name, action1);
return _this.db.getActionModuleIds(fCheckSetEntry);
},
testFetchSeveralModules: function(test) {
var action1, action1name, action2, action2name, fCheckModule, fCheckSetEntries, forkEnds, semaphore;
semaphore = 2;
forkEnds = function() {
if (--semaphore === 0) {
return test.done();
}
};
test.expect(3);
action1name = 'test-action-module_1';
action2name = 'test-action-module_2';
action1 = 'unit-test action module 1 content';
action2 = 'unit-test action module 2 content';
fCheckModule = function(mod) {
var myTest;
myTest = test;
console.log('check module');
return function(err, obj) {
console.log('db answered');
myTest.strictEqual(mod, obj, "Module does not equal the expected one");
return forkEnds();
};
};
fCheckSetEntries = function(err, obj) {
test.ok(__indexOf.call(obj, action1name) >= 0 && __indexOf.call(obj, action2name) >= 0, 'Not all action module Ids in set');
console.log('setentries fetched');
this.db.getActionModule(action1name, fCheckModule(action1));
return this.db.getActionModule(action2name, fCheckModule(action2));
};
_this.db.storeActionModule(action1name, action1);
_this.db.storeActionModule(action2name, action2);
return _this.db.getActionModuleIds(fCheckSetEntries);
}
};
exports.tearDown = function(cb) {
_this.db.shutDown();
return cb();
};
}).call(this);

View file

@ -1,5 +1,6 @@
exports.setUp = ( cb ) =>
@log = require '../js-coffee/logging'
@db = require '../js-coffee/db_interface'
@db logType: 2
cb()
@ -69,14 +70,6 @@ exports.events =
test.strictEqual obj, null, 'There was an event in the queue!?'
test.done()
testPushing: ( test ) =>
test.expect 1
fPush = ->
@db.pushEvent null
@db.pushEvent @evt1
test.throws fPush, Error, 'This should not throw an error'
test.done()
testNonEmptyPopping: ( test ) =>
test.expect 3
@db.pushEvent @evt1
@ -107,63 +100,98 @@ exports.events =
forkEnds()
exports.action_modules =
setUp: ( cb ) =>
@action1 = '
exports.testFunctionOne = function( args ) {
var data = {
companyId: \'961\',
context: \'17936\',
text: \'Binder entry based on event: \' + args.info
};
needle.post(\'https://probinder.com/service/27/save\', data);
};'
@action2 = '
// This is just a console.log which should fail
console.log(\'Why is this being printed??\');
exports.testFunctionTwo = function( args ) {
// empty function)
};'
cb()
# setUp: ( cb ) =>
# @db logType: 1
# cb()
testStoreModule: ( test ) =>
test.expect 1
fStore = ->
@db.storeActionModule 'test-action-module_null', null
@db.storeActionModule 'test-action-module_1', @action1
test.throws fStore, Error, 'Storing Action Module should not throw an error'
test.done()
testModule: ( test ) =>
test.expect 4
action1name = 'test-action-module_1'
action1 = 'unit-test action module 1 content'
testFetchModule: ( test ) =>
test.expect 0
test.done()
fCheckSetEntry = ( err , obj ) =>
test.ok action1name in obj, 'Expected key not in action-modules set'
@db.getActionModule action1name, fCheckModuleExists
testFetchModules: ( test ) =>
test.expect 0
test.done()
fCheckModuleExists = ( err , obj ) =>
test.strictEqual obj, action1, 'Retrieved Action Module is not what we expected'
@log.print 'delete action module'
@db.deleteActionModule action1name
@log.print 'tried to delete action module'
@db.getActionModule action1name, fCheckModuleNotExists
testStoreParams: ( test ) =>
test.expect 0
test.done()
fCheckModuleNotExists = ( err , obj ) =>
@log.print 'got action module'
test.strictEqual obj, null, 'Action module still exists'
@log.print 'compared action module'
@db.getActionModuleIds fCheckModuleNotExistsInSet
testFetchParams: ( test ) =>
test.expect 0
test.done()
fCheckModuleNotExistsInSet = ( err , obj ) =>
test.ok action1name not in obj, 'Action module key still exists in set'
test.done()
exports.event_modules =
test: ( test ) =>
test.expect 0
test.done()
@db.storeActionModule action1name, action1
@db.getActionModuleIds fCheckSetEntry
testFetchSeveralModules: ( test ) =>
semaphore = 2
test.expect 3
action1name = 'test-action-module_1'
action2name = 'test-action-module_2'
action1 = 'unit-test action module 1 content'
action2 = 'unit-test action module 2 content'
fCheckModule = ( mod ) ->
myTest = test
sem = semaphore
forkEnds = () ->
console.log 'fork ends'
myTest.done() if --sem is 0
console.log 'check module'
( err, obj ) ->
console.log 'db answered'
myTest.strictEqual mod, obj, "Module does not equal the expected one"
forkEnds()
fCheckSetEntries = ( err, obj ) ->
test.ok action1name in obj and action2name in obj, 'Not all action module Ids in set'
console.log 'setentries fetched'
@db.getActionModule action1name, fCheckModule(action1)
@db.getActionModule action2name, fCheckModule(action2)
@db.storeActionModule action1name, action1
@db.storeActionModule action2name, action2
@db.getActionModuleIds fCheckSetEntries
exports.rules =
test: ( test ) =>
test.expect 0
test.done()
# testFetchModules: ( test ) =>
# test.expect 0
# test.done()
exports.users =
test: ( test ) =>
test.expect 0
test.done()
# testStoreParams: ( test ) =>
# test.expect 0
# test.done()
# testFetchParams: ( test ) =>
# test.expect 0
# test.done()
# exports.event_modules =
# test: ( test ) =>
# test.expect 0
# test.done()
# exports.rules =
# test: ( test ) =>
# test.expect 0
# test.done()
# exports.users =
# test: ( test ) =>
# test.expect 0
# test.done()
exports.tearDown = ( cb ) =>