Ports the leaf data-layer modules and validates them: - mschema/ (models, eligibility, merge, render), db/ (connection, sampling, introspect, fetch_ca), rest/client.py -- renamed psdwp3->nsp, verbatim. - L0 (testcontainers, real Postgres): db connection read-only enforcement (psd_ro cannot CREATE/INSERT), introspect against a known schema (tables, columns, types, comments, FKs, enum, composite PK), sampling most-frequent values + truncation reporting. 15 tests, ~4s. - L1 (fake data): rest/client RPC contract (mocked transport -- X-API-Key header, payloads, base_url slash handling, HTTP/network error surfacing), mschema/render 3 formats (markdown, mschema-text, schema-dict) + eligibility rules (wide_text excluded, short_text/numeric/enum/temporal/ boolean eligible, annotation override wins). 25 tests. pyproject registers l0/l2 markers + addopts '-m not l2' (L2 opt-in). Deferred to their dependency-porting tasks: test_rrf.py (search needs vectorstore, B3) and the 11 CLI contract tests (need _guards/session, wired when each command lands). 'Not assumed reliable' now has real teeth for the data layer; CLI/search contracts follow.
59 lines
2.3 KiB
Python
59 lines
2.3 KiB
Python
"""L0: db/introspect against a known schema (testcontainers).
|
|
Verifies the ported introspection returns the right tables, columns, types, comments,
|
|
FKs, and indexes from a real Postgres catalog.
|
|
"""
|
|
import pytest
|
|
|
|
from nsp.db.introspect import IntrospectionError, introspect
|
|
|
|
pytestmark = [pytest.mark.l0]
|
|
|
|
|
|
def test_introspect_returns_known_tables(admin_engine):
|
|
schema = introspect(admin_engine, "testdb", "dw")
|
|
assert set(schema.tables.keys()) == {"dim_pazienti", "dim_periodi", "fct_ricoveri"}
|
|
assert schema.db_schema == "dw"
|
|
|
|
|
|
def test_introspect_columns_types_and_comments(admin_engine):
|
|
schema = introspect(admin_engine, "testdb", "dw")
|
|
pazienti = schema.tables["dim_pazienti"]
|
|
assert set(pazienti.columns.keys()) == {"id_paziente", "nome", "citta"}
|
|
assert pazienti.columns["id_paziente"].pk is True
|
|
assert pazienti.columns["id_paziente"].nullable is False
|
|
assert "bigint" in pazienti.columns["id_paziente"].type.lower()
|
|
assert pazienti.comment == "Anagrafica pazienti"
|
|
assert pazienti.columns["citta"].comment == "Comune di residenza"
|
|
|
|
|
|
def test_introspect_enum_detected(admin_engine):
|
|
schema = introspect(admin_engine, "testdb", "dw")
|
|
tipo_col = schema.tables["fct_ricoveri"].columns["tipo"]
|
|
assert tipo_col.is_enum is True
|
|
|
|
|
|
def test_introspect_foreign_keys(admin_engine):
|
|
schema = introspect(admin_engine, "testdb", "dw")
|
|
ricoveri = schema.tables["fct_ricoveri"]
|
|
fk_tables = {fk.ref_table for fk in ricoveri.foreign_keys}
|
|
assert "dim_pazienti" in fk_tables
|
|
assert "dim_periodi" in fk_tables
|
|
# the composite FK to dim_periodi (anno, mese)
|
|
periodi_fk = [fk for fk in ricoveri.foreign_keys if fk.ref_table == "dim_periodi"]
|
|
assert periodi_fk
|
|
assert set(periodi_fk[0].columns) == {"anno", "mese"}
|
|
|
|
|
|
def test_introspect_indexes(admin_engine):
|
|
schema = introspect(admin_engine, "testdb", "dw")
|
|
# composite PK on dim_periodi (anno, mese)
|
|
periodi = schema.tables["dim_periodi"]
|
|
pk_indexes = [i for i in periodi.indexes if i.primary]
|
|
assert pk_indexes, "dim_periodi should have a primary index"
|
|
assert set(pk_indexes[0].columns) == {"anno", "mese"}
|
|
|
|
|
|
def test_introspect_nonexistent_schema_raises(admin_engine):
|
|
with pytest.raises(IntrospectionError):
|
|
introspect(admin_engine, "testdb", "nonexistent_schema")
|