Skip to content

Commit

Permalink
Add decr command
Browse files Browse the repository at this point in the history
  • Loading branch information
ngokchaoho committed Dec 18, 2023
1 parent 8555dbe commit e00702b
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
46 changes: 44 additions & 2 deletions pyredis/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ def _handle_exists(command, datastore):
return Error("ERR wrong number of arguments for 'exists' command")


def _handle_del(command, datastore):
if len(command) >= 2:
found = 0
for key in command[1:]:
if key.data.decode() in datastore:
del datastore._data[key.data.decode()]
found += 1
return Integer(found)
else:
return Error("ERR wrong number of arguments for 'del' command")


def _handle_incr(command, datastore):
if len(command) == 2:
key = command[1].data.decode()
try:
value = datastore[key] + 1
except KeyError:
value = 1 # first time increase
datastore[key] = value
return Integer(value)
return Error("ERR wrong number of arguments for 'incr' command")


def _handle_decr(command, datastore):
if len(command) == 2:
key = command[1].data.decode()
try:
value = datastore[key] - 1
except KeyError:
value = -1
except TypeError:
return Error("value is not an integer or out of range")
datastore[key] = value
return Integer(value)
return Error("ERR wrong number of arguments for 'decr' command")


def _handle_unrecognised_command(command, *args):
args = " ".join((f"'{c.data.decode()}'" for c in command[1:]))
return Error(
Expand All @@ -87,10 +125,14 @@ def handle_command(command, datastore):

case "SET":
return _handle_set(command, datastore)

case "GET":
return _handle_get(command, datastore)
case "EXISTS":
return _handle_exists(command, datastore)

case "DEL":
return _handle_del(command, datastore)
case "INCR":
return _handle_incr(command, datastore)
case "DECR":
return _handle_decr(command, datastore)
return _handle_unrecognised_command(command)
54 changes: 54 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ def datastore():
),
Integer(1),
),
# DEL tests
(
Array([BulkString(b"SET"), SimpleString(b"key1"), SimpleString(b"Hello")]),
SimpleString("OK"),
),
(
Array([BulkString(b"SET"), SimpleString(b"key2"), SimpleString(b"World")]),
SimpleString("OK"),
),
(
Array(
[
BulkString(b"DEL"),
SimpleString(b"key1"),
SimpleString(b"key2"),
SimpleString(b"key3"),
]
),
Integer(2),
),
],
)
def test_handle_command(command, expected, datastore):
Expand Down Expand Up @@ -187,3 +207,37 @@ def test_expire_on_read(datastore):
sleep(0.15)
with pytest.raises(KeyError):
datastore["key"]


# Incr Tests
def test_handle_incr_command_valid_key():
datastore = DataStore()
result = handle_command(
Array([BulkString(b"incr"), SimpleString(b"ki")]), datastore
)
assert result == Integer(1)
result = handle_command(
Array([BulkString(b"incr"), SimpleString(b"ki")]), datastore
)
assert result == Integer(2)


# Decr Tests
def test_handle_decr():
datastore = DataStore()
result = handle_command(
Array([BulkString(b"incr"), SimpleString(b"kd")]), datastore
)
assert result == Integer(1)
result = handle_command(
Array([BulkString(b"incr"), SimpleString(b"kd")]), datastore
)
assert result == Integer(2)
result = handle_command(
Array([BulkString(b"decr"), SimpleString(b"kd")]), datastore
)
assert result == Integer(1)
result = handle_command(
Array([BulkString(b"decr"), SimpleString(b"kd")]), datastore
)
assert result == Integer(0)

0 comments on commit e00702b

Please sign in to comment.