|
| 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 |
0 commit comments