diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/cli_tests/test_cli_start.py | 8 | ||||
-rw-r--r-- | tests/cli_tests/test_config.py | 34 | ||||
-rw-r--r-- | tests/unittests/command_parse/test_hash_parse.py | 21 | ||||
-rw-r--r-- | tests/unittests/test_client.py | 76 |
4 files changed, 119 insertions, 20 deletions
diff --git a/tests/cli_tests/test_cli_start.py b/tests/cli_tests/test_cli_start.py index 00fcbf7..8045947 100644 --- a/tests/cli_tests/test_cli_start.py +++ b/tests/cli_tests/test_cli_start.py @@ -81,3 +81,11 @@ def test_connect_via_socket(fake_redis_socket): c.expect("redis /tmp/test.sock") c.close() + + +def test_iredis_start_with_prompt(): + cli = pexpect.spawn("iredis --prompt '{host}abc{port}def{client_name}'", timeout=2) + cli.logfile_read = open("cli_test.log", "ab") + cli.expect("iredis") + cli.expect("127.0.0.1abc6379defNone") + cli.close() diff --git a/tests/cli_tests/test_config.py b/tests/cli_tests/test_config.py index 7d6839d..1795c62 100644 --- a/tests/cli_tests/test_config.py +++ b/tests/cli_tests/test_config.py @@ -23,3 +23,37 @@ def test_log_location_config(): content = logfile.read() assert len(content) > 100 + + +def test_load_prompt_from_config(iredis_client, clean_redis): + config_content = dedent( + """ + [main] + prompt = {host}abc{port}xx{db} + """ + ) + with open("/tmp/iredisrc", "w+") as etc_config: + etc_config.write(config_content) + + cli = pexpect.spawn("iredis -n 15 --iredisrc /tmp/iredisrc", timeout=1) + cli.expect("iredis") + cli.expect("127.0.0.1abc6379xx15") + cli.close() + + +def test_prompt_cli_overwrite_config(iredis_client, clean_redis): + config_content = dedent( + """ + [main] + prompt = {host}abc{port}xx{db} + """ + ) + with open("/tmp/iredisrc", "w+") as etc_config: + etc_config.write(config_content) + + cli = pexpect.spawn( + "iredis -n 15 --iredisrc /tmp/iredisrc --prompt='{db}-12345'", timeout=1 + ) + cli.expect("iredis") + cli.expect("15-12345") + cli.close() diff --git a/tests/unittests/command_parse/test_hash_parse.py b/tests/unittests/command_parse/test_hash_parse.py index 0c3aa04..2bde9e0 100644 --- a/tests/unittests/command_parse/test_hash_parse.py +++ b/tests/unittests/command_parse/test_hash_parse.py @@ -43,3 +43,24 @@ def test_hset(judge_command): "HSET foo bar hello", {"command": "HSET", "key": "foo", "field": "bar", "value": "hello"}, ) + + +def test_hrandfield(judge_command): + judge_command( + "HRANDFIELD coin", + {"command": "HRANDFIELD", "key": "coin"}, + ) + judge_command( + "HRANDFIELD coin -5 WITHVALUES", + { + "command": "HRANDFIELD", + "key": "coin", + "count": "-5", + "withvalues_const": "WITHVALUES", + }, + ) + judge_command( + "HRANDFIELD coin -5", + {"command": "HRANDFIELD", "key": "coin", "count": "-5"}, + ) + judge_command("HRANDFIELD coin WITHVALUES", None) diff --git a/tests/unittests/test_client.py b/tests/unittests/test_client.py index 5c6628f..45f0054 100644 --- a/tests/unittests/test_client.py +++ b/tests/unittests/test_client.py @@ -1,3 +1,4 @@ +import os import re import pytest import redis @@ -20,6 +21,11 @@ def completer(): return IRedisCompleter() +zset_type = "ziplist" +if os.environ["REDIS_VERSION"] == "7": + zset_type = "listpack" + + @pytest.mark.parametrize( "_input, command_name, expect_args", [ @@ -169,7 +175,16 @@ def test_not_retry_on_authentication_error(iredis_client, config): iredis_client.execute("None", "GET", ["foo"]) -@pytest.mark.skipif("int(os.environ['REDIS_VERSION']) < 6") +@pytest.mark.skipif( + "int(os.environ['REDIS_VERSION']) != 6", + reason=""" +in redis7, it will not work if you: +1. connect redis without password +2. set a password +3. auth + +the auth will fail""", +) def test_auto_select_db_and_auth_for_reconnect_only_6(iredis_client, config): config.retry_times = 2 config.raw = True @@ -256,6 +271,13 @@ def test_peek_key_not_exist(iredis_client, clean_redis, config): assert peek_result == ["non-exist-key doesn't exist."] +def test_iredis_with_username(): + with patch("redis.connection.Connection.connect"): + c = Client("127.0.0.1", "6379", username="abc", password="abc1") + assert c.connection.username == "abc" + assert c.connection.password == "abc1" + + def test_peek_string(iredis_client, clean_redis): clean_redis.set("foo", "bar") peek_result = list(iredis_client.do_peek("foo")) @@ -337,12 +359,13 @@ def test_peek_zset_fetch_all(iredis_client, clean_redis): "myzset", dict(zip([f"hello-{index}" for index in range(3)], range(3))) ) peek_result = list(iredis_client.do_peek("myzset")) + formatted_text_rematch( peek_result[0][0:9], FormattedText( [ ("class:dockey", "key: "), - ("", r"zset \(ziplist\) mem: \d+ bytes, ttl: -1"), + ("", rf"zset \({zset_type}\) mem: \d+ bytes, ttl: -1"), ("", "\n"), ("class:dockey", "zcount: "), ("", "3"), @@ -365,7 +388,7 @@ def test_peek_zset_fetch_part(iredis_client, clean_redis): FormattedText( [ ("class:dockey", "key: "), - ("", r"zset \(ziplist\) mem: \d+ bytes, ttl: -1"), + ("", rf"zset \({zset_type}\) mem: \d+ bytes, ttl: -1"), ("", "\n"), ("class:dockey", "zcount: "), ("", "40"), @@ -527,29 +550,23 @@ def test_version_parse_for_auth(iredis_client): "info, version", [ ( - ( - "# Server\r\nredis_version:df--128-NOTFOUND\r\n" - "redis_mode:standalone\r\narch_bits:64" - ), + "# Server\r\nredis_version:df--128-NOTFOUND\r\n" + "redis_mode:standalone\r\narch_bits:64", "df--128-NOTFOUND", ), ( - ( - "# Server\r\nredis_version:6.2.5\r\n" - "redis_git_sha1:00000000\r\n" - "redis_git_dirty:0\r\n" - "redis_build_id:915e5480613bc9b6\r\n" - "redis_mode:standalone " - ), + "# Server\r\nredis_version:6.2.5\r\n" + "redis_git_sha1:00000000\r\n" + "redis_git_dirty:0\r\n" + "redis_build_id:915e5480613bc9b6\r\n" + "redis_mode:standalone ", "6.2.5", ), ( - ( - "# Server\r\nredis_version:5.0.14.1\r\n" - "redis_git_sha1:00000000\r\nredis_git_dirty:0\r\n" - "redis_build_id:915e5480613bc9b6\r\n" - "redis_mode:standalone " - ), + "# Server\r\nredis_version:5.0.14.1\r\n" + "redis_git_sha1:00000000\r\nredis_git_dirty:0\r\n" + "redis_build_id:915e5480613bc9b6\r\n" + "redis_mode:standalone ", "5.0.14.1", ), ], @@ -564,3 +581,22 @@ def test_version_path(info, version): client = Client("127.0.0.1", "6379", None) client.get_server_info() assert mock_config.version == version + + +def test_prompt(): + c = Client() + assert str(c) == "127.0.0.1:6379> " + + c = Client(prompt="{host} {port} {db}") + assert str(c) == "127.0.0.1 6379 0" + + c = Client(prompt="{host} {port} {db} {username}") + assert str(c) == "127.0.0.1 6379 0 None" + + c = Client(prompt="{host} {port} {db} {username}", username="foo1") + assert str(c) == "127.0.0.1 6379 0 foo1" + + c = Client(prompt="{client_id} aabc") + assert re.match(r"^\d+ aabc$", str(c)) + c = Client(prompt="{client_addr} >") + assert re.match(r"^127.0.0.1:\d+ >$", str(c)) |