"""Smoke-tests that every `ENTITY_REGISTRY` import schema is importable and constructible, plus spot checks of enum-alias coercion.""" import pytest from pydantic import ValidationError from webapi.imports.schemas.registry import ENTITY_REGISTRY from webapi.imports.schemas.cama.ordenes_estudio import OrdenesEstudioImportSchema as CamaOrdenesEstudioImportSchema from webapi.imports.schemas.tea.ados2 import Ados2ImportSchema from webapi.imports.schemas.tea.mchat import MchatImportSchema from webapi.imports.schemas.cacu.biopsia_cervical import BiopsiaCervicalImportSchema from webapi.imports.schemas.cacu.episodio_clinico import EpisodioClinicoImportSchema # Domains whose Core layer is fully permissive (every field Optional); # see `tea/pacientes.py`'s docstring. CaCu/CaMa entities all carry at least # one business-required field (`pac_id_hospital` and friends). _FULLY_OPTIONAL_PREFIXES = ("tea.",) @pytest.mark.parametrize("entity_key", list(ENTITY_REGISTRY.keys())) def test_every_registered_schema_is_constructible(entity_key): spec = ENTITY_REGISTRY[entity_key] schema_cls = spec.schema if entity_key.startswith(_FULLY_OPTIONAL_PREFIXES): # Fully permissive: an empty row must construct cleanly. instance = schema_cls() assert instance is not None else: # Has at least one required field: an empty row must be rejected. with pytest.raises(ValidationError): schema_cls() # --- Spot checks ----------------------------------------------------------- # CaMa entity with a required, unique integer ord_numero_hospital and an # *_ALIAS-backed enum field. def test_cama_ordenes_estudio_requires_numero_hospital_and_coerces_tipo_estudio(): with pytest.raises(ValidationError) as excinfo: CamaOrdenesEstudioImportSchema(pac_id_hospital="H-1") errors = excinfo.value.errors() assert any(e["loc"] == ("ord_numero_hospital",) for e in errors) schema = CamaOrdenesEstudioImportSchema( pac_id_hospital="H-1", ord_numero_hospital=12345, ord_tipo_estudio="biopsia chica" ) assert schema.ord_tipo_estudio == "BIOPSIA_CHICA" def test_tea_ados2_coerces_module_and_classification_aliases(): """Pins down: the ADOS-2's module comes from the bare enum and its classification takes the old spelling `NO_TEA`. Fails if either catalog stops normalizing case or aliases.""" schema = Ados2ImportSchema(ado_modulo="modulo_1", ado_clasificacion="no_tea") assert (schema.ado_modulo, schema.ado_clasificacion) == ("MODULO_1", "NO_ESPECTRO") @pytest.mark.parametrize(("schema", "field", "value"), [ (MchatImportSchema, "mch_puntaje", 21), (Ados2ImportSchema, "ado_css", 0), (Ados2ImportSchema, "ado_sa_b1", 4), ]) def test_tea_scores_out_of_their_range_are_rejected(schema, field, value): """Pins down: an M-CHAT score above 20, a CSS below 1 and an ADOS-2 item above 3 are validation errors. Fails if a bound of `Bounded` is dropped from the schemas.""" with pytest.raises(ValidationError): schema(**{field: value}) # CaCu entity with a real *_ALIAS-backed field (GRADO_CIN_ALIAS). def test_cacu_biopsia_cervical_coerces_cin_grado_alias(): schema = BiopsiaCervicalImportSchema( pac_id_hospital="H-1", bx_cin_grado="LESION INTRAEPITELIAL ESCAMOSA DE BAJO GRADO", ) assert schema.bx_cin_grado == "LIEBG" with pytest.raises(ValidationError): BiopsiaCervicalImportSchema(bx_cin_grado="LIEBG") # pac_id_hospital missing # CaCu EpisodioClinico: requires pac_id_hospital and coerces tipo_episodio/ # especialidad_medica through their respective *_ALIAS catalogs. def test_cacu_episodio_clinico_requires_pac_id_hospital_and_coerces_catalogs(): with pytest.raises(ValidationError): EpisodioClinicoImportSchema(tipo_episodio="SCREENING") # pac_id_hospital missing schema = EpisodioClinicoImportSchema( pac_id_hospital="H-1", tipo_episodio="SCREENING", especialidad_medica="MEDICINA FAMILIAR", ) assert schema.tipo_episodio == "TAMIZAJE" assert schema.especialidad_medica == "MEDICINA_FAMILIAR"