summaryrefslogtreecommitdiffstats
path: root/tests/unittests/command_parse/test_generic_parse.py
blob: f41d8879b3bbe3f37caf97581a9a1bf1d7d9eb23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def test_del(judge_command):
    judge_command("DEL abc", {"command": "DEL", "keys": "abc"})
    judge_command("DEL bc1", {"command": "DEL", "keys": "bc1"})
    judge_command("DEL 1", {"command": "DEL", "keys": "1"})
    judge_command('DEL "hello world"', {"command": "DEL", "keys": '"hello world"'})
    judge_command(
        r'DEL "hell\"o world"', {"command": "DEL", "keys": r'"hell\"o world"'}
    )
    judge_command("DEL abc def", {"command": "DEL", "keys": "abc def"})
    judge_command("DEL 1 2", {"command": "DEL", "keys": "1 2"})
    judge_command("DEL 'he \"llo'", {"command": "DEL", "keys": "'he \"llo'"})
    judge_command(
        """DEL 'he "llo' "abc" 'def' """,
        {"command": "DEL", "keys": "'he \"llo' \"abc\" 'def'"},
    )


def test_exists(judge_command):
    judge_command("EXISTS foo bar", {"command": "EXISTS", "keys": "foo bar"})
    judge_command("EXISTS foo", {"command": "EXISTS", "keys": "foo"})
    judge_command("EXISTS 1", {"command": "EXISTS", "keys": "1"})
    judge_command('EXISTS "foo bar"', {"command": "EXISTS", "keys": '"foo bar"'})
    judge_command(r'EXISTS "\""', {"command": "EXISTS", "keys": r'"\""'})


def test_expire(judge_command):
    judge_command("EXPIRE key 12", {"command": "EXPIRE", "key": "key", "second": "12"})
    judge_command("EXPIRE key a12", None)
    judge_command("EXPIRE 12 12", {"command": "EXPIRE", "key": "12", "second": "12"})
    judge_command("EXPIRE 12", None)


def test_expireat(judge_command):
    judge_command(
        "EXPIRE key 1565787643",
        {"command": "EXPIRE", "key": "key", "second": "1565787643"},
    )
    judge_command("EXPIRE key a12", None)


def test_keys(judge_command):
    judge_command("KEYS *", {"command": "KEYS", "pattern": "*"})
    judge_command("KEYS *abc", {"command": "KEYS", "pattern": "*abc"})
    judge_command("keys abc*", {"command": "keys", "pattern": "abc*"})


def test_move(judge_command):
    judge_command("MOVE key 14", {"command": "MOVE", "key": "key", "index": "14"})


def test_pexpire(judge_command):
    judge_command(
        "PEXPIRE key 12", {"command": "PEXPIRE", "key": "key", "millisecond": "12"}
    )
    judge_command("PEXPIRE key a12", None)
    judge_command(
        "PEXPIRE 12 12", {"command": "PEXPIRE", "key": "12", "millisecond": "12"}
    )
    judge_command("PEXPIRE 12", None)


def test_pexpireat(judge_command):
    judge_command(
        "PEXPIREAT key 1565787643",
        {"command": "PEXPIREAT", "key": "key", "timestampms": "1565787643"},
    )
    judge_command("PEXPIREAT key a12", None)


def test_rename(judge_command):
    judge_command(
        "rename key newkey", {"command": "rename", "key": "key", "newkey": "newkey"}
    )
    judge_command(
        "rename 123 newkey", {"command": "rename", "key": "123", "newkey": "newkey"}
    )
    judge_command("rename 123 ", None)


def test_scan(judge_command):
    judge_command(
        "SCAN 0 MATCH task* COUNT 15 TYPE string",
        {
            "command": "SCAN",
            "cursor": "0",
            "match": "MATCH",
            "pattern": "task*",
            "count_const": "COUNT",
            "count": "15",
            "type_const": "TYPE",
            "type": "string",
        },
    )
    judge_command("SCAN 0", {"command": "SCAN", "cursor": "0"})
    judge_command(
        "SCAN 0 MATCH task*",
        {"command": "SCAN", "cursor": "0", "match": "MATCH", "pattern": "task*"},
    )
    judge_command(
        "SCAN 0 COUNT 15 TYPE string",
        {
            "command": "SCAN",
            "cursor": "0",
            "count_const": "COUNT",
            "count": "15",
            "type_const": "TYPE",
            "type": "string",
        },
    )


def test_migrate(judge_command):
    judge_command(
        'MIGRATE 192.168.1.34 6379 " " 0 5000 KEYS key1 key2 key3',
        {
            "command": "MIGRATE",
            "host": "192.168.1.34",
            "port": "6379",
            "key": '" "',
            "index": "0",
            "timeout": "5000",
            "const_keys": "KEYS",
            "keys": "key1 key2 key3",
        },
    )
    judge_command(
        "MIGRATE 192.168.1.34 6379 foo 0 5000 auth password1 KEYS key1 key2 key3",
        {
            "command": "MIGRATE",
            "host": "192.168.1.34",
            "port": "6379",
            "key": "foo",
            "index": "0",
            "timeout": "5000",
            "const_keys": "KEYS",
            "keys": "key1 key2 key3",
            "auth": "auth",
            "password": "password1",
        },
    )
    judge_command(
        "MIGRATE 192.168.1.34 6379 foo 0 5000 auth username1 password1 KEYS key1 key2 key3",
        {
            "command": "MIGRATE",
            "host": "192.168.1.34",
            "port": "6379",
            "key": "foo",
            "index": "0",
            "timeout": "5000",
            "const_keys": "KEYS",
            "keys": "key1 key2 key3",
            "auth": "auth",
            "password": "password1",
            "username": "username1",
        },
    )


def test_object(judge_command):
    judge_command(
        "object refcount mylist",
        {"command": "object", "object": "refcount", "key": "mylist"},
    )


def test_wait(judge_command):
    judge_command("WAIT 3 100", {"command": "WAIT", "count": "3", "timeout": "100"})


def test_restore(judge_command):
    judge_command(
        'RESTORE mykey 0 "\n\x17\x17\x00\x00\x00\x12\x00\x00\x00\x03\x00\x00\xc0\x01\x00\x04\xc0\x02\x00\x04\xc0\x03\x00\xff\x04\x00u#<\xc0;.\xe9\xdd"',  # noqa
        {
            "command": "RESTORE",
            "key": "mykey",
            "timeout": "0",
            "value": '"\n\x17\x17\x00\x00\x00\x12\x00\x00\x00\x03\x00\x00\xc0\x01\x00\x04\xc0\x02\x00\x04\xc0\x03\x00\xff\x04\x00u#<\xc0;.\xe9\xdd"',  # noqa
        },
    )