summaryrefslogtreecommitdiffstats
path: root/tests/unittests/command_parse/test_string_parse.py
blob: df56b253c152e22e7556a692861216c1fbcf4646 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
def test_set(judge_command):
    judge_command("SET abc bar", {"command": "SET", "key": "abc", "value": "bar"})
    judge_command(
        "SET abc bar EX 10",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "expiration": "EX",
            "millisecond": "10",
        },
    )
    judge_command(
        "SET abc bar px 10000",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "expiration": "px",
            "millisecond": "10000",
        },
    )
    judge_command(
        "SET abc bar px 10000 nx",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "expiration": "px",
            "millisecond": "10000",
            "condition": "nx",
        },
    )
    judge_command(
        "SET abc bar px 10000 XX",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "expiration": "px",
            "millisecond": "10000",
            "condition": "XX",
        },
    )
    judge_command(
        "SET abc bar XX px 10000",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "expiration": "px",
            "millisecond": "10000",
            "condition": "XX",
        },
    )
    judge_command(
        "SET abc bar XX",
        {"command": "SET", "key": "abc", "value": "bar", "condition": "XX"},
    )
    # keepttl
    judge_command(
        "SET abc bar XX keepttl",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "condition": "XX",
            "keepttl": "keepttl",
        },
    )
    judge_command(
        "SET abc bar keepttl XX",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "condition": "XX",
            "keepttl": "keepttl",
        },
    )
    judge_command(
        "SET abc bar XX px 10000 KEEPTTL",
        {
            "command": "SET",
            "key": "abc",
            "value": "bar",
            "expiration": "px",
            "millisecond": "10000",
            "condition": "XX",
            "keepttl": "KEEPTTL",
        },
    )


def test_append(judge_command):
    judge_command("append foo bar", {"command": "append", "key": "foo", "value": "bar"})
    judge_command(
        "APPEND foo 'bar'", {"command": "APPEND", "key": "foo", "value": "'bar'"}
    )
    judge_command("APPEND foo", None)


def test_bitcount(judge_command):
    judge_command("bitcount foo", {"command": "bitcount", "key": "foo"})
    judge_command(
        "bitcount foo 1 5",
        {"command": "bitcount", "key": "foo", "start": "1", "end": "5"},
    )
    judge_command(
        "bitcount foo 1 -5",
        {"command": "bitcount", "key": "foo", "start": "1", "end": "-5"},
    )
    judge_command(
        "bitcount foo -2 -1",
        {"command": "bitcount", "key": "foo", "start": "-2", "end": "-1"},
    )
    judge_command("bitcount foo -2", None)


def test_getrange(judge_command):
    judge_command("getrange foo", None)
    judge_command(
        "getrange foo 1 5",
        {"command": "getrange", "key": "foo", "start": "1", "end": "5"},
    )
    judge_command(
        "getrange foo 1 -5",
        {"command": "getrange", "key": "foo", "start": "1", "end": "-5"},
    )
    judge_command(
        "getrange foo -2 -1",
        {"command": "getrange", "key": "foo", "start": "-2", "end": "-1"},
    )
    judge_command("getrange foo -2", None)


def test_get_set(judge_command):
    judge_command("GETSET abc bar", {"command": "GETSET", "key": "abc", "value": "bar"})


def test_incr(judge_command):
    judge_command("INCR foo", {"command": "INCR", "key": "foo"})
    judge_command("INCR", None)
    judge_command("INCR foo 1", None)


def test_incr_by(judge_command):
    judge_command("INCRBY foo", None)
    judge_command("INCRBY", None)
    judge_command("INCRBY foo 1", {"command": "INCRBY", "key": "foo", "delta": "1"})
    judge_command("INCRBY foo 200", {"command": "INCRBY", "key": "foo", "delta": "200"})
    judge_command("INCRBY foo -21", {"command": "INCRBY", "key": "foo", "delta": "-21"})


def test_decr(judge_command):
    judge_command("DECR foo", {"command": "DECR", "key": "foo"})
    judge_command("DECR", None)
    judge_command("DECR foo 1", None)


def test_decr_by(judge_command):
    judge_command("DECRBY foo", None)
    judge_command("DECRBY", None)
    judge_command("DECRBY foo 1", {"command": "DECRBY", "key": "foo", "delta": "1"})
    judge_command("DECRBY foo 200", {"command": "DECRBY", "key": "foo", "delta": "200"})
    judge_command("DECRBY foo -21", {"command": "DECRBY", "key": "foo", "delta": "-21"})


def test_command_set_range(judge_command):
    judge_command(
        "SETRANGE foo 10 bar",
        {"command": "SETRANGE", "key": "foo", "offset": "10", "value": "bar"},
    )
    judge_command("SETRANGE foo bar", None)
    judge_command(
        "SETRANGE Redis 10 'hello world'",
        {
            "command": "SETRANGE",
            "key": "Redis",
            "offset": "10",
            "value": "'hello world'",
        },
    )


def test_command_set_ex(judge_command):
    judge_command(
        "SETEX key 10 value",
        {"command": "SETEX", "key": "key", "second": "10", "value": "value"},
    )
    judge_command("SETEX foo 10", None)
    judge_command(
        "setex Redis 10 'hello world'",
        {"command": "setex", "key": "Redis", "second": "10", "value": "'hello world'"},
    )


def test_command_setbit(judge_command):
    judge_command(
        "SETBIT key 10 0",
        {"command": "SETBIT", "key": "key", "offset": "10", "bit": "0"},
    )
    judge_command(
        "SETBIT foo 10 1",
        {"command": "SETBIT", "key": "foo", "offset": "10", "bit": "1"},
    )
    judge_command("SETBIT foo 10 10", None)
    judge_command("SETBIT foo 10 abc", None)
    judge_command("SETBIT foo 10", None)
    judge_command("SETBIT foo", None)


def test_command_getbit(judge_command):
    judge_command("GETBIT key 10", {"command": "GETBIT", "key": "key", "offset": "10"})
    judge_command("GETBIT foo 0", {"command": "GETBIT", "key": "foo", "offset": "0"})
    judge_command("GETBIT foo -1", None)
    judge_command("SETBIT foo abc", None)
    judge_command("SETBIT foo", None)


def test_command_incrbyfloat(judge_command):
    judge_command("INCRBYFLOAT key", None)
    judge_command(
        "INCRBYFLOAT key 1.1", {"command": "INCRBYFLOAT", "key": "key", "float": "1.1"}
    )
    judge_command(
        "INCRBYFLOAT key .1", {"command": "INCRBYFLOAT", "key": "key", "float": ".1"}
    )
    judge_command(
        "INCRBYFLOAT key 1.", {"command": "INCRBYFLOAT", "key": "key", "float": "1."}
    )
    judge_command(
        "INCRBYFLOAT key 5.0e3",
        {"command": "INCRBYFLOAT", "key": "key", "float": "5.0e3"},
    )
    judge_command(
        "INCRBYFLOAT key -5.0e3",
        {"command": "INCRBYFLOAT", "key": "key", "float": "-5.0e3"},
    )


def test_command_mget(judge_command):
    judge_command("mget foo bar", {"command": "mget", "keys": "foo bar"})


def test_mset(judge_command):
    judge_command("mset foo bar", {"command": "mset", "key": "foo", "value": "bar"})
    judge_command(
        "mset foo bar hello world",
        {"command": "mset", "key": "hello", "value": "world"},
    )


def test_psetex(judge_command):
    judge_command(
        "psetex foo 1000 bar",
        {"command": "psetex", "key": "foo", "value": "bar", "millisecond": "1000"},
    )
    judge_command("psetex foo bar", None)


def test_bitop(judge_command):
    judge_command(
        "BITOP AND dest key1 key2",
        {"command": "BITOP", "operation": "AND", "key": "dest", "keys": "key1 key2"},
    )
    judge_command(
        "BITOP AND dest key1",
        {"command": "BITOP", "operation": "AND", "key": "dest", "keys": "key1"},
    )
    judge_command("BITOP AND dest", None)


def test_bitpos(judge_command):
    judge_command(
        "BITPOS mykey 1 3 5",
        {"command": "BITPOS", "key": "mykey", "bit": "1", "start": "3", "end": "5"},
    )
    judge_command("BITPOS mykey 1", {"command": "BITPOS", "key": "mykey", "bit": "1"})
    judge_command(
        "BITPOS mykey 1 3",
        {"command": "BITPOS", "key": "mykey", "bit": "1", "start": "3"},
    )


def test_bitfield(judge_command):
    judge_command(
        "BITFIELD mykey INCRBY i5 100 1 GET u4 0",
        {
            "command": "BITFIELD",
            "key": "mykey",
            "incrby": "INCRBY",
            "inttype": ["i5", "u4"],
            "offset": ["100", "0"],
            "value": "1",
            "get": "GET",
        },
    )
    judge_command(
        "BITFIELD mystring SET i8 #0 100",
        {
            "command": "BITFIELD",
            "key": "mystring",
            "set": "SET",
            "inttype": "i8",
            "offset": "#0",
            "value": "100",
        },
    )
    judge_command(
        "BITFIELD mykey incrby u2 100 1 OVERFLOW SAT incrby u2 102 1",
        {
            "command": "BITFIELD",
            "key": "mykey",
            "incrby": "incrby",
            "inttype": "u2",
            "offset": "102",
            "value": "1",
            "overflow": "OVERFLOW",
            "overflow_option": "SAT",
        },
    )


def test_stralgo(judge_command):
    judge_command(
        "STRALGO LCS STRINGS ohmytext mynewtext",
        {
            "command": "STRALGO",
            "str_algo": "LCS",
            "strings_const": "STRINGS",
            "values": "ohmytext mynewtext",
        },
    )

    # Due to redis' command design, this can't be fix in any ways.
    judge_command(
        "STRALGO LCS STRINGS ohmytext mynewtext LEN",
        {
            "command": "STRALGO",
            "str_algo": "LCS",
            "strings_const": "STRINGS",
            "values": "ohmytext mynewtext LEN",
        },
    )