|
| 1 | +import json |
| 2 | + |
| 3 | +import duckdb |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +def _parse_json_func(error_prefix: str): |
| 8 | + """Helper to check that the error message is indeed parsable json""" |
| 9 | + |
| 10 | + def parse_func(exception): |
| 11 | + msg = exception.args[0] |
| 12 | + assert msg.startswith(error_prefix) |
| 13 | + json_str = msg.split(error_prefix, 1)[1] |
| 14 | + try: |
| 15 | + json.loads(json_str) |
| 16 | + except: |
| 17 | + return False |
| 18 | + return True |
| 19 | + |
| 20 | + return parse_func |
| 21 | + |
| 22 | + |
| 23 | +def test_json_syntax_error(): |
| 24 | + conn = duckdb.connect() |
| 25 | + conn.execute("SET errors_as_json='true'") |
| 26 | + with pytest.raises(duckdb.ParserException, match="SYNTAX_ERROR", check=_parse_json_func("Parser Error: ")): |
| 27 | + conn.execute("syntax error") |
| 28 | + |
| 29 | + |
| 30 | +def test_json_catalog_error(): |
| 31 | + conn = duckdb.connect() |
| 32 | + conn.execute("SET errors_as_json='true'") |
| 33 | + with pytest.raises(duckdb.CatalogException, match="MISSING_ENTRY", check=_parse_json_func("Catalog Error: ")): |
| 34 | + conn.execute("SELECT * FROM nonexistent_table") |
| 35 | + |
| 36 | + |
| 37 | +def test_json_syntax_error_extract_statements(): |
| 38 | + conn = duckdb.connect() |
| 39 | + conn.execute("SET errors_as_json='true'") |
| 40 | + with pytest.raises(duckdb.ParserException, match="SYNTAX_ERROR", check=_parse_json_func("Parser Error: ")): |
| 41 | + conn.extract_statements("syntax error") |
| 42 | + |
| 43 | + |
| 44 | +def test_json_syntax_error_get_table_names(): |
| 45 | + conn = duckdb.connect() |
| 46 | + conn.execute("SET errors_as_json='true'") |
| 47 | + with pytest.raises(duckdb.ParserException, match="SYNTAX_ERROR", check=_parse_json_func("Parser Error: ")): |
| 48 | + conn.get_table_names("syntax error") |
0 commit comments