"""What Automático checks without asking the model: documents grouped by the words their layout prints, a proposed pattern kept only when it captures what the model read, a column's date format, and the site's codes as a ConceptMap the platform accepts.""" from core import concept_map from core.drafting import codes_schema, layout_schema, table_schema from models.draft import fields from models.draft.documents import layouts FORM = ( "SOLICITUD DE CITOLOGÍA CERVICAL\nNombre: {name}\nFecha de recepción: {date}\n" "Calidad de la muestra: SATISFACTORIO\nResultado: {result}\nObservaciones: ninguna" ) MAMMOGRAM = ( "REPORTE DE MASTOGRAFÍA BILATERAL\nPaciente: {name}\nFecha del estudio: {date}\n" "Hallazgos: densidad mamaria tipo B, sin masas\nBI-RADS: 2" ) def test_documents_of_one_layout_group_together_whoever_they_are_about(): texts = [ FORM.format(name="ANA LÓPEZ RUIZ", date="02/03/2025", result="NEGATIVO"), MAMMOGRAM.format(name="LUISA ORTIZ MENA", date="11/04/2025"), FORM.format(name="ROSA DÍAZ PAZ", date="15/06/2025", result="LSIL"), MAMMOGRAM.format(name="MARTA SOLÍS GIL", date="01/07/2025"), FORM.format(name="EVA RUIZ CANO", date="20/08/2025", result="ASC-US"), ] found = layouts(texts) assert found[0] == found[2] == found[4] != found[1] == found[3] def test_a_pattern_is_kept_only_if_it_captures_the_values_read(): texts = [ FORM.format(name="ANA LÓPEZ RUIZ", date="02/03/2025", result="NEGATIVO"), FORM.format(name="ROSA DÍAZ PAZ", date="15/06/2025", result="LSIL"), ] read = [ (texts[0], {"sample_date": "2025-03-02", "result": "NILM"}), (texts[1], {"sample_date": "2025-06-15", "result": "LSIL"}), ] codes = fields.Codes("sitio") entity = "cervical_cytology" date = r"recepci[oó]n:\s*(\d{2}/\d{2}/\d{4})" assert fields.checks(entity, "sample_date", date, "%d/%m/%Y", read, codes) assert not fields.checks(entity, "sample_date", date, "%m/%d/%Y", read, codes) # 15th month assert not fields.checks(entity, "sample_date", r"(\d{2}/\d{2})", None, read, codes) assert not fields.checks(entity, "sample_date", r"recepci[oó]n:", None, read, codes) # no group assert not fields.checks(entity, "result", r"resultado:(\s*)", None, read, codes) # nothing # A coded field: what the site writes becomes its ConceptMap, unless it is the code. assert fields.checks(entity, "result", r"resultado:\s*(\S+)", None, read, codes) assert codes.table() == {(entity, "result"): {"negativo": "NILM"}} # A pattern taking text that means two things is the wrong text. assert not fields.checks(entity, "result", r"(calidad)", None, read, fields.Codes("x")) found = codes.concept_map() assert concept_map.problems(found) == [] assert concept_map.table([found], "result") == {"negativo": "NILM"} def test_a_columns_dates_get_the_format_every_value_parses_with(): assert fields.date_format(["02/03/2025", "15/06/2025"]) == "%d/%m/%Y" assert fields.date_format(["2025-03-02", None]) == "%Y-%m-%d" assert fields.date_format(["mañana"]) is None def test_answers_are_held_to_the_platforms_terms(): layout = layout_schema()["properties"]["entity"]["enum"] assert "cervical_cytology" in layout and "none" in layout and "identity" not in layout codes = codes_schema(["NEGATIVO", "BAJO GRADO"], ["NILM", "LSIL"]) one = codes["$defs"]["Code"]["properties"] assert one["raw"]["enum"] == ["NEGATIVO", "BAJO GRADO"] assert {"enum": ["NILM", "LSIL"], "type": "string"} in one["code"]["anyOf"] table = table_schema(["num", "nombre"]) assert table["$defs"]["Who"]["properties"]["column"]["enum"] == ["num", "nombre"]