From 6ca031c3f82038a89e3fba2ccd34080aad063002 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Sun, 4 Oct 2020 14:55:19 +0200 Subject: [PATCH] Add support for SCRIPT FLUSH subcommand This commit adds support for the SCRIPT FLUSH subcommand which flushes all the loaded scripts from Redis. The subcommand takes no arguments and returns OK after completing its work. --- README.rst | 1 - fakeredis/_server.py | 8 ++++++-- test/test_fakeredis.py | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index cc4920c..374f222 100644 --- a/README.rst +++ b/README.rst @@ -310,7 +310,6 @@ scripting --------- * script debug - * script flush * script kill diff --git a/fakeredis/_server.py b/fakeredis/_server.py index 12541a2..d114a1a 100644 --- a/fakeredis/_server.py +++ b/fakeredis/_server.py @@ -2316,8 +2316,7 @@ def time(self): return [str(now_s).encode(), str(now_us).encode()] # Script commands - # TODO: script flush - # (script debug and script kill will probably not be supported) + # script debug and script kill will probably not be supported def _convert_redis_arg(self, lua_runtime, value): # Type checks are exact to avoid issues like bool being a subclass of int. @@ -2477,6 +2476,11 @@ def script(self, subcmd, *args): return sha1 elif casematch(subcmd, b'exists'): return [int(sha1 in self._server.script_cache) for sha1 in args] + elif casematch(subcmd, b'flush'): + if len(args) != 0: + raise SimpleError(BAD_SUBCOMMAND_MSG.format('SCRIPT')) + self._server.script_cache = {} + return OK else: raise SimpleError(BAD_SUBCOMMAND_MSG.format('SCRIPT')) diff --git a/test/test_fakeredis.py b/test/test_fakeredis.py index 2b150f2..1541cff 100644 --- a/test/test_fakeredis.py +++ b/test/test_fakeredis.py @@ -4506,6 +4506,26 @@ def test_script_exists(r): assert r.script_exists("a", sha1_one, "c", sha1_two, "e", "f") == [0, 1, 0, 1, 0, 0] +@pytest.mark.parametrize("args", [("a",), tuple("abcdefghijklmn")]) +def test_script_flush_errors_with_args(r, args): + with pytest.raises(redis.ResponseError): + raw_command(r, "SCRIPT FLUSH %s" % " ".join(args)) + + +def test_script_flush(r): + # generate/load six unique scripts and store their sha1 hash values + sha1_values = [r.script_load("return '%s'" % char) for char in "abcdef"] + + # assert the scripts all exist prior to flushing + assert r.script_exists(*sha1_values) == [1] * len(sha1_values) + + # flush and assert OK response + assert r.script_flush() is True + + # assert none of the scripts exists after flushing + assert r.script_exists(*sha1_values) == [0] * len(sha1_values) + + @fake_only def test_lua_log(r, caplog): logger = fakeredis._server.LOGGER