summaryrefslogtreecommitdiffstats
path: root/tests/test_parseutils.py
blob: cad7a8ca63e2bfdb9a35e9768ce18dd97faa6513 (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
import pytest
from litecli.packages.parseutils import (
    extract_tables,
    query_starts_with,
    queries_start_with,
    is_destructive,
)


def test_empty_string():
    tables = extract_tables("")
    assert tables == []


def test_simple_select_single_table():
    tables = extract_tables("select * from abc")
    assert tables == [(None, "abc", None)]


def test_simple_select_single_table_schema_qualified():
    tables = extract_tables("select * from abc.def")
    assert tables == [("abc", "def", None)]


def test_simple_select_multiple_tables():
    tables = extract_tables("select * from abc, def")
    assert sorted(tables) == [(None, "abc", None), (None, "def", None)]


def test_simple_select_multiple_tables_schema_qualified():
    tables = extract_tables("select * from abc.def, ghi.jkl")
    assert sorted(tables) == [("abc", "def", None), ("ghi", "jkl", None)]


def test_simple_select_with_cols_single_table():
    tables = extract_tables("select a,b from abc")
    assert tables == [(None, "abc", None)]


def test_simple_select_with_cols_single_table_schema_qualified():
    tables = extract_tables("select a,b from abc.def")
    assert tables == [("abc", "def", None)]


def test_simple_select_with_cols_multiple_tables():
    tables = extract_tables("select a,b from abc, def")
    assert sorted(tables) == [(None, "abc", None), (None, "def", None)]


def test_simple_select_with_cols_multiple_tables_with_schema():
    tables = extract_tables("select a,b from abc.def, def.ghi")
    assert sorted(tables) == [("abc", "def", None), ("def", "ghi", None)]


def test_select_with_hanging_comma_single_table():
    tables = extract_tables("select a, from abc")
    assert tables == [(None, "abc", None)]


def test_select_with_hanging_comma_multiple_tables():
    tables = extract_tables("select a, from abc, def")
    assert sorted(tables) == [(None, "abc", None), (None, "def", None)]


def test_select_with_hanging_period_multiple_tables():
    tables = extract_tables("SELECT t1. FROM tabl1 t1, tabl2 t2")
    assert sorted(tables) == [(None, "tabl1", "t1"), (None, "tabl2", "t2")]


def test_simple_insert_single_table():
    tables = extract_tables('insert into abc (id, name) values (1, "def")')

    # sqlparse mistakenly assigns an alias to the table
    # assert tables == [(None, 'abc', None)]
    assert tables == [(None, "abc", "abc")]


@pytest.mark.xfail
def test_simple_insert_single_table_schema_qualified():
    tables = extract_tables('insert into abc.def (id, name) values (1, "def")')
    assert tables == [("abc", "def", None)]


def test_simple_update_table():
    tables = extract_tables("update abc set id = 1")
    assert tables == [(None, "abc", None)]


def test_simple_update_table_with_schema():
    tables = extract_tables("update abc.def set id = 1")
    assert tables == [("abc", "def", None)]


def test_join_table():
    tables = extract_tables("SELECT * FROM abc a JOIN def d ON a.id = d.num")
    assert sorted(tables) == [(None, "abc", "a"), (None, "def", "d")]


def test_join_table_schema_qualified():
    tables = extract_tables("SELECT * FROM abc.def x JOIN ghi.jkl y ON x.id = y.num")
    assert tables == [("abc", "def", "x"), ("ghi", "jkl", "y")]


def test_join_as_table():
    tables = extract_tables("SELECT * FROM my_table AS m WHERE m.a > 5")
    assert tables == [(None, "my_table", "m")]


def test_query_starts_with():
    query = "USE test;"
    assert query_starts_with(query, ("use",)) is True

    query = "DROP DATABASE test;"
    assert query_starts_with(query, ("use",)) is False


def test_query_starts_with_comment():
    query = "# comment\nUSE test;"
    assert query_starts_with(query, ("use",)) is True


def test_queries_start_with():
    sql = "# comment\n" "show databases;" "use foo;"
    assert queries_start_with(sql, ("show", "select")) is True
    assert queries_start_with(sql, ("use", "drop")) is True
    assert queries_start_with(sql, ("delete", "update")) is False


def test_is_destructive():
    sql = "use test;\n" "show databases;\n" "drop database foo;"
    assert is_destructive(sql) is True