summaryrefslogtreecommitdiffstats
path: root/tests/unittests/test_completers.py
blob: d0344fd2e0148ef08fef0c7a05cd31b79ea35f95 (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
346
347
348
349
350
351
352
353
354
355
356
from unittest.mock import MagicMock, patch

import pendulum
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.completion import Completion

from iredis.completers import MostRecentlyUsedFirstWordCompleter
from iredis.completers import IRedisCompleter, TimestampCompleter, IntegerTypeCompleter


def test_LUF_completer_touch():
    c = MostRecentlyUsedFirstWordCompleter(3, ["one", "two"])
    c.touch("hello")
    assert c.words == ["hello", "one", "two"]

    c.touch("foo")
    assert c.words == ["foo", "hello", "one"]

    c.touch("hello")
    assert c.words == ["hello", "foo", "one"]


def test_LUF_completer_touch_words():
    c = MostRecentlyUsedFirstWordCompleter(3, [])
    c.touch_words(["hello", "world", "foo", "bar"])
    assert c.words == ["bar", "foo", "world"]

    c.touch_words(["one", "two"])
    assert c.words == ["two", "one", "bar"]


def test_newbie_mode_complete_without_meta_dict():
    fake_document = MagicMock()
    fake_document.text_before_cursor = fake_document.text = "GEOR"
    completer = IRedisCompleter(hint=False)
    completions = list(completer.get_completions(fake_document, None))
    assert [word.text for word in completions] == ["GEORADIUS", "GEORADIUSBYMEMBER"]
    assert [word.display_meta for word in completions] == [
        FormattedText([("", "")]),
        FormattedText([("", "")]),
    ]


def test_newbie_mode_complete_with_meta_dict():
    fake_document = MagicMock()
    fake_document.text_before_cursor = fake_document.text = "GEOR"
    completer = IRedisCompleter(hint=True)
    completions = list(completer.get_completions(fake_document, None))

    assert sorted([completion.display_meta for completion in completions]) == [
        FormattedText(
            [
                (
                    "",
                    "Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member",  # noqa
                )
            ]
        ),
        FormattedText(
            [
                (
                    "",
                    "Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point",  # noqa
                )
            ]
        ),
    ]


def test_newbie_mode_complete_with_meta_dict_command_is_lowercase():
    fake_document = MagicMock()
    fake_document.text_before_cursor = fake_document.text = "geor"
    completer = IRedisCompleter(hint=True)
    completions = list(completer.get_completions(fake_document, None))

    assert sorted([completion.display_meta for completion in completions]) == [
        FormattedText(
            [
                (
                    "",
                    "Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member",  # noqa
                )
            ]
        ),
        FormattedText(
            [
                (
                    "",
                    "Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point",  # noqa
                )
            ]
        ),
    ]


def test_iredis_completer_update_for_response():
    c = IRedisCompleter()
    c.update_completer_for_response(
        "HGETALL",
        (),
        [
            b"Behave",
            b"misbehave",
            b"Interpret",
            b"misinterpret",
            b"Lead",
            b"mislead",
            b"Trust",
            b"mistrust",
        ],
    )
    assert c.field_completer.words == ["Trust", "Lead", "Interpret", "Behave"]


def test_categoryname_completer_update_for_response():
    c = IRedisCompleter()
    c.update_completer_for_response(
        "ACL CAT",
        (),
        [b"scripting", b"watch"],
    )
    assert sorted(c.catetoryname_completer.words) == ["scripting", "watch"]
    c.update_completer_for_response(
        "ACL CAT",
        ("scripting"),
        [b"foo", b"bar"],
    )
    assert sorted(c.catetoryname_completer.words) == ["scripting", "watch"]


def test_completer_when_there_are_spaces_in_command():
    c = IRedisCompleter()
    c.update_completer_for_response(
        "ACL    cat",
        (),
        [b"scripting", b"watch"],
    )
    assert sorted(c.catetoryname_completer.words) == ["scripting", "watch"]

    c.update_completer_for_response(
        "acl \t   cat",
        (),
        [b"hello", b"world"],
    )
    assert sorted(c.catetoryname_completer.words) == [
        "hello",
        "scripting",
        "watch",
        "world",
    ]


def test_iredis_completer_no_exception_for_none_response():
    c = IRedisCompleter()
    c.update_completer_for_response("XPENDING", None, None)
    c.update_completer_for_response("KEYS", None, None)


def test_group_completer():
    fake_document = MagicMock()
    previous_commands = ["xgroup create abc world 123", "xgroup setid abc hello 123"]
    fake_document.text = fake_document.text_before_cursor = "XGROUP DESTROY key "
    completer = IRedisCompleter()
    for command in previous_commands:
        completer.update_completer_for_input(command)
    completions = list(completer.get_completions(fake_document, None))
    assert completions == [
        Completion(
            text="hello",
            start_position=0,
            display=FormattedText([("", "hello")]),
            display_meta=FormattedText([("", "")]),
        ),
        Completion(
            text="world",
            start_position=0,
            display=FormattedText([("", "world")]),
            display_meta=FormattedText([("", "")]),
        ),
    ]


@patch("iredis.completers.pendulum.now")
def test_timestamp_completer_humanize_time_completion(fake_now):
    fake_now.return_value = pendulum.from_timestamp(1578487013)
    c = TimestampCompleter()

    fake_document = MagicMock()
    fake_document.text = fake_document.text_before_cursor = "30"
    completions = list(c.get_completions(fake_document, None))

    assert completions == [
        Completion(
            text="1575895013000",
            start_position=-2,
            display=FormattedText([("", "1575895013000")]),
            display_meta="30 days ago (2019-12-09 12:36:53)",
        ),
        Completion(
            text="1578379013000",
            start_position=-2,
            display=FormattedText([("", "1578379013000")]),
            display_meta="30 hours ago (2020-01-07 06:36:53)",
        ),
        Completion(
            text="1578485213000",
            start_position=-2,
            display=FormattedText([("", "1578485213000")]),
            display_meta="30 minutes ago (2020-01-08 12:06:53)",
        ),
        Completion(
            text="1578486983000",
            start_position=-2,
            display=FormattedText([("", "1578486983000")]),
            display_meta="30 seconds ago (2020-01-08 12:36:23)",
        ),
    ]

    # No plural
    fake_document.text = fake_document.text_before_cursor = "1"
    completions = list(c.get_completions(fake_document, None))

    assert completions == [
        Completion(
            text="1546951013000",
            start_position=-1,
            display=FormattedText([("", "1546951013000")]),
            display_meta="1 year ago (2019-01-08 12:36:53)",
        ),
        Completion(
            text="1575808613000",
            start_position=-1,
            display=FormattedText([("", "1575808613000")]),
            display_meta="1 month ago (2019-12-08 12:36:53)",
        ),
        Completion(
            text="1578400613000",
            start_position=-1,
            display=FormattedText([("", "1578400613000")]),
            display_meta="1 day ago (2020-01-07 12:36:53)",
        ),
        Completion(
            text="1578483413000",
            start_position=-1,
            display=FormattedText([("", "1578483413000")]),
            display_meta="1 hour ago (2020-01-08 11:36:53)",
        ),
        Completion(
            text="1578486953000",
            start_position=-1,
            display=FormattedText([("", "1578486953000")]),
            display_meta="1 minute ago (2020-01-08 12:35:53)",
        ),
        Completion(
            text="1578487012000",
            start_position=-1,
            display=FormattedText([("", "1578487012000")]),
            display_meta="1 second ago (2020-01-08 12:36:52)",
        ),
    ]


def test_timestamp_completer_datetime_format_time_completion():
    c = TimestampCompleter()
    fake_document = MagicMock()
    fake_document.text = fake_document.text_before_cursor = "2020-02-07"
    completions = list(c.get_completions(fake_document, None))

    assert completions == [
        Completion(
            text="1581033600000",
            start_position=-10,
            display=FormattedText([("", "1581033600000")]),
            display_meta="2020-02-07T00:00:00+00:00",
        )
    ]


def test_integer_type_completer():
    c = IntegerTypeCompleter()
    fake_document = MagicMock()
    fake_document.text = fake_document.get_word_before_cursor.return_value = "i"
    completions = list(c.get_completions(fake_document, None))
    assert len(completions) == 64

    fake_document.text = fake_document.get_word_before_cursor.return_value = "u"
    completions = list(c.get_completions(fake_document, None))
    assert len(completions) == 63

    c.touch("u4")
    assert list(c.get_completions(fake_document, None))[0].text == "u4"


def test_completion_casing():
    c = IRedisCompleter(completion_casing="auto")
    fake_document = MagicMock()
    fake_document.text = fake_document.text_before_cursor = "ge"
    assert [
        completion.text for completion in c.get_completions(fake_document, None)
    ] == [
        "get",
        "getset",
        "getbit",
        "geopos",
        "geoadd",
        "geohash",
        "geodist",
        "getrange",
        "georadius",
        "georadiusbymember",
    ]

    c = IRedisCompleter(completion_casing="auto")
    fake_document.text = fake_document.text_before_cursor = "GET"
    assert [
        completion.text for completion in c.get_completions(fake_document, None)
    ] == ["GET", "GETSET", "GETBIT", "GETRANGE"]

    c = IRedisCompleter(completion_casing="upper")
    fake_document.text = fake_document.text_before_cursor = "get"
    assert [
        completion.text for completion in c.get_completions(fake_document, None)
    ] == ["GET", "GETSET", "GETBIT", "GETRANGE"]

    c = IRedisCompleter(completion_casing="lower")
    fake_document.text = fake_document.text_before_cursor = "GET"
    assert [
        completion.text for completion in c.get_completions(fake_document, None)
    ] == ["get", "getset", "getbit", "getrange"]


def test_username_completer():
    completer = IRedisCompleter()
    completer.update_completer_for_input("acl deluser laixintao")
    completer.update_completer_for_input("acl deluser antirez")

    fake_document = MagicMock()
    fake_document.text_before_cursor = fake_document.text = "acl deluser "
    completions = list(completer.get_completions(fake_document, None))
    assert sorted([completion.text for completion in completions]) == [
        "antirez",
        "laixintao",
    ]


def test_username_touch_for_response():
    c = IRedisCompleter()
    c.update_completer_for_response(
        "acl   users",
        (),
        [b"hello", b"world"],
    )
    assert sorted(c.username_completer.words) == [
        "hello",
        "world",
    ]