Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});

});