From bf54859a275c4a7af74f6f8e1e84bf79163de16b Mon Sep 17 00:00:00 2001 From: barisusakli Date: Thu, 25 Dec 2014 01:53:28 -0500 Subject: [PATCH] key tests --- src/database/redis/main.js | 4 +- tests/database.js | 46 -------------- tests/database/keys.js | 121 +++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 47 deletions(-) create mode 100644 tests/database/keys.js diff --git a/src/database/redis/main.js b/src/database/redis/main.js index ca9f698362..0ed5d699ca 100644 --- a/src/database/redis/main.js +++ b/src/database/redis/main.js @@ -89,7 +89,9 @@ module.exports = function(redisClient, module) { }; module.set = function(key, value, callback) { - redisClient.set(key, value, callback); + redisClient.set(key, value, function(err) { + callback(err); + }); }; module.increment = function(key, callback) { diff --git a/tests/database.js b/tests/database.js index ecad48aedd..dc83bbcca9 100644 --- a/tests/database.js +++ b/tests/database.js @@ -11,52 +11,6 @@ describe('Test database', function() { }); - it('should not throw err', function(done) { - - function get(callback) { - db.get('testingStr', function(err, data) { - callback(err, {'get': data}); - }); - } - - function set(callback) { - db.set('testingStr', 'oppa gangnam style', function(err, data) { - callback(err, {'set': data}); - }); - } - - function deleteKey(callback) { - db.delete('testingStr', function(err, data) { - callback(err, {'delete': data}); - }); - } - - function exists(callback) { - db.exists('testingStr', function(err, data) { - callback(err, {'exists': data}); - }); - } - - var keyTasks = [ - get, - set, - get, - exists, - deleteKey, - deleteKey, - get, - exists - ]; - - async.series(keyTasks, function(err, results) { - assert.equal(err, null, 'error in key methods'); - assert.ok(results); - - done(); - }); - - }); - it('should not throw err', function(done) { var objectKey = 'testObj'; diff --git a/tests/database/keys.js b/tests/database/keys.js new file mode 100644 index 0000000000..d723e77777 --- /dev/null +++ b/tests/database/keys.js @@ -0,0 +1,121 @@ + +var async = require('async'), + assert = require('assert'), + db = require('./mocks/databasemock'); + +describe('Key methods', function() { + + + it('should set a key without error', function(done) { + db.set('testKey', 'testValue', function(err) { + assert(err, null, 'db.set error'); + assert(arguments.length, 1, 'db.set too many parameters returned'); + done(); + }); + }); + + it('should get a key without error', function(done) { + db.get('testKey', function(err, value) { + assert.equal(err, null, 'db.get error'); + assert.equal(arguments.length, 2, 'db.get arguments.length error'); + assert.strictEqual(value, 'testValue', 'db.get returned value type is different'); + done(); + }); + }); + + it('should return true if key exist', function(done) { + db.exists('testKey', function(err, exists) { + assert.equal(err, null, 'db.exists error'); + assert.equal(arguments.length, 2, 'db.exists arguments.length error'); + assert.strictEqual(exists, true, 'db.exists did not return true for existing key'); + done(); + }); + }); + + it('should delete a key without error', function(done) { + db.delete('testKey', function(err) { + assert.equal(err, null, 'db.delete error'); + assert.equal(arguments.length, 1, 'db.delete arguments.length error'); + + db.get('testKey', function(err, value) { + assert.equal(err, null, 'db.get error'); + assert.equal(value, false, 'db.get deleted key is not falsy'); + done(); + }); + }); + }); + + it('should return false if key does not exist or was deleted', function(done) { + db.exists('testKey', function(err, exists) { + assert.equal(err, null, 'db.exists error'); + assert.strictEqual(exists, false, 'db.exists did not return false for non-existing key'); + done(); + }); + }); + + it('should delete all keys passed in', function(done) { + async.parallel([ + function(next) { + db.set('key1', 'value1', next); + }, + function(next) { + db.set('key2', 'value2', next); + } + ], function(err) { + if (err) { + return done(err); + } + db.deleteAll(['key1', 'key2'], function(err) { + assert.equal(err, null, 'db.deleteAll error'); + assert.equal(arguments.length, 1, 'arguments.length error'); + db.exists('key1', function(err, exists) { + assert.equal(exists, false, 'deleted key is not falsy'); + done(); + }); + }); + }); + }); + + describe('increment', function() { + it('should initialize key to 1', function(done) { + db.increment('keyToIncrement', function(err, value) { + assert.equal(err, null, 'db.increment error'); + assert.strictEqual(parseInt(value, 10), 1, 'value not incremented'); + done(); + }); + }); + + it('should increment key to 2', function(done) { + db.increment('keyToIncrement', function(err, value) { + assert.equal(err, null, 'db.increment error'); + assert.strictEqual(parseInt(value, 10), 2, 'value not incremented'); + done(); + }); + }); + }); + + describe('rename', function() { + it('should rename key to new name', function(done) { + db.set('keyOldName', 'renamedKeyValue', function(err) { + if (err) { + return done(err); + } + db.rename('keyOldName', 'keyNewName', function(err) { + assert.equal(err, null, 'db.rename error'); + assert.equal(arguments.length, 1, 'db.rename arguments.length error'); + + db.get('keyNewName', function(err, value) { + assert.equal(err, null, 'db.rename error'); + assert.equal(value, 'renamedKeyValue', 'renamed key value does not match'); + done(); + }); + }); + }); + }); + }); + + + after(function() { + db.flushdb(); + }); +});