From 4d5ed784a100d65c7c7c7caef7d2e76a141d7cb9 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 30 Dec 2014 18:41:13 -0500 Subject: [PATCH] more hash tests getObjectValues isObjectField deleteObjectField incrObjectField --- src/database/mongo/hash.js | 4 +- src/database/redis/hash.js | 4 +- tests/database/hash.js | 87 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js index c35df47952..6db3b5a4a9 100644 --- a/src/database/mongo/hash.js +++ b/src/database/mongo/hash.js @@ -182,7 +182,9 @@ module.exports = function(db, module) { var data = {}; field = helpers.fieldToString(field); data[field] = ''; - db.collection('objects').update({_key: key}, {$unset : data}, callback); + db.collection('objects').update({_key: key}, {$unset : data}, function(err, res) { + callback(err); + }); }; module.incrObjectField = function(key, field, callback) { diff --git a/src/database/redis/hash.js b/src/database/redis/hash.js index 02df03c92e..ddc21b893c 100644 --- a/src/database/redis/hash.js +++ b/src/database/redis/hash.js @@ -89,7 +89,9 @@ module.exports = function(redisClient, module) { }; module.deleteObjectField = function(key, field, callback) { - redisClient.hdel(key, field, callback); + redisClient.hdel(key, field, function(err, res) { + callback(err); + }); }; module.incrObjectField = function(key, field, callback) { diff --git a/tests/database/hash.js b/tests/database/hash.js index 18dbc1fbfb..6c0d356725 100644 --- a/tests/database/hash.js +++ b/tests/database/hash.js @@ -167,6 +167,93 @@ describe('Hash methods', function() { }); }); + describe('getObjectValues()', function() { + it('should return an empty array for a object that does not exist', function(done) { + db.getObjectKeys('doesnotexist', function(err, values) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.equal(Array.isArray(values) && values.length === 0, true); + done(); + }); + }); + + it('should return an array of values for the object\'s fields', function(done) { + db.getObjectKeys('testObject1', function(err, values) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.equal(Array.isArray(values) && values.length === 3, true); + values.forEach(function(value) { + assert.notEqual(['baris', 'usakli', 99].indexOf(value), -1); + }); + + done(); + }); + }); + }); + + describe('isObjectField()', function() { + it('should return false if object does not exist', function(done) { + db.isObjectField('doesnotexist', 'field1', function(err, value) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.equal(value, false); + done(); + }); + }); + + it('should return false if field does not exist', function(done) { + db.getObjectKeys('testObject1', 'field1', function(err, value) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.equal(value, false); + done(); + }); + }); + + it('should return true if field exists', function(done) { + db.getObjectKeys('testObject1', function(err, value) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.equal(value, true); + done(); + }); + }); + }); + + describe('deleteObjectField()', function() { + it('should delete an objects field', function(done) { + db.deleteObjectField('testObject1', 'lastname', function(err) { + assert.equal(err, null); + assert.equal(arguments.length, 1); + db.isObjectField('testObject1', 'lastname', function(err, isField) { + assert.equal(err, null); + assert.equal(isField, false); + done(); + }); + }); + }); + }); + + describe('incrObjectField()', function() { + it('should set an objects field to 1 if object does not exist', function(done) { + db.incrObjectField('testObject3', 'field1', function(err, newValue) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.equal(newValue, 1); + done(); + }); + }); + + it('should increment an object fields by 1 and return it', function(done) { + db.incrObjectField('testObject1', 'age', function(err, newValue) { + assert.equal(err, null); + assert.equal(arguments.length, 2); + assert.equal(newValue, 100); + done(); + }); + }); + }); + after(function() { db.flushdb();