Skip to content

Commit

Permalink
Add SMISMEMBER command (#20)
Browse files Browse the repository at this point in the history
Add corresponding tests
Test with fake and real redis.
Add markers to pytest.ini to get rid of warning
Add conditional run only for redis 4 and above

Co-authored-by: Daniel M <[email protected]>
  • Loading branch information
Oleg and cunla authored May 25, 2022
1 parent 47ee568 commit 1947736
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion REDIS_COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ list of [unimplemented commands](#unimplemented-commands).
* sinter
* sinterstore
* sismember
* smismember
* smembers
* smove
* spop
Expand Down Expand Up @@ -399,7 +400,6 @@ All of the redis commands are implemented in fakeredis with these exceptions:

### set
* sintercard
* smismember

### stream
* xack
Expand Down
4 changes: 4 additions & 0 deletions fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,10 @@ def sinterstore(self, dst, *keys):
def sismember(self, key, member):
return int(member in key.value)

@command((Key(set), bytes), (bytes,))
def smismember(self, key, *members):
return [self.sismember(key, member) for member in members]

@command((Key(set),))
def smembers(self, key):
return list(key.value)
Expand Down
8 changes: 8 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[pytest]
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
fake: run tests only with fake redis
real: run tests with a locally running real Redis server
redis4_and_above: run tests for redis-py 4.1.2 and above
redis3_and_above: run tests for redis-py 3.x and above
redis_below_v3: run tests for redis-py 2.x and below
22 changes: 22 additions & 0 deletions test/test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,28 @@ def test_sismember_wrong_type(r):
with pytest.raises(redis.ResponseError):
r.sismember('foo', 'member')

@redis4_and_above
def test_smismember(r):
assert r.smismember('foo', ['member1', 'member2', 'member3']) == [0, 0, 0]
r.sadd('foo', 'member1', 'member2', 'member3')
assert r.smismember('foo', ['member1', 'member2', 'member3']) == [1, 1, 1]
assert r.smismember('foo', ['member1', 'member2', 'member3', 'member4']) == [1, 1, 1, 0]
assert r.smismember('foo', ['member4', 'member2', 'member3']) == [0, 1, 1]
# should also work if provided values as arguments
assert r.smismember('foo', 'member4', 'member2', 'member3') == [0, 1, 1]

@redis4_and_above
def test_smismember_wrong_type(r):
# verify that command fails when the key itself is not a SET
zadd(r, 'foo', {'member': 1})
with pytest.raises(redis.ResponseError):
r.smismember('foo', 'member')

# verify that command fails if the input parameter is of wrong type
r.sadd('foo2', 'member1', 'member2', 'member3')
with pytest.raises(redis.DataError, match='Invalid input of type'):
r.smismember('foo2', [["member1", "member2"]])


def test_smembers(r):
assert r.smembers('foo') == set()
Expand Down

0 comments on commit 1947736

Please sign in to comment.