Skip to content

Commit

Permalink
Add support for SCRIPT EXISTS subcommand
Browse files Browse the repository at this point in the history
I've added support for the SCRIPT EXISTS subcommand in the SCRIPTS
command handler. The subcommand takes in a sequence of sha1 hash values
of Lua scripts and checks if scripts with those sha1 hash values are
currently registered with Redis.

The command will return a redis List with the integers 1 and 0 to
indicate which sha1 hash is known to Redis. In accordance with Redis,
this subcommand returns an empty List if no hash values were passed as
arguments to the subcommand.
  • Loading branch information
SebastiaanZ committed Oct 4, 2020
1 parent aaaad4b commit 091f15d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ scripting
---------

* script debug
* script exists
* script flush
* script kill

Expand Down
4 changes: 3 additions & 1 deletion fakeredis/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2316,7 +2316,7 @@ def time(self):
return [str(now_s).encode(), str(now_us).encode()]

# Script commands
# TODO: script exists, script flush
# TODO: script flush
# (script debug and script kill will probably not be supported)

def _convert_redis_arg(self, lua_runtime, value):
Expand Down Expand Up @@ -2475,6 +2475,8 @@ def script(self, subcmd, *args):
sha1 = hashlib.sha1(script).hexdigest().encode()
self._server.script_cache[sha1] = script
return sha1
elif casematch(subcmd, b'exists'):
return [int(sha1 in self._server.script_cache) for sha1 in args]
else:
raise SimpleError(BAD_SUBCOMMAND_MSG.format('SCRIPT'))

Expand Down
20 changes: 20 additions & 0 deletions test/test_fakeredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4486,6 +4486,26 @@ def test_script(r):
assert result == b'42'


def test_script_exists(r):
# test response for no arguments by bypassing the py-redis command
# as it requires at least one argument
assert raw_command(r, "SCRIPT EXISTS") == []

# use single character characters for non-existing scripts, as those
# will never be equal to an actual sha1 hash digest
assert r.script_exists("a") == [0]
assert r.script_exists("a", "b", "c", "d", "e", "f") == [0, 0, 0, 0, 0, 0]

sha1_one = r.script_load("return 'a'")
assert r.script_exists(sha1_one) == [1]
assert r.script_exists(sha1_one, "a") == [1, 0]
assert r.script_exists("a", "b", "c", sha1_one, "e") == [0, 0, 0, 1, 0]

sha1_two = r.script_load("return 'b'")
assert r.script_exists(sha1_one, sha1_two) == [1, 1]
assert r.script_exists("a", sha1_one, "c", sha1_two, "e", "f") == [0, 1, 0, 1, 0, 0]


@fake_only
def test_lua_log(r, caplog):
logger = fakeredis._server.LOGGER
Expand Down

0 comments on commit 091f15d

Please sign in to comment.