Skip to content

Commit 73a1b82

Browse files
committed
Add test_exceptions
1 parent 539ce3d commit 73a1b82

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

cratedb_sqlparse_py/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ check = [
205205
format = [
206206
{ cmd = "ruff format ." },
207207
# Configure Ruff not to auto-fix (remove!) unused variables (F841) and `print` statements (T201).
208-
{ cmd = "ruff check --fix --ignore=ERA --ignore=F401 --ignore=F841 --ignore=T20 ." },
208+
{ cmd = "ruff check --fix --ignore=ERA --ignore=F401 --ignore=F841 --ignore=T20 --ignore=E501 ." },
209209
{ cmd = "pyproject-fmt --keep-full-version pyproject.toml" },
210210
]
211211

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import pytest
2+
3+
4+
def test_exception_message():
5+
from cratedb_sqlparse import sqlparse
6+
7+
r = sqlparse("""
8+
SELEC 1;
9+
SELECT A, B, C, D FROM tbl1;
10+
SELECT D, A FROM tbl1 WHERE;
11+
""")
12+
expected_message = "InputMismatchException[line 2:9 mismatched input 'SELEC' expecting {'SELECT', 'DEALLOCATE', 'FETCH', 'END', 'WITH', 'CREATE', 'ALTER', 'KILL', 'CLOSE', 'BEGIN', 'START', 'COMMIT', 'ANALYZE', 'DISCARD', 'EXPLAIN', 'SHOW', 'OPTIMIZE', 'REFRESH', 'RESTORE', 'DROP', 'INSERT', 'VALUES', 'DELETE', 'UPDATE', 'SET', 'RESET', 'COPY', 'GRANT', 'DENY', 'REVOKE', 'DECLARE'}]"
13+
expected_message_2 = "\n SELEC 1;\n ^^^^^\n SELECT A, B, C, D FROM tbl1;\n SELECT D, A FROM tbl1 WHERE;\n "
14+
assert r[0].exception.error_message == expected_message
15+
assert r[0].exception.original_query_with_error_marked == expected_message_2
16+
17+
18+
def test_sqlparse_raises_exception():
19+
from cratedb_sqlparse import ParsingException, sqlparse
20+
21+
query = "SELCT 2"
22+
23+
with pytest.raises(ParsingException):
24+
sqlparse(query, raise_exception=True)
25+
26+
27+
def test_sqlparse_collects_exception():
28+
from cratedb_sqlparse import sqlparse
29+
30+
query = "SELCT 2"
31+
32+
statements = sqlparse(query)
33+
assert statements[0]
34+
35+
36+
def test_sqlparse_collects_exceptions():
37+
from cratedb_sqlparse import sqlparse
38+
39+
r = sqlparse("""
40+
SELECT A FROM tbl1 where ;
41+
SELECT 1;
42+
SELECT D, A FROM tbl1 WHERE;
43+
""")
44+
45+
assert len(r) == 3
46+
47+
assert r[0].exception is not None
48+
assert r[1].exception is None
49+
assert r[2].exception is not None
50+
51+
52+
def test_sqlparse_collects_exceptions_2():
53+
from cratedb_sqlparse import sqlparse
54+
55+
# Different combination of the query to validate
56+
r = sqlparse("""
57+
SELEC 1;
58+
SELECT A, B, C, D FROM tbl1;
59+
SELECT D, A FROM tbl1 WHERE;
60+
""")
61+
62+
assert r[0].exception is not None
63+
assert r[1].exception is None
64+
assert r[2].exception is not None
65+
66+
67+
def test_sqlparse_collects_exceptions_3():
68+
from cratedb_sqlparse import sqlparse
69+
70+
# Different combination of the query to validate
71+
r = sqlparse("""
72+
SELECT 1;
73+
SELECT A, B, C, D FROM tbl1;
74+
INSERT INTO doc.tbl VALUES (1,2, 'three', ['four']);
75+
""")
76+
77+
assert r[0].exception is None
78+
assert r[1].exception is None
79+
assert r[2].exception is None

cratedb_sqlparse_py/tests/test_lexer.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,20 @@ def test_sqlparse_dollar_string():
4444
assert r[0].query == query
4545

4646

47-
def test_sqlparse_raises_exception():
48-
from cratedb_sqlparse import ParsingException, sqlparse
47+
def test_sqlparse_multiquery_edge_case():
48+
# Test for https://github.com/crate/cratedb-sqlparse/issues/28,
49+
# if this ends up parsing 3 statements, we can change this test,
50+
# it's here so we can programmatically track if the behavior changes.
51+
from cratedb_sqlparse import sqlparse
4952

50-
query = "SALUT MON AMIE"
53+
query = """
54+
SELECT A FROM tbl1 where ;
55+
SELEC 1;
56+
SELECT D, A FROM tbl1 WHERE;
57+
"""
5158

52-
with pytest.raises(ParsingException):
53-
sqlparse(query)
59+
statements = sqlparse(query)
60+
assert len(statements) == 1
5461

5562

5663
def test_sqlparse_is_case_insensitive():

0 commit comments

Comments
 (0)