summaryrefslogtreecommitdiffstats
path: root/tests/test_parseutils.py
blob: e49aec68e31ffd6c2ed0885d8904af6ff2f3e34b (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
from pgcli.packages.parseutils import extract_tables


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

def test_simple_select_single_table():
    tables = extract_tables('select * from abc')
    assert tables == ['abc']

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

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

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

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

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

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

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

def test_join_table():
    expected = {'a': 'abc', 'd': 'def'}
    tables = extract_tables('SELECT * FROM abc a JOIN def d ON a.id = d.num')
    tables_aliases = extract_tables(
            'SELECT * FROM abc a JOIN def d ON a.id = d.num', True)
    assert tables == sorted(expected.values())
    assert tables_aliases == expected

def test_join_as_table():
    expected = {'m': 'my_table'}
    assert extract_tables(
            'SELECT * FROM my_table AS m WHERE m.a > 5') == \
                    sorted(expected.values())
    assert extract_tables(
            'SELECT * FROM my_table AS m WHERE m.a > 5', True) == expected