Files
ThothII/harness/nsp/mschema/models.py
T
marcopan eb3bde90e2 test(harness): L0 testcontainers + L1 contract tests for ported db/mschema/rest (A9, spec §1)
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.
2026-06-26 22:53:08 +02:00

91 lines
2.1 KiB
Python

from datetime import datetime
from pathlib import Path
from typing import Self
import yaml
from pydantic import BaseModel, Field
class _YamlModel(BaseModel):
def to_yaml(self, path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
data = self.model_dump(by_alias=True, mode="json", exclude_defaults=False)
path.write_text(
yaml.safe_dump(data, sort_keys=False, allow_unicode=True, width=120)
)
@classmethod
def from_yaml(cls, path: Path) -> Self:
raw = yaml.safe_load(path.read_text())
return cls.model_validate(raw)
class ColumnPhysical(BaseModel):
type: str
nullable: bool = True
pk: bool = False
default: str | None = None
comment: str = ""
examples: list[str] = []
is_enum: bool = False
eligible: bool = True
eligibility_reason: str = ""
class ForeignKey(BaseModel):
columns: list[str]
ref_table: str
ref_columns: list[str]
name: str = ""
class Index(BaseModel):
name: str
columns: list[str]
unique: bool = False
primary: bool = False
type: str = "btree"
class TablePhysical(BaseModel):
comment: str = ""
row_count: int = 0 # stima da pg_class.reltuples
columns: dict[str, ColumnPhysical] = {}
foreign_keys: list[ForeignKey] = []
indexes: list[Index] = []
class PhysicalSchema(_YamlModel):
database: str
db_schema: str = Field(alias="schema")
introspected_at: datetime
tables: dict[str, TablePhysical] = {}
model_config = {"populate_by_name": True}
class ColumnAnnotation(BaseModel):
description: str = ""
synonyms: list[str] = []
concepts: list[str] = []
evidence: list[str] = []
notes: str = ""
eligible: bool | None = None
class TableAnnotation(BaseModel):
description: str = ""
concepts: list[str] = []
notes: str = ""
columns: dict[str, ColumnAnnotation] = {}
class Annotations(_YamlModel):
tables: dict[str, TableAnnotation] = {}
@classmethod
def from_yaml(cls, path: Path) -> "Annotations":
if not path.exists():
return cls()
return super().from_yaml(path)