Skip to content

Commit

Permalink
Refactor split cmd as list
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbodong22011 committed Aug 16, 2023
1 parent b637f71 commit c5ad263
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 150 deletions.
83 changes: 7 additions & 76 deletions cts.json
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,7 @@
"two", "2",
"one", "3"
]
],
],
"since": "1.2.0"
},

Expand Down Expand Up @@ -3183,7 +3183,7 @@
"result": [
"OK",
[
"matches",
"matches",
[
[
[1,1],
Expand All @@ -3210,7 +3210,7 @@
"result": [
"OK",
[
"matches",
"matches",
[
[
[1,1],
Expand All @@ -3237,7 +3237,7 @@
"result": [
"OK",
[
"matches",
"matches",
[
[
[1,1],
Expand Down Expand Up @@ -3898,7 +3898,7 @@
],
"since": "2.8.7"
},

{
"name": "bitpos with BYTE / BIT",
"command": [
Expand Down Expand Up @@ -5221,23 +5221,6 @@
"command_split": true
},

{
"name": "function dump command",
"command": [
"function flush",
"function load \"#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)\"",
"function dump"
],
"result": [
"OK",
"mylib",
"\\xf5\\xc3@X@]\\x1f#!lua name=mylib \\n redis.registe\\rr_function(\\'my@\\x0b\\x02\\', @\\x06`\\x12\\nkeys, args) 6\\x03turn`\\x0c\\x07[1] end)\\x0b\\x00\\x181K\\x11\\x14\\xbbS\\xc7"
],
"since": "7.0.0",
"result_binary": true,
"command_split": true
},

{
"name": "function flush command",
"command": [
Expand All @@ -5249,19 +5232,6 @@
"since": "7.0.0"
},

{
"name": "function kill command",
"command": [
"function flush",
"function kill"
],
"command_split": true,
"result": [
"OK",
"NOTBUSY No scripts in execution right now."
],
"since": "7.0.0"
},

{
"name": "function list command",
Expand Down Expand Up @@ -5313,22 +5283,15 @@
"name": "function restore command",
"command": [
"function flush",
"function load \"#!lua name=mylib \n redis.register_function('myfunc', function(keys, args) return args[1] end)\"",
"function dump",
"function restore \"\\xf5\\xc3@X@]\\x1f#!lua name=mylib \\n redis.registe\\rr_function(\\'my@\\x0b\\x02\\', @\\x06`\\x12\\nkeys, args) 6\\x03turn`\\x0c\\x07[1] end)\\x0b\\x00\\x181K\\x11\\x14\\xbbS\\xc7\" flush",
"function flush"
"function restore \"\\xf5\\xc3@X@]\\x1f#!lua name=mylib \\n redis.registe\\rr_function('my@\\x0b\\x02', @\\x06`\\x12\\nkeys, args) 6\\x03turn`\\x0c\\a[1] end)\\n\\x00q\\xee\\xf6b\\xe9$\\xbaN\" flush"
],
"result": [
"OK",
"mylib",
"\\xf5\\xc3@X@]\\x1f#!lua name=mylib \\n redis.registe\\rr_function(\\'my@\\x0b\\x02\\', @\\x06`\\x12\\nkeys, args) 6\\x03turn`\\x0c\\x07[1] end)\\x0b\\x00\\x181K\\x11\\x14\\xbbS\\xc7",
"test",
"OK"
],
"since": "7.0.0",
"command_split": true,
"command_binary": true,
"result_binary": true
"command_binary": true
},

{
Expand All @@ -5354,23 +5317,6 @@
"since": "7.0.0"
},

{
"name": "script debug command",
"command": [
"script debug yes",
"script debug no",
"script debug sync",
"script debug no"
],
"result": [
"OK",
"OK",
"OK",
"OK"
],
"since": "3.2.0"
},

{
"name": "script exists command",
"command": [
Expand Down Expand Up @@ -5427,21 +5373,6 @@
"since": "6.2.0"
},

{
"name": "scritpt kill command",
"command": [
"script load return",
"evalsha 63143b6f8007b98c53ca2149822777b3566f9241 0",
"script flush"
],
"result": [
"63143b6f8007b98c53ca2149822777b3566f9241",
null,
"True"
],
"since": "2.6.0"
},

{
"name": "script load command",
"command": [
Expand Down
95 changes: 21 additions & 74 deletions redis_compatibility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def trans_result_to_bytes(result):
return result


def trans_cmd_binary(test, cmd):
def trans_cmd_to_binary(test, cmd):
if 'command_binary' in test:
array = bytearray()
i = 0
Expand Down Expand Up @@ -141,77 +141,28 @@ def trans_cmd_binary(test, cmd):
return cmd


def trans_cmd_split(test, cmd):
def split_cmd_as_list(test, cmd):
if 'command_split' in test:
# split command by ""
# input: 'hello "world of python" example'
# output: ['hello', 'world of python', 'example']
if isinstance(cmd, bytes):
parts = []
in_quote = False
current_part = b''
for char in cmd:
if char == ord('"'):
in_quote = not in_quote
elif char == ord(' ') and not in_quote:
parts.append(current_part)
current_part = b''
else:
current_part += bytes([char])
parts.append(current_part)
return parts
else:
parts = []
in_quote = False
current_part = ''
for char in cmd:
if char == '"':
in_quote = not in_quote
elif char == ' ' and not in_quote:
parts.append(current_part)
current_part = ''
else:
current_part += char
parts.append(current_part)
return parts
else:
return cmd


def trans_result_binary(test, result):
if 'result_binary' in test:
if isinstance(result, bytes):
result = result.decode()
array = bytearray()
i = 0
while i < len(result):
if result[i] == '\\' and i + 1 < len(result):
if result[i + 1] == '\\':
array.append(92)
elif result[i + 1] == '\'':
array.append(39)
elif result[i + 1] == '"':
array.append(34)
elif result[i + 1] == 'n':
array.append(10)
elif result[i + 1] == 'r':
array.append(13)
elif result[i + 1] == 't':
array.append(9)
elif result[i + 1] == 'a':
array.append(7)
elif result[i + 1] == 'b':
array.append(8)
elif result[i + 1] == 'x' and i + 3 < len(result):
array.append(int(result[i + 2:i + 4], 16))
i += 2
i += 2
parts = []
in_quote = False
current_part = b''
for char in cmd:
# If command_binary is true, then char is `int`, otherwise char is str.
byte = ord(char) if isinstance(char, str) else char
if byte == ord('"'):
in_quote = not in_quote
elif byte == ord(' ') and not in_quote:
parts.append(current_part)
current_part = b''
else:
array.append(ord(result[i]))
i += 1
return bytes(array)
current_part += char.encode() if isinstance(char, str) else bytes([char])
parts.append(current_part)
return parts
else:
return result
return cmd


def sort_nested_list(result):
Expand Down Expand Up @@ -246,25 +197,21 @@ def run_test(test):
trans_result_to_bytes(result)
try:
for idx, cmd in enumerate(command):
cmd = trans_cmd_binary(test, cmd)
tcmd = trans_cmd_split(test, cmd)
cmd = trans_cmd_to_binary(test, cmd)
tcmd = split_cmd_as_list(test, cmd)
if isinstance(tcmd, list):
ret = trans_result_to_bytes(r.execute_command(*tcmd))
else:
ret = trans_result_to_bytes(r.execute_command(tcmd))
if 'sort_result' in test and isinstance(result[idx], list):
sort_nested_list(ret)
sort_nested_list(result[idx])
result[idx] = trans_result_binary(test, result[idx])
if result[idx] != ret:
test_failed(g_results[since], name, f"expected: {result[idx]}, result: {ret}")
return
test_passed(g_results[since])
except Exception as e:
if name == "function kill command" and str(e) == "NOTBUSY No scripts in execution right now.":
test_passed(g_results[since])
else:
test_failed(g_results[since], name, e)
test_failed(g_results[since], name, e)


def run_compatibility_tests(filename):
Expand All @@ -284,7 +231,7 @@ def generate_html_report(logdir, configs):
html = open(filepath, "w")
html.write("This page is automatically generated by <a href=\"https://github.com/tair-opensource/"
"compatibility-test-suite-for-redis\">compatibility-test-suite-for-redis</a>"
"to show the compatibility of the following Redis-Like systems and different versions of Redis.<br><br>")
" to show the compatibility of the following Redis-Like systems and different versions of Redis.<br><br>")
html.write("<table>")
# generate header
html.write("<thead>")
Expand Down

0 comments on commit c5ad263

Please sign in to comment.