|
3 | 3 | import pytest |
4 | 4 |
|
5 | 5 | from mycli.packages import special |
6 | | -from mycli.packages.completion_engine import suggest_type |
| 6 | +from mycli.packages.completion_engine import ( |
| 7 | + _find_doubled_backticks, |
| 8 | + is_inside_quotes, |
| 9 | + suggest_type, |
| 10 | +) |
7 | 11 |
|
8 | 12 |
|
9 | 13 | def sorted_dicts(dicts): |
@@ -628,3 +632,81 @@ def test_quoted_where(): |
628 | 632 | text = "'where i=';" |
629 | 633 | suggestions = suggest_type(text, text) |
630 | 634 | assert suggestions == [{"type": "keyword"}] |
| 635 | + |
| 636 | + |
| 637 | +def test_find_doubled_backticks_none(): |
| 638 | + text = 'select `ab`' |
| 639 | + assert _find_doubled_backticks(text) == [] |
| 640 | + |
| 641 | + |
| 642 | +def test_find_doubled_backticks_some(): |
| 643 | + text = 'select `a``b`' |
| 644 | + assert _find_doubled_backticks(text) == [9, 10] |
| 645 | + |
| 646 | + |
| 647 | +def test_inside_quotes_01(): |
| 648 | + text = "select '" |
| 649 | + assert is_inside_quotes(text, len(text)) == 'single' |
| 650 | + |
| 651 | + |
| 652 | +def test_inside_quotes_02(): |
| 653 | + text = "select '\\'" |
| 654 | + assert is_inside_quotes(text, len(text)) == 'single' |
| 655 | + |
| 656 | + |
| 657 | +def test_inside_quotes_03(): |
| 658 | + text = "select '`" |
| 659 | + assert is_inside_quotes(text, len(text)) == 'single' |
| 660 | + |
| 661 | + |
| 662 | +def test_inside_quotes_04(): |
| 663 | + text = 'select "' |
| 664 | + assert is_inside_quotes(text, len(text)) == 'double' |
| 665 | + |
| 666 | + |
| 667 | +def test_inside_quotes_05(): |
| 668 | + text = 'select "\\"\'' |
| 669 | + assert is_inside_quotes(text, len(text)) == 'double' |
| 670 | + |
| 671 | + |
| 672 | +def test_inside_quotes_06(): |
| 673 | + text = 'select ""' |
| 674 | + assert is_inside_quotes(text, len(text)) is False |
| 675 | + |
| 676 | + |
| 677 | +@pytest.mark.parametrize( |
| 678 | + ["text", "position", "expected"], |
| 679 | + [ |
| 680 | + ("select `'", len("select `'"), 'backtick'), |
| 681 | + ("select `' ", len("select `' "), 'backtick'), |
| 682 | + ("select `'", -1, 'backtick'), |
| 683 | + ("select `'", -2, False), |
| 684 | + ('select `ab` ', -1, False), |
| 685 | + ('select `ab` ', -2, 'backtick'), |
| 686 | + ('select `a``b` ', -1, False), |
| 687 | + ('select `a``b` ', -2, 'backtick'), |
| 688 | + ('select `a``b` ', -3, 'backtick'), |
| 689 | + ('select `a``b` ', -4, 'backtick'), |
| 690 | + ('select `a``b` ', -5, 'backtick'), |
| 691 | + ('select `a``b` ', -6, 'backtick'), |
| 692 | + ('select `a``b` ', -7, False), |
| 693 | + ] |
| 694 | +) # fmt: skip |
| 695 | +def test_inside_quotes_backtick_01(text, position, expected): |
| 696 | + assert is_inside_quotes(text, position) == expected |
| 697 | + |
| 698 | + |
| 699 | +def test_inside_quotes_backtick_02(): |
| 700 | + """Empty backtick pairs are treated as a doubled (escaped) backtick. |
| 701 | + This is okay because it is invalid SQL, and we don't have to complete on it. |
| 702 | + """ |
| 703 | + text = 'select ``' |
| 704 | + assert is_inside_quotes(text, -1) is False |
| 705 | + |
| 706 | + |
| 707 | +def test_inside_quotes_backtick_03(): |
| 708 | + """Empty backtick pairs are treated as a doubled (escaped) backtick. |
| 709 | + This is okay because it is invalid SQL, and we don't have to complete on it. |
| 710 | + """ |
| 711 | + text = 'select ``' |
| 712 | + assert is_inside_quotes(text, -2) is False |
0 commit comments