|
| 1 | +"""Silent-failure regression tests: unsupported constructs must warn, not vanish.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | +import warnings |
| 7 | +from schemaforge.generators.json_schema_generator import JSONSchemaGenerator |
| 8 | +from schemaforge.parsers.sql_parser import SQLParser |
| 9 | +from schemaforge.type_config import TypeConfig |
| 10 | + |
| 11 | + |
| 12 | +def test_sql_parser_warns_on_foreign_key_constraint() -> None: |
| 13 | + sql = """ |
| 14 | + CREATE TABLE orders ( |
| 15 | + id INTEGER PRIMARY KEY, |
| 16 | + customer_id INTEGER, |
| 17 | + FOREIGN KEY (customer_id) REFERENCES customers(id) |
| 18 | + ); |
| 19 | + """ |
| 20 | + with warnings.catch_warnings(record=True) as caught: |
| 21 | + warnings.simplefilter("always") |
| 22 | + SQLParser().parse(sql) |
| 23 | + assert any("FOREIGN KEY" in str(w.message) for w in caught) |
| 24 | + |
| 25 | + |
| 26 | +def test_sql_parser_warns_on_named_check_constraint() -> None: |
| 27 | + sql = """ |
| 28 | + CREATE TABLE products ( |
| 29 | + price NUMERIC, |
| 30 | + CONSTRAINT positive_price CHECK (price >= 0) |
| 31 | + ); |
| 32 | + """ |
| 33 | + with warnings.catch_warnings(record=True) as caught: |
| 34 | + warnings.simplefilter("always") |
| 35 | + SQLParser().parse(sql) |
| 36 | + assert any("CHECK" in str(w.message) for w in caught) |
| 37 | + |
| 38 | + |
| 39 | +def test_sql_parser_no_warning_for_plain_table() -> None: |
| 40 | + sql = """ |
| 41 | + CREATE TABLE users ( |
| 42 | + id INTEGER PRIMARY KEY, |
| 43 | + email VARCHAR(255) NOT NULL |
| 44 | + ); |
| 45 | + """ |
| 46 | + with warnings.catch_warnings(record=True) as caught: |
| 47 | + warnings.simplefilter("always") |
| 48 | + SQLParser().parse(sql) |
| 49 | + assert not caught |
| 50 | + |
| 51 | + |
| 52 | +def test_json_schema_generator_warns_on_malformed_override() -> None: |
| 53 | + from schemaforge.ir import Column, ColumnType |
| 54 | + |
| 55 | + class MalformedOverrideCfg(TypeConfig): |
| 56 | + """Returns an override that looks like JSON but fails to parse.""" |
| 57 | + |
| 58 | + def get_override(self, col, fmt, type_args=None): # noqa: D102 |
| 59 | + return "{broken json}" |
| 60 | + |
| 61 | + col = Column(name="email", type=ColumnType.STRING, nullable=True) |
| 62 | + gen = JSONSchemaGenerator(type_config=MalformedOverrideCfg()) |
| 63 | + with warnings.catch_warnings(record=True) as caught: |
| 64 | + warnings.simplefilter("always") |
| 65 | + gen._column_to_prop(col) |
| 66 | + assert any("failed to parse" in str(w.message) for w in caught) |
| 67 | + |
| 68 | + |
| 69 | +def _make_column(name: str): |
| 70 | + from schemaforge.ir import Column, ColumnType |
| 71 | + |
| 72 | + return Column(name=name, type=ColumnType.STRING, nullable=True) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize( |
| 76 | + "sql", |
| 77 | + [ |
| 78 | + "CREATE TABLE t (a INT, CONSTRAINT fk_a FOREIGN KEY (a) REFERENCES o(a));", |
| 79 | + "CREATE TABLE t (a INT, FOREIGN KEY (a) REFERENCES o(a));", |
| 80 | + ], |
| 81 | +) |
| 82 | +def test_warning_includes_table_name(sql: str) -> None: |
| 83 | + with warnings.catch_warnings(record=True) as caught: |
| 84 | + warnings.simplefilter("always") |
| 85 | + SQLParser().parse(sql) |
| 86 | + assert any("'t'" in str(w.message) for w in caught) |
0 commit comments