| 1 |
import os |
| 2 |
import re |
| 3 |
import tempfile |
| 4 |
from textwrap import dedent |
| 5 |
|
| 6 |
import pexpect |
| 7 |
import pytest |
| 8 |
import redis |
| 9 |
|
| 10 |
from iredis.client import Client |
| 11 |
from iredis.commands import split_command_args |
| 12 |
from iredis.redis_grammar import get_command_grammar |
| 13 |
from iredis.exceptions import InvalidArguments |
| 14 |
from iredis.config import Config, config as global_config |
| 15 |
|
| 16 |
TIMEOUT = 5 |
| 17 |
HISTORY_FILE = ".iredis_history" |
| 18 |
|
| 19 |
|
| 20 |
@pytest.fixture |
| 21 |
def token_should_match(): |
| 22 |
def match_func(token, tomatch): |
| 23 |
assert re.fullmatch(token, tomatch) is not None |
| 24 |
|
| 25 |
return match_func |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def token_should_not_match(): |
| 30 |
def match_func(token, tomatch): |
| 31 |
assert re.fullmatch(token, tomatch) is None |
| 32 |
|
| 33 |
return match_func |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def judge_command(): |
| 38 |
def judge_command_func(input_text, expect): |
| 39 |
if expect == "invalid": |
| 40 |
with pytest.raises(InvalidArguments): |
| 41 |
split_command_args(input_text) |
| 42 |
return |
| 43 |
|
| 44 |
command, _ = split_command_args(input_text) |
| 45 |
grammar = get_command_grammar(command) |
| 46 |
|
| 47 |
m = grammar.match(input_text) |
| 48 |
|
| 49 |
# test on not match |
| 50 |
if not expect: |
| 51 |
assert m is None |
| 52 |
return |
| 53 |
|
| 54 |
variables = m.variables() |
| 55 |
print(f"Found variables: {variables}") |
| 56 |
for expect_token, expect_value in expect.items(): |
| 57 |
all_variables = variables.getall(expect_token) |
| 58 |
if len(all_variables) > 1: |
| 59 |
assert sorted(all_variables) == sorted(expect_value) |
| 60 |
else: |
| 61 |
assert variables.get(expect_token) == expect_value |
| 62 |
|
| 63 |
return judge_command_func |
| 64 |
|
| 65 |
|
| 66 |
@pytest.fixture(scope="function") |
| 67 |
def clean_redis(): |
| 68 |
""" |
| 69 |
Return a empty redis db. (redis-py client) |
| 70 |
""" |
| 71 |
client = redis.StrictRedis(db=15) |
| 72 |
client.flushdb() |
| 73 |
return client |
| 74 |
|
| 75 |
|
| 76 |
@pytest.fixture |
| 77 |
def iredis_client(): |
| 78 |
return Client("127.0.0.1", "6379", db=15) |
| 79 |
|
| 80 |
|
| 81 |
@pytest.fixture |
| 82 |
def config(): |
| 83 |
newconfig = Config() |
| 84 |
global_config.__dict__ = newconfig.__dict__ |
| 85 |
config.raw = False |
| 86 |
return global_config |
| 87 |
|
| 88 |
|
| 89 |
@pytest.fixture(scope="function") |
| 90 |
def cli(): |
| 91 |
"""Open iredis subprocess to test""" |
| 92 |
f = tempfile.TemporaryFile("w") |
| 93 |
config_content = dedent( |
| 94 |
""" |
| 95 |
[main] |
| 96 |
log_location = |
| 97 |
warning = True |
| 98 |
""" |
| 99 |
) |
| 100 |
f.write(config_content) |
| 101 |
f.close() |
| 102 |
env = os.environ.copy() |
| 103 |
env["PROMPT_TOOLKIT_NO_CPR"] = "1" |
| 104 |
env["TERM"] = "xterm-256color" |
| 105 |
|
| 106 |
child = pexpect.spawn( |
| 107 |
f"iredis -n 15 --iredisrc {f.name}", |
| 108 |
timeout=TIMEOUT, |
| 109 |
env=env, |
| 110 |
dimensions=(40, 120), |
| 111 |
) |
| 112 |
child.logfile_read = open("cli_test.log", "ab") |
| 113 |
child.expect(["https://github.com/laixintao/iredis/issues", "127.0.0.1"]) |
| 114 |
yield child |
| 115 |
child.close() |
| 116 |
|
| 117 |
|
| 118 |
@pytest.fixture(scope="function") |
| 119 |
def raw_cli(): |
| 120 |
"""Open iredis subprocess to test""" |
| 121 |
TEST_IREDISRC = "/tmp/.iredisrc.test" |
| 122 |
config_content = dedent( |
| 123 |
""" |
| 124 |
[main] |
| 125 |
log_location = |
| 126 |
warning = True |
| 127 |
""" |
| 128 |
) |
| 129 |
|
| 130 |
with open(TEST_IREDISRC, "w+") as test_iredisrc: |
| 131 |
test_iredisrc.write(config_content) |
| 132 |
|
| 133 |
child = pexpect.spawn( |
| 134 |
f"iredis --raw -n 15 --iredisrc {TEST_IREDISRC}", timeout=TIMEOUT |
| 135 |
) |
| 136 |
child.logfile_read = open("cli_test.log", "ab") |
| 137 |
child.expect(["https://github.com/laixintao/iredis/issues", "127.0.0.1"]) |
| 138 |
yield child |
| 139 |
child.close() |
| 140 |
|
| 141 |
|
| 142 |
@pytest.fixture(scope="function") |
| 143 |
def cli_without_warning(): |
| 144 |
f = tempfile.TemporaryFile("w") |
| 145 |
config_content = dedent( |
| 146 |
""" |
| 147 |
[main] |
| 148 |
log_location = /tmp/iredis1.log |
| 149 |
warning = False |
| 150 |
""" |
| 151 |
) |
| 152 |
f.write(config_content) |
| 153 |
f.close() |
| 154 |
|
| 155 |
cli = pexpect.spawn(f"iredis -n 15 --iredisrc {f.name}", timeout=1) |
| 156 |
cli.logfile_read = open("cli_test.log", "ab") |
| 157 |
yield cli |
| 158 |
cli.close() |
| 159 |
os.remove("/tmp/iredisrc") |