summaryrefslogtreecommitdiffstats
path: root/tests/test_pgcompleter.py
blob: 909fa0b7e01252d4e6cdbb553462de9ec6df0a9e (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
import pytest
from pgcli import pgcompleter


def test_load_alias_map_file_missing_file():
    with pytest.raises(
        pgcompleter.InvalidMapFile,
        match=r"Cannot read alias_map_file - /path/to/non-existent/file.json does not exist$",
    ):
        pgcompleter.load_alias_map_file("/path/to/non-existent/file.json")


def test_load_alias_map_file_invalid_json(tmp_path):
    fpath = tmp_path / "foo.json"
    fpath.write_text("this is not valid json")
    with pytest.raises(pgcompleter.InvalidMapFile, match=r".*is not valid json$"):
        pgcompleter.load_alias_map_file(str(fpath))


@pytest.mark.parametrize(
    "table_name, alias",
    [
        ("SomE_Table", "SET"),
        ("SOmeTabLe", "SOTL"),
        ("someTable", "T"),
    ],
)
def test_generate_alias_uses_upper_case_letters_from_name(table_name, alias):
    assert pgcompleter.generate_alias(table_name) == alias


@pytest.mark.parametrize(
    "table_name, alias",
    [
        ("some_tab_le", "stl"),
        ("s_ome_table", "sot"),
        ("sometable", "s"),
    ],
)
def test_generate_alias_uses_first_char_and_every_preceded_by_underscore(
    table_name, alias
):
    assert pgcompleter.generate_alias(table_name) == alias


@pytest.mark.parametrize(
    "table_name, alias_map, alias",
    [
        ("some_table", {"some_table": "my_alias"}, "my_alias"),
    ],
)
def test_generate_alias_can_use_alias_map(table_name, alias_map, alias):
    assert pgcompleter.generate_alias(table_name, alias_map) == alias


@pytest.mark.parametrize(
    "table_name, alias_map, alias",
    [
        ("SomeTable", {"SomeTable": "my_alias"}, "my_alias"),
    ],
)
def test_generate_alias_prefers_alias_over_upper_case_name(
    table_name, alias_map, alias
):
    assert pgcompleter.generate_alias(table_name, alias_map) == alias


@pytest.mark.parametrize(
    "table_name, alias",
    [
        ("Some_tablE", "SE"),
        ("SomeTab_le", "ST"),
    ],
)
def test_generate_alias_prefers_upper_case_name_over_underscore_name(table_name, alias):
    assert pgcompleter.generate_alias(table_name) == alias