Skip to content

Commit 32ccb56

Browse files
committed
Add SISMEMBER command support for Sets.
- Also, add tests
1 parent a74c51b commit 32ccb56

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

lib/set.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,24 @@ exports.scard = function (mockInstance, key, callback) {
8686
}
8787

8888
mockInstance._callCallback(callback, null, count);
89-
}
89+
}
90+
91+
/**
92+
* Sismember
93+
*/
94+
exports.sismember = function (mockInstance, key, value, callback) {
95+
var isMember = 0;
96+
if (mockInstance.storage[key]) {
97+
if (mockInstance.storage[key].type !== 'set') {
98+
var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
99+
return mockInstance._callCallback(callback, err);
100+
} else {
101+
var set = mockInstance.storage[key].value;
102+
if (set.indexOf(value) >= 0) {
103+
isMember = 1;
104+
}
105+
}
106+
}
107+
108+
mockInstance._callCallback(callback, null, isMember);
109+
}

test/redis-mock.set.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,51 @@ describe('scard', function () {
228228
});
229229

230230
});
231+
232+
describe('sismember', function () {
233+
234+
it('should return 1 if the value exists in the set', function (done) {
235+
var r = redismock.createClient();
236+
r.sadd('foo', 'bar', function (err, result) {
237+
r.sismember('foo', 'bar', function (err, result) {
238+
result.should.eql(1);
239+
r.end();
240+
done();
241+
});
242+
});
243+
});
244+
245+
it('should return 0 if the value does not exist in the set', function (done) {
246+
var r = redismock.createClient();
247+
r.sadd('foo', 'bar', function (err, result) {
248+
r.sismember('foo', 'boo', function (err, result) {
249+
result.should.eql(0);
250+
r.end();
251+
done();
252+
});
253+
});
254+
});
255+
256+
it('should return 0 if key does not exist', function (done) {
257+
var r = redismock.createClient();
258+
r.sadd('foo', 'bar', 'baz', function (err, result) {
259+
r.sismember('qux', 'bar', function (err, result) {
260+
result.should.eql(0);
261+
r.end();
262+
done();
263+
});
264+
});
265+
});
266+
267+
it('should return error when the value stored at the key is not a set', function (done) {
268+
var r = redismock.createClient();
269+
r.hset('foo', 'bar', 'baz', function (err, result) {
270+
r.sismember('foo', 'bar', function (err, result) {
271+
err.message.should.eql('WRONGTYPE Operation against a key holding the wrong kind of value');
272+
r.end();
273+
done();
274+
});
275+
});
276+
});
277+
278+
});

0 commit comments

Comments
 (0)