Skip to content

Commit 6832705

Browse files
authored
Update FUNCTION LOAD changes (#2139)
1 parent 6ba4641 commit 6832705

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

redis/client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,6 @@ class AbstractRedis:
760760
"DEBUG OBJECT": parse_debug_object,
761761
"FUNCTION DELETE": bool_ok,
762762
"FUNCTION FLUSH": bool_ok,
763-
"FUNCTION LOAD": bool_ok,
764763
"FUNCTION RESTORE": bool_ok,
765764
"GEOHASH": lambda r: list(map(str_if_bytes, r)),
766765
"GEOPOS": lambda r: list(

redis/commands/core.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5703,28 +5703,20 @@ class FunctionCommands:
57035703

57045704
def function_load(
57055705
self,
5706-
engine: str,
5707-
library: str,
57085706
code: str,
57095707
replace: Optional[bool] = False,
5710-
description: Optional[str] = None,
57115708
) -> Union[Awaitable[str], str]:
57125709
"""
57135710
Load a library to Redis.
5714-
:param engine: the name of the execution engine for the library
5715-
:param library: the unique name of the library
5716-
:param code: the source code
5717-
:param replace: changes the behavior to replace the library if a library called
5718-
``library`` already exists
5719-
:param description: description to the library
5711+
:param code: the source code (must start with
5712+
Shebang statement that provides a metadata about the library)
5713+
:param replace: changes the behavior to overwrite the existing library
5714+
with the new contents.
5715+
Return the library name that was loaded.
57205716
57215717
For more information see https://redis.io/commands/function-load
57225718
"""
5723-
pieces = [engine, library]
5724-
if replace:
5725-
pieces.append("REPLACE")
5726-
if description is not None:
5727-
pieces.append(description)
5719+
pieces = ["REPLACE"] if replace else []
57285720
pieces.append(code)
57295721
return self.execute_command("FUNCTION LOAD", *pieces)
57305722

tests/test_function.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
from .conftest import skip_if_server_version_lt
66

7-
function = "redis.register_function('myfunc', function(keys, args) return args[1] end)"
7+
engine = "lua"
8+
lib = "mylib"
9+
lib2 = "mylib2"
10+
function = "redis.register_function{function_name='myfunc', callback=function(keys, \
11+
args) return args[1] end, flags={ 'no-writes' }}"
812
function2 = "redis.register_function('hello', function() return 'Hello World' end)"
9-
set_function = "redis.register_function('set', function(keys, args) \
10-
return redis.call('SET', keys[1], args[1]) end)"
11-
get_function = "redis.register_function('get', function(keys, args) \
12-
return redis.call('GET', keys[1]) end)"
13+
set_function = "redis.register_function('set', function(keys, args) return \
14+
redis.call('SET', keys[1], args[1]) end)"
15+
get_function = "redis.register_function('get', function(keys, args) return \
16+
redis.call('GET', keys[1]) end)"
1317

1418

1519
@skip_if_server_version_lt("7.0.0")
@@ -19,25 +23,28 @@ def reset_functions(self, r):
1923
r.function_flush()
2024

2125
def test_function_load(self, r):
22-
assert r.function_load("Lua", "mylib", function)
23-
assert r.function_load("Lua", "mylib", function, replace=True)
26+
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
27+
assert lib == r.function_load(f"#!{engine} name={lib} \n {function}")
28+
assert lib == r.function_load(
29+
f"#!{engine} name={lib} \n {function}", replace=True
30+
)
2431
with pytest.raises(ResponseError):
25-
r.function_load("Lua", "mylib", function)
32+
r.function_load(f"#!{engine} name={lib} \n {function}")
2633
with pytest.raises(ResponseError):
27-
r.function_load("Lua", "mylib2", function)
34+
r.function_load(f"#!{engine} name={lib2} \n {function}")
2835

2936
def test_function_delete(self, r):
30-
r.function_load("Lua", "mylib", set_function)
37+
r.function_load(f"#!{engine} name={lib} \n {set_function}")
3138
with pytest.raises(ResponseError):
32-
r.function_load("Lua", "mylib", set_function)
39+
r.function_load(f"#!{engine} name={lib} \n {set_function}")
3340
assert r.fcall("set", 1, "foo", "bar") == "OK"
3441
assert r.function_delete("mylib")
3542
with pytest.raises(ResponseError):
3643
r.fcall("set", 1, "foo", "bar")
37-
assert r.function_load("Lua", "mylib", set_function)
44+
assert lib == r.function_load(f"#!{engine} name={lib} \n {set_function}")
3845

3946
def test_function_flush(self, r):
40-
r.function_load("Lua", "mylib", function)
47+
r.function_load(f"#!{engine} name={lib} \n {function}")
4148
assert r.fcall("myfunc", 0, "hello") == "hello"
4249
assert r.function_flush()
4350
with pytest.raises(ResponseError):
@@ -47,36 +54,35 @@ def test_function_flush(self, r):
4754

4855
@pytest.mark.onlynoncluster
4956
def test_function_list(self, r):
50-
r.function_load("Lua", "mylib", function)
57+
r.function_load(f"#!{engine} name={lib} \n {function}")
5158
res = [
5259
[
5360
"library_name",
5461
"mylib",
5562
"engine",
5663
"LUA",
57-
"description",
58-
None,
5964
"functions",
60-
[["name", "myfunc", "description", None]],
65+
[["name", "myfunc", "description", None, "flags", ["no-writes"]]],
6166
],
6267
]
6368
assert r.function_list() == res
6469
assert r.function_list(library="*lib") == res
65-
assert r.function_list(withcode=True)[0][9] == function
70+
assert (
71+
r.function_list(withcode=True)[0][7]
72+
== f"#!{engine} name={lib} \n {function}"
73+
)
6674

6775
@pytest.mark.onlycluster
6876
def test_function_list_on_cluster(self, r):
69-
r.function_load("Lua", "mylib", function)
77+
r.function_load(f"#!{engine} name={lib} \n {function}")
7078
function_list = [
7179
[
7280
"library_name",
7381
"mylib",
7482
"engine",
7583
"LUA",
76-
"description",
77-
None,
7884
"functions",
79-
[["name", "myfunc", "description", None]],
85+
[["name", "myfunc", "description", None, "flags", ["no-writes"]]],
8086
],
8187
]
8288
primaries = r.get_primaries()
@@ -86,33 +92,36 @@ def test_function_list_on_cluster(self, r):
8692
assert r.function_list() == res
8793
assert r.function_list(library="*lib") == res
8894
node = primaries[0].name
89-
assert r.function_list(withcode=True)[node][0][9] == function
95+
assert (
96+
r.function_list(withcode=True)[node][0][7]
97+
== f"#!{engine} name={lib} \n {function}"
98+
)
9099

91100
def test_fcall(self, r):
92-
r.function_load("Lua", "mylib", set_function)
93-
r.function_load("Lua", "mylib2", get_function)
101+
r.function_load(f"#!{engine} name={lib} \n {set_function}")
102+
r.function_load(f"#!{engine} name={lib2} \n {get_function}")
94103
assert r.fcall("set", 1, "foo", "bar") == "OK"
95104
assert r.fcall("get", 1, "foo") == "bar"
96105
with pytest.raises(ResponseError):
97106
r.fcall("myfunc", 0, "hello")
98107

99108
def test_fcall_ro(self, r):
100-
r.function_load("Lua", "mylib", function)
109+
r.function_load(f"#!{engine} name={lib} \n {function}")
101110
assert r.fcall_ro("myfunc", 0, "hello") == "hello"
102-
r.function_load("Lua", "mylib2", set_function)
111+
r.function_load(f"#!{engine} name={lib2} \n {set_function}")
103112
with pytest.raises(ResponseError):
104113
r.fcall_ro("set", 1, "foo", "bar")
105114

106115
def test_function_dump_restore(self, r):
107-
r.function_load("Lua", "mylib", set_function)
116+
r.function_load(f"#!{engine} name={lib} \n {set_function}")
108117
payload = r.function_dump()
109118
assert r.fcall("set", 1, "foo", "bar") == "OK"
110119
r.function_delete("mylib")
111120
with pytest.raises(ResponseError):
112121
r.fcall("set", 1, "foo", "bar")
113122
assert r.function_restore(payload)
114123
assert r.fcall("set", 1, "foo", "bar") == "OK"
115-
r.function_load("Lua", "mylib2", get_function)
124+
r.function_load(f"#!{engine} name={lib2} \n {get_function}")
116125
assert r.fcall("get", 1, "foo") == "bar"
117126
r.function_delete("mylib")
118127
assert r.function_restore(payload, "FLUSH")

0 commit comments

Comments
 (0)