Skip to content

Commit

Permalink
add scard (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ authored Jun 6, 2024
1 parent 6806556 commit 59ceb51
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
15 changes: 15 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,18 @@ def test_lpop(client, request):
assert c.lpop("list") == "element2"
assert c.lpop("list") is None
assert c.lpop("nonexistent") is None


@pytest.mark.parametrize("client", ["znsclient", "redisclient"])
def test_scard(client, request):
c = request.getfixturevalue("znsclient")
c.sadd("set", "member1")
c.sadd("set", "member2")
assert c.scard("set") == 2
assert c.scard("nonexistent") == 0
c.delete("set")
assert c.scard("set") == 0
c.sadd("set", "member1")
assert c.scard("set") == 1
c.sadd("set", "member2")
assert c.scard("set") == 2
3 changes: 3 additions & 0 deletions znsocket/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,6 @@ def hvals(self, name):

def lpop(self, name):
return self.sio.call("lpop", {"name": name})

def scard(self, name):
return self.sio.call("scard", {"name": name})
10 changes: 9 additions & 1 deletion znsocket/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def hvals(sid, data):
return []

@sio.event
def lpop(sid, data):
def lpop(sid, data) -> t.Optional[t.Any]:
name = data.pop("name")
try:
return storage[name].pop(0)
Expand All @@ -296,4 +296,12 @@ def lpop(sid, data):
except IndexError:
return None

@sio.event
def scard(sid, data) -> int:
name = data.pop("name")
try:
return len(storage[name])
except KeyError:
return 0

return sio

0 comments on commit 59ceb51

Please sign in to comment.