|
| 1 | +import re |
| 2 | +import time |
| 3 | +import logging |
| 4 | + |
| 5 | +from preql import Preql |
| 6 | +from preql.autocomplete import autocomplete |
| 7 | +from preql.loggers import test_log |
| 8 | + |
| 9 | +from .common import PreqlTests, SQLITE_URI, POSTGRES_URI |
| 10 | + |
| 11 | +class AutocompleteTests(PreqlTests): |
| 12 | + uri = SQLITE_URI |
| 13 | + optimized = True |
| 14 | + |
| 15 | + def test_basic(self): |
| 16 | + p = self.Preql() |
| 17 | + state = p.interp.state |
| 18 | + |
| 19 | + assert "value" in autocomplete(state, "func d(){ [1]{") |
| 20 | + assert "value" in autocomplete(state, "func d(){ [1][") |
| 21 | + assert "value" not in autocomplete(state, "func d(){ [1]") |
| 22 | + |
| 23 | + res = autocomplete(state, """ |
| 24 | + func x(param1) { |
| 25 | + hello = "b" |
| 26 | + """) |
| 27 | + assert "hello" in res, res.keys() |
| 28 | + |
| 29 | + res = autocomplete(state, """ |
| 30 | + func x(param1) { |
| 31 | + hello = "b |
| 32 | + """) |
| 33 | + |
| 34 | + res = autocomplete(state, """ |
| 35 | + func x(param1) { |
| 36 | + hello = [1] {value, value+2} |
| 37 | + """) |
| 38 | + assert "hello" in res, res.keys() |
| 39 | + |
| 40 | + def test_progressive(self): |
| 41 | + p = self.Preql() |
| 42 | + state = p.interp.state |
| 43 | + |
| 44 | + s0 = """ |
| 45 | + func hello() = 0 |
| 46 | +
|
| 47 | + a = <<<hello>>> |
| 48 | + """ |
| 49 | + progressive_test(state, s0) |
| 50 | + progressive_test(state, s0, True) |
| 51 | + |
| 52 | + s1 = """ |
| 53 | + func get_users(logins) { |
| 54 | + const table matched_logins = <<<leftjoin>>>(l:logins.value, u:User.login) |
| 55 | +
|
| 56 | + existing_users = <<<matched_logins>>>[<<<u>>>!=null] {<<<u>>>.id} |
| 57 | + new_users = new[] User(login: <<<matched_logins>>>[<<<u>>>==null] {<<<l>>>.value}) |
| 58 | +
|
| 59 | + return <<<existing_users>>> + <<<new_users>>> |
| 60 | + } |
| 61 | +
|
| 62 | + hello = <<<get_users>>>([1,2,3]) |
| 63 | + do_whatever = <<<hello>>> |
| 64 | +
|
| 65 | + """ |
| 66 | + progressive_test(state, s1*2) |
| 67 | + progressive_test(state, s1, True) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +def _parse_autocomplete_requirements(s): |
| 73 | + matches = {} |
| 74 | + offset = 0 |
| 75 | + def g(m): |
| 76 | + nonlocal offset |
| 77 | + start = m.start() + offset |
| 78 | + x ,= m.groups() |
| 79 | + matches[start] = x |
| 80 | + offset -= 6 |
| 81 | + return x |
| 82 | + |
| 83 | + new_s = re.sub("<<<(\w+)>>>", g, s) |
| 84 | + for k, v in matches.items(): |
| 85 | + assert new_s[k:k+len(v)] == v, (k, v) |
| 86 | + return new_s, matches |
| 87 | + |
| 88 | + |
| 89 | +def progressive_test(state, s, test_partial=False): |
| 90 | + total = 0 |
| 91 | + start = time.time() |
| 92 | + |
| 93 | + s,d = _parse_autocomplete_requirements(s) |
| 94 | + for i in range(1, len(s)): |
| 95 | + ps = s[:i] |
| 96 | + if i in d or test_partial: |
| 97 | + names = autocomplete(state, ps) |
| 98 | + total += 1 |
| 99 | + if i in d: |
| 100 | + assert d[i] in names, (i, d[i]) |
| 101 | + |
| 102 | + duration = time.time() - start |
| 103 | + test_log.info(f"Total {total} autocompletions in {duration:.2f} seconds, or {1000*duration/total:.2f} ms per autocomplete") |
0 commit comments