Skip to content

Commit

Permalink
Add SISMEMBER command support for Sets.
Browse files Browse the repository at this point in the history
- Also, add tests
  • Loading branch information
arnavk committed Jul 27, 2015
1 parent a74c51b commit 1b73875
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/redis-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down
22 changes: 21 additions & 1 deletion lib/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,24 @@ exports.scard = function (mockInstance, key, callback) {
}

mockInstance._callCallback(callback, null, count);
}
}

/**
* 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);
}
48 changes: 48 additions & 0 deletions test/redis-mock.set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

});

0 comments on commit 1b73875

Please sign in to comment.