Debian Codegraph @

tests/unittests/test_completers.py

raw
1 from unittest.mock import MagicMock
2
3 from freezegun import freeze_time
4 from prompt_toolkit.formatted_text import FormattedText
5 from prompt_toolkit.completion import Completion
6
7 from iredis.completers import MostRecentlyUsedFirstWordCompleter
8 from iredis.completers import IRedisCompleter, TimestampCompleter, IntegerTypeCompleter
9
10
11 def test_LUF_completer_touch():
12 c = MostRecentlyUsedFirstWordCompleter(3, ["one", "two"])
13 c.touch("hello")
14 assert c.words == ["hello", "one", "two"]
15
16 c.touch("foo")
17 assert c.words == ["foo", "hello", "one"]
18
19 c.touch("hello")
20 assert c.words == ["hello", "foo", "one"]
21
22
23 def test_LUF_completer_touch_words():
24 c = MostRecentlyUsedFirstWordCompleter(3, [])
25 c.touch_words(["hello", "world", "foo", "bar"])
26 assert c.words == ["bar", "foo", "world"]
27
28 c.touch_words(["one", "two"])
29 assert c.words == ["two", "one", "bar"]
30
31
32 def test_newbie_mode_complete_without_meta_dict():
33 fake_document = MagicMock()
34 fake_document.text_before_cursor = fake_document.text = "GEOR"
35 completer = IRedisCompleter(hint=False)
36 completions = list(completer.get_completions(fake_document, None))
37 assert [word.text for word in completions] == [
38 "GEORADIUS",
39 "GEORADIUS_RO",
40 "GEORADIUSBYMEMBER",
41 "GEORADIUSBYMEMBER_RO",
42 ]
43 assert [word.display_meta for word in completions] == [
44 FormattedText([("", "")]),
45 FormattedText([("", "")]),
46 FormattedText([("", "")]),
47 FormattedText([("", "")]),
48 ]
49
50
51 def test_newbie_mode_complete_with_meta_dict():
52 fake_document = MagicMock()
53 fake_document.text_before_cursor = fake_document.text = "GEOR"
54 completer = IRedisCompleter(hint=True)
55 completions = list(completer.get_completions(fake_document, None))
56
57 assert sorted([completion.display_meta for completion in completions]) == [
58 FormattedText(
59 [
60 (
61 "",
62 "Queries a geospatial index for members within a distance from a coordinate, optionally stores the result.", # noqa
63 )
64 ]
65 ),
66 FormattedText(
67 [
68 (
69 "",
70 "Queries a geospatial index for members within a distance from a member, optionally stores the result.", # noqa
71 )
72 ]
73 ),
74 FormattedText(
75 [
76 (
77 "",
78 "Returns members from a geospatial index that are within a distance from a coordinate.", # noqa
79 )
80 ]
81 ),
82 FormattedText(
83 [
84 (
85 "",
86 "Returns members from a geospatial index that are within a distance from a member.", # noqa
87 )
88 ]
89 ),
90 ]
91
92
93 def test_newbie_mode_complete_with_meta_dict_command_is_lowercase():
94 fake_document = MagicMock()
95 fake_document.text_before_cursor = fake_document.text = "geor"
96 completer = IRedisCompleter(hint=True)
97 completions = list(completer.get_completions(fake_document, None))
98
99 assert sorted([completion.display_meta for completion in completions]) == [
100 FormattedText(
101 [
102 (
103 "",
104 "Queries a geospatial index for members within a distance from a coordinate, optionally stores the result.", # noqa
105 )
106 ]
107 ),
108 FormattedText(
109 [
110 (
111 "",
112 "Queries a geospatial index for members within a distance from a member, optionally stores the result.", # noqa
113 )
114 ]
115 ),
116 FormattedText(
117 [
118 (
119 "",
120 "Returns members from a geospatial index that are within a distance from a coordinate.", # noqa
121 )
122 ]
123 ),
124 FormattedText(
125 [
126 (
127 "",
128 "Returns members from a geospatial index that are within a distance from a member.", # noqa
129 )
130 ]
131 ),
132 ]
133
134
135 def test_iredis_completer_update_for_response():
136 c = IRedisCompleter()
137 c.update_completer_for_response(
138 "HGETALL",
139 (),
140 [
141 b"Behave",
142 b"misbehave",
143 b"Interpret",
144 b"misinterpret",
145 b"Lead",
146 b"mislead",
147 b"Trust",
148 b"mistrust",
149 ],
150 )
151 assert c.field_completer.words == ["Trust", "Lead", "Interpret", "Behave"]
152
153
154 def test_categoryname_completer_update_for_response():
155 c = IRedisCompleter()
156 c.update_completer_for_response(
157 "ACL CAT",
158 (),
159 [b"scripting", b"watch"],
160 )
161 assert sorted(c.catetoryname_completer.words) == ["scripting", "watch"]
162 c.update_completer_for_response(
163 "ACL CAT",
164 ("scripting"),
165 [b"foo", b"bar"],
166 )
167 assert sorted(c.catetoryname_completer.words) == ["scripting", "watch"]
168
169
170 def test_completer_when_there_are_spaces_in_command():
171 c = IRedisCompleter()
172 c.update_completer_for_response(
173 "ACL cat",
174 (),
175 [b"scripting", b"watch"],
176 )
177 assert sorted(c.catetoryname_completer.words) == ["scripting", "watch"]
178
179 c.update_completer_for_response(
180 "acl \t cat",
181 (),
182 [b"hello", b"world"],
183 )
184 assert sorted(c.catetoryname_completer.words) == [
185 "hello",
186 "scripting",
187 "watch",
188 "world",
189 ]
190
191
192 def test_iredis_completer_no_exception_for_none_response():
193 c = IRedisCompleter()
194 c.update_completer_for_response("XPENDING", None, None)
195 c.update_completer_for_response("KEYS", None, None)
196
197
198 def test_group_completer():
199 fake_document = MagicMock()
200 previous_commands = ["xgroup create abc world 123", "xgroup setid abc hello 123"]
201 fake_document.text = fake_document.text_before_cursor = "XGROUP DESTROY key "
202 completer = IRedisCompleter()
203 for command in previous_commands:
204 completer.update_completer_for_input(command)
205 completions = list(completer.get_completions(fake_document, None))
206 assert completions == [
207 Completion(
208 text="hello",
209 start_position=0,
210 display=FormattedText([("", "hello")]),
211 display_meta=FormattedText([("", "")]),
212 ),
213 Completion(
214 text="world",
215 start_position=0,
216 display=FormattedText([("", "world")]),
217 display_meta=FormattedText([("", "")]),
218 ),
219 ]
220
221
222 @freeze_time("2020-01-08 12:36:53")
223 def test_timestamp_completer_humanize_time_completion():
224 c = TimestampCompleter(is_milliseconds=True, future_time=False)
225
226 fake_document = MagicMock()
227 fake_document.text = fake_document.text_before_cursor = "30"
228 completions = list(c.get_completions(fake_document, None))
229
230 assert completions == [
231 Completion(
232 text="1575895013000",
233 start_position=-2,
234 display=FormattedText([("", "1575895013000")]),
235 display_meta="30 days ago (2019-12-09 12:36:53)",
236 ),
237 Completion(
238 text="1578379013000",
239 start_position=-2,
240 display=FormattedText([("", "1578379013000")]),
241 display_meta="30 hours ago (2020-01-07 06:36:53)",
242 ),
243 Completion(
244 text="1578485213000",
245 start_position=-2,
246 display=FormattedText([("", "1578485213000")]),
247 display_meta="30 minutes ago (2020-01-08 12:06:53)",
248 ),
249 Completion(
250 text="1578486983000",
251 start_position=-2,
252 display=FormattedText([("", "1578486983000")]),
253 display_meta="30 seconds ago (2020-01-08 12:36:23)",
254 ),
255 ]
256
257 # No plural
258 fake_document.text = fake_document.text_before_cursor = "1"
259 completions = list(c.get_completions(fake_document, None))
260
261 assert completions == [
262 Completion(
263 text="1546951013000",
264 start_position=-1,
265 display=FormattedText([("", "1546951013000")]),
266 display_meta="1 year ago (2019-01-08 12:36:53)",
267 ),
268 Completion(
269 text="1575808613000",
270 start_position=-1,
271 display=FormattedText([("", "1575808613000")]),
272 display_meta="1 month ago (2019-12-08 12:36:53)",
273 ),
274 Completion(
275 text="1578400613000",
276 start_position=-1,
277 display=FormattedText([("", "1578400613000")]),
278 display_meta="1 day ago (2020-01-07 12:36:53)",
279 ),
280 Completion(
281 text="1578483413000",
282 start_position=-1,
283 display=FormattedText([("", "1578483413000")]),
284 display_meta="1 hour ago (2020-01-08 11:36:53)",
285 ),
286 Completion(
287 text="1578486953000",
288 start_position=-1,
289 display=FormattedText([("", "1578486953000")]),
290 display_meta="1 minute ago (2020-01-08 12:35:53)",
291 ),
292 Completion(
293 text="1578487012000",
294 start_position=-1,
295 display=FormattedText([("", "1578487012000")]),
296 display_meta="1 second ago (2020-01-08 12:36:52)",
297 ),
298 ]
299
300
301 @freeze_time("2020-01-08 12:36:53")
302 def test_timestamp_completer_humanize_time_completion_seconds():
303 c = TimestampCompleter(is_milliseconds=False, future_time=False)
304
305 fake_document = MagicMock()
306 fake_document.text = fake_document.text_before_cursor = "30"
307 completions = list(c.get_completions(fake_document, None))
308
309 assert completions == [
310 Completion(
311 text="1575895013",
312 start_position=-2,
313 display=FormattedText([("", "1575895013")]),
314 display_meta="30 days ago (2019-12-09 12:36:53)",
315 ),
316 Completion(
317 text="1578379013",
318 start_position=-2,
319 display=FormattedText([("", "1578379013")]),
320 display_meta="30 hours ago (2020-01-07 06:36:53)",
321 ),
322 Completion(
323 text="1578485213",
324 start_position=-2,
325 display=FormattedText([("", "1578485213")]),
326 display_meta="30 minutes ago (2020-01-08 12:06:53)",
327 ),
328 Completion(
329 text="1578486983",
330 start_position=-2,
331 display=FormattedText([("", "1578486983")]),
332 display_meta="30 seconds ago (2020-01-08 12:36:23)",
333 ),
334 ]
335
336
337 @freeze_time("2020-01-08 12:36:53")
338 def test_timestamp_completer_humanize_time_completion_seconds_future_time():
339 c = TimestampCompleter(is_milliseconds=False, future_time=True)
340
341 fake_document = MagicMock()
342 fake_document.text = fake_document.text_before_cursor = "30"
343 completions = list(c.get_completions(fake_document, None))
344
345 print(completions)
346 for c in completions:
347 print(c.text)
348 print(c.display)
349 print(c.display_meta)
350 assert completions == [
351 Completion(
352 text="1578487043",
353 start_position=-2,
354 display=FormattedText([("", "1578487043")]),
355 display_meta="30 seconds later (2020-01-08 12:37:23)",
356 ),
357 Completion(
358 text="1578488813",
359 start_position=-2,
360 display=FormattedText([("", "1578488813")]),
361 display_meta="30 minutes later (2020-01-08 13:06:53)",
362 ),
363 Completion(
364 text="1578595013",
365 start_position=-2,
366 display=FormattedText([("", "1578595013")]),
367 display_meta="30 hours later (2020-01-09 18:36:53)",
368 ),
369 Completion(
370 text="1581079013",
371 start_position=-2,
372 display=FormattedText([("", "1581079013")]),
373 display_meta="30 days later (2020-02-07 12:36:53)",
374 ),
375 ]
376
377
378 def test_timestamp_completer_datetime_format_time_completion():
379 c = TimestampCompleter(is_milliseconds=True, future_time=False)
380 fake_document = MagicMock()
381 fake_document.text = fake_document.text_before_cursor = "2020-02-07"
382 completions = list(c.get_completions(fake_document, None))
383
384 assert completions == [
385 Completion(
386 text="1581033600000",
387 start_position=-10,
388 display=FormattedText([("", "1581033600000")]),
389 display_meta="2020-02-07 00:00:00+00:00",
390 )
391 ]
392
393
394 def test_integer_type_completer():
395 c = IntegerTypeCompleter()
396 fake_document = MagicMock()
397 fake_document.text = fake_document.get_word_before_cursor.return_value = "i"
398 completions = list(c.get_completions(fake_document, None))
399 assert len(completions) == 64
400
401 fake_document.text = fake_document.get_word_before_cursor.return_value = "u"
402 completions = list(c.get_completions(fake_document, None))
403 assert len(completions) == 63
404
405 c.touch("u4")
406 assert list(c.get_completions(fake_document, None))[0].text == "u4"
407
408
409 def test_completion_casing():
410 c = IRedisCompleter(completion_casing="auto")
411 fake_document = MagicMock()
412 fake_document.text = fake_document.text_before_cursor = "ge"
413 assert [
414 completion.text for completion in c.get_completions(fake_document, None)
415 ] == [
416 "get",
417 "getex",
418 "getset",
419 "getdel",
420 "getbit",
421 "geopos",
422 "geoadd",
423 "geohash",
424 "geodist",
425 "getrange",
426 "geosearch",
427 "georadius",
428 "georadius_ro",
429 "geosearchstore",
430 "georadiusbymember",
431 "georadiusbymember_ro",
432 ]
433
434 c = IRedisCompleter(completion_casing="auto")
435 fake_document.text = fake_document.text_before_cursor = "GET"
436 assert [
437 completion.text for completion in c.get_completions(fake_document, None)
438 ] == ["GET", "GETEX", "GETSET", "GETDEL", "GETBIT", "GETRANGE"]
439
440 c = IRedisCompleter(completion_casing="upper")
441 fake_document.text = fake_document.text_before_cursor = "get"
442 assert [
443 completion.text for completion in c.get_completions(fake_document, None)
444 ] == ["GET", "GETEX", "GETSET", "GETDEL", "GETBIT", "GETRANGE"]
445
446 c = IRedisCompleter(completion_casing="lower")
447 fake_document.text = fake_document.text_before_cursor = "GET"
448 assert [
449 completion.text for completion in c.get_completions(fake_document, None)
450 ] == ["get", "getex", "getset", "getdel", "getbit", "getrange"]
451
452
453 def test_username_completer():
454 completer = IRedisCompleter()
455 completer.update_completer_for_input("acl deluser laixintao")
456 completer.update_completer_for_input("acl deluser antirez")
457
458 fake_document = MagicMock()
459 fake_document.text_before_cursor = fake_document.text = "acl deluser "
460 completions = list(completer.get_completions(fake_document, None))
461 assert sorted([completion.text for completion in completions]) == [
462 "antirez",
463 "laixintao",
464 ]
465
466
467 def test_username_touch_for_response():
468 c = IRedisCompleter()
469 c.update_completer_for_response(
470 "acl users",
471 (),
472 [b"hello", b"world"],
473 )
474 assert sorted(c.username_completer.words) == [
475 "hello",
476 "world",
477 ]