From 1b73875e34039898f15705c44f7cf9e73b50e2b6 Mon Sep 17 00:00:00 2001 From: Arnav Kumar Date: Mon, 27 Jul 2015 17:37:46 +0800 Subject: [PATCH] Add SISMEMBER command support for Sets. - Also, add tests --- lib/redis-mock.js | 5 ++++ lib/set.js | 22 ++++++++++++++++- test/redis-mock.set.test.js | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/lib/redis-mock.js b/lib/redis-mock.js index 4c02bd5..5faee71 100755 --- a/lib/redis-mock.js +++ b/lib/redis-mock.js @@ -385,6 +385,11 @@ RedisClient.prototype.smembers = RedisClient.prototype.SMEMBERS = function (key, RedisClient.prototype.scard = RedisClient.prototype.SCARD = function (key, callback) { setfunctions.scard.call(this, MockInstance, key, callback); } + +RedisClient.prototype.sismember = RedisClient.prototype.SISMEMBER = function (key, value, callback) { + setfunctions.sismember.call(this, MockInstance, key, value, callback); +} + /** * Other commands (Lua scripts) */ diff --git a/lib/set.js b/lib/set.js index 4147b9a..df30ae6 100644 --- a/lib/set.js +++ b/lib/set.js @@ -86,4 +86,24 @@ exports.scard = function (mockInstance, key, callback) { } mockInstance._callCallback(callback, null, count); -} \ No newline at end of file +} + +/** + * Sismember + */ +exports.sismember = function (mockInstance, key, value, callback) { + var isMember = 0; + if (mockInstance.storage[key]) { + if (mockInstance.storage[key].type !== 'set') { + var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value'); + return mockInstance._callCallback(callback, err); + } else { + var set = mockInstance.storage[key].value; + if (set.indexOf(value) >= 0) { + isMember = 1; + } + } + } + + mockInstance._callCallback(callback, null, isMember); +} diff --git a/test/redis-mock.set.test.js b/test/redis-mock.set.test.js index 80f5cdd..6442e51 100644 --- a/test/redis-mock.set.test.js +++ b/test/redis-mock.set.test.js @@ -228,3 +228,51 @@ describe('scard', function () { }); }); + +describe('sismember', function () { + + it('should return 1 if the value exists in the set', function (done) { + var r = redismock.createClient(); + r.sadd('foo', 'bar', function (err, result) { + r.sismember('foo', 'bar', function (err, result) { + result.should.eql(1); + r.end(); + done(); + }); + }); + }); + + it('should return 0 if the value does not exist in the set', function (done) { + var r = redismock.createClient(); + r.sadd('foo', 'bar', function (err, result) { + r.sismember('foo', 'boo', function (err, result) { + result.should.eql(0); + r.end(); + done(); + }); + }); + }); + + it('should return 0 if key does not exist', function (done) { + var r = redismock.createClient(); + r.sadd('foo', 'bar', 'baz', function (err, result) { + r.sismember('qux', 'bar', function (err, result) { + result.should.eql(0); + r.end(); + done(); + }); + }); + }); + + it('should return error when the value stored at the key is not a set', function (done) { + var r = redismock.createClient(); + r.hset('foo', 'bar', 'baz', function (err, result) { + r.sismember('foo', 'bar', function (err, result) { + err.message.should.eql('WRONGTYPE Operation against a key holding the wrong kind of value'); + r.end(); + done(); + }); + }); + }); + +});