From 3c2ab790ad917d1817da66858dd359d66f818c9b Mon Sep 17 00:00:00 2001 From: abrichr Date: Sat, 21 Nov 2026 22:18:27 -0500 Subject: [PATCH 1/2] refactor(form-tx): move the patient registration preset to JSON The patient registration domain schema (field keys and label synonyms) is vocabulary for one kind of form, not engine code. It now ships as form_tx/presets/patient_registration.json (openadapt.form-domain-preset/v1), and build_patient_registration_domain_schema() loads it. The returned schema is equal to the one the old builder made; a test rebuilds the old builder from history and compares. --- .../openadapt_flow/form_tx/pack_compiler.py | 305 +--------- .../form_tx/presets/__init__.py | 51 ++ .../form_tx/presets/patient_registration.json | 530 ++++++++++++++++++ packages/flow/tests/test_form_tx_presets.py | 74 +++ 4 files changed, 659 insertions(+), 301 deletions(-) create mode 100644 packages/flow/openadapt_flow/form_tx/presets/__init__.py create mode 100644 packages/flow/openadapt_flow/form_tx/presets/patient_registration.json create mode 100644 packages/flow/tests/test_form_tx_presets.py diff --git a/packages/flow/openadapt_flow/form_tx/pack_compiler.py b/packages/flow/openadapt_flow/form_tx/pack_compiler.py index efdd2d5d86..acde36f3ed 100644 --- a/packages/flow/openadapt_flow/form_tx/pack_compiler.py +++ b/packages/flow/openadapt_flow/form_tx/pack_compiler.py @@ -142,307 +142,10 @@ class PackInductionReport(BaseModel): def build_patient_registration_domain_schema() -> CanonicalDomainSchema: - """Build generic clinical patient registration domain schema.""" - return CanonicalDomainSchema( - schema_id="patient_registration.v1", - fields=( - CanonicalFieldSpec( - dossier_key="first_name", - synonyms=( - "first name", - "given name", - "patient first name", - "legal first name", - "fname", - "first_name", - "given_name", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("patient", "demographics", "personal", "identity"), - required=True, - ), - CanonicalFieldSpec( - dossier_key="middle_name", - synonyms=( - "middle name", - "middle initial", - "mname", - "middle_name", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("patient", "demographics", "personal"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="last_name", - synonyms=( - "last name", - "surname", - "family name", - "patient last name", - "legal last name", - "lname", - "last_name", - "family_name", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("patient", "demographics", "personal", "identity"), - required=True, - ), - CanonicalFieldSpec( - dossier_key="date_of_birth", - synonyms=( - "date of birth", - "birth date", - "birthdate", - "dob", - "patient dob", - "born on", - "date_of_birth", - "birth_date", - ), - expected_codec=FieldCodecSpec( - kind=FormCodecKind.ISO_DATE, date_order="MDY" - ), - section_hints=("patient", "demographics", "personal"), - required=True, - ), - CanonicalFieldSpec( - dossier_key="administrative_sex", - synonyms=( - "administrative sex", - "sex at birth", - "biological sex", - "legal sex", - "sex", - "gender", - "sex_at_birth", - "administrative_sex", - ), - expected_codec=FieldCodecSpec( - kind=FormCodecKind.ENUM_MAP, - enum_aliases={ - "F": ("female", "f"), - "M": ("male", "m"), - "X": ("non-binary", "other", "unknown", "x", "u"), - }, - ), - section_hints=("patient", "demographics", "personal"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="phone_number", - synonyms=( - "phone number", - "mobile phone", - "primary phone", - "contact phone", - "cell phone", - "telephone", - "phone", - "mobile", - "tel", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.PHONE_DIGITS), - section_hints=("contact", "patient", "demographics"), - required=True, - ), - CanonicalFieldSpec( - dossier_key="email_address", - synonyms=( - "email address", - "email", - "patient email", - "electronic mail", - "email_address", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CASEFOLD), - section_hints=("contact", "patient", "demographics"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="street_address", - synonyms=( - "street address", - "home address", - "address line 1", - "residential address", - "street", - "address1", - "addr_line1", - "street_address", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("address", "contact", "residence", "demographics"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="city", - synonyms=("city", "town", "municipality", "patient_city"), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("address", "contact", "residence"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="state_code", - synonyms=("state", "province", "state code", "region", "state_code"), - expected_codec=FieldCodecSpec(kind=FormCodecKind.UPPER), - section_hints=("address", "contact", "residence"), - required=False, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="postal_code", - synonyms=( - "postal code", - "zip code", - "zip", - "postcode", - "postal_code", - "zip_code", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("address", "contact", "residence"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="insurance_plan_name", - synonyms=( - "insurance plan", - "payer name", - "coverage plan", - "health plan", - "insurance carrier", - "payer", - "insurance_plan", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("insurance", "coverage", "billing", "payer"), - required=False, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="insurance_member_id", - synonyms=( - "member id", - "subscriber id", - "insurance member id", - "policy member number", - "member number", - "member_id", - "subscriber_id", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.UPPER), - section_hints=("insurance", "coverage", "billing", "payer"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="emergency_contact_name", - synonyms=( - "emergency contact name", - "emergency contact", - "next of kin name", - "emergency_contact_name", - "emerg_name", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("emergency", "next of kin"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="emergency_contact_phone", - synonyms=( - "emergency contact phone", - "emergency phone", - "next of kin phone", - "emergency_contact_phone", - "emerg_phone", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.PHONE_DIGITS), - section_hints=("emergency", "next of kin"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="consent_to_treat", - synonyms=( - "consent to treat", - "treatment consent", - "hipaa consent", - "patient consent", - "consent_to_treat", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.BOOLEAN_YES_NO), - section_hints=("consent", "authorization", "agreement"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="full_name", - synonyms=( - "patient full name", - "full name", - "legal full name", - "complete name", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.TITLE_CASE), - section_hints=("patient", "demographics", "personal"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="visit_type", - synonyms=( - "visit category", - "visit type", - "appointment type", - "visit_type", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CASEFOLD), - section_hints=("queue", "visit", "routing", "appointment"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="clinical_department", - synonyms=( - "clinical department", - "department", - "specialty", - "clinic department", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CASEFOLD), - section_hints=("queue", "department", "routing"), - required=False, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="consulting_provider", - synonyms=( - "consulting doctor", - "attending physician", - "consulting provider", - "attending doctor", - "consulting_doctor", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CASEFOLD), - section_hints=("queue", "department", "routing", "provider"), - required=False, - ), - ), - expected_outputs=( - CanonicalFieldSpec( - dossier_key="medical_record_number", - synonyms=( - "medical record number", - "mrn", - "patient id", - "chart number", - "registration number", - "record number", - "mrn_output", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - required=True, - ), - ), - ) + """The patient registration preset (``presets/patient_registration.json``).""" + from openadapt_flow.form_tx.presets import load_domain_schema + + return load_domain_schema("patient_registration") def build_property_quote_domain_schema() -> CanonicalDomainSchema: diff --git a/packages/flow/openadapt_flow/form_tx/presets/__init__.py b/packages/flow/openadapt_flow/form_tx/presets/__init__.py new file mode 100644 index 0000000000..57c67ebcd6 --- /dev/null +++ b/packages/flow/openadapt_flow/form_tx/presets/__init__.py @@ -0,0 +1,51 @@ +"""Domain presets for ``form_tx`` pack induction, kept as data. + +A preset is a canonical domain schema (the fields a kind of form asks for, +with label synonyms) used by ``induce_form_pack`` and the ``--domain`` flag. +It is vocabulary for one kind of form, so it lives here as JSON +(``openadapt.form-domain-preset/v1``), not in the engine's code. The +``build_*_domain_schema`` functions in ``pack_compiler`` load these files and +return the same objects they used to build. +""" + +from __future__ import annotations + +import json +from functools import lru_cache +from pathlib import Path +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from openadapt_flow.form_tx.pack_compiler import CanonicalDomainSchema + +__all__ = ["PRESET_SCHEMA", "load_domain_schema", "preset_names"] + +PRESET_SCHEMA = "openadapt.form-domain-preset/v1" +_DIR = Path(__file__).resolve().parent + + +def preset_names() -> tuple[str, ...]: + """The names of the presets shipped with this package.""" + + return tuple(sorted(p.stem for p in _DIR.glob("*.json"))) + + +@lru_cache(maxsize=None) +def _raw(name: str) -> dict[str, Any]: + if name not in preset_names(): + raise ValueError(f"unknown form_tx domain preset {name!r}") + data = json.loads((_DIR / f"{name}.json").read_text(encoding="utf-8")) + if not isinstance(data, dict) or data.get("schema") != PRESET_SCHEMA: + raise ValueError(f"preset {name!r} is not {PRESET_SCHEMA}") + preset = data.get("preset") + if not isinstance(preset, dict): + raise ValueError(f"preset {name!r} has no preset object") + return preset + + +def load_domain_schema(name: str) -> "CanonicalDomainSchema": + """The canonical domain schema stored as preset ``name``.""" + + from openadapt_flow.form_tx.pack_compiler import CanonicalDomainSchema + + return CanonicalDomainSchema.model_validate(_raw(name)) diff --git a/packages/flow/openadapt_flow/form_tx/presets/patient_registration.json b/packages/flow/openadapt_flow/form_tx/presets/patient_registration.json new file mode 100644 index 0000000000..54b7f6d37c --- /dev/null +++ b/packages/flow/openadapt_flow/form_tx/presets/patient_registration.json @@ -0,0 +1,530 @@ +{ + "schema": "openadapt.form-domain-preset/v1", + "preset": { + "schema_id": "patient_registration.v1", + "fields": [ + { + "dossier_key": "first_name", + "synonyms": [ + "first name", + "given name", + "patient first name", + "legal first name", + "fname", + "first_name", + "given_name" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "patient", + "demographics", + "personal", + "identity" + ], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "middle_name", + "synonyms": [ + "middle name", + "middle initial", + "mname", + "middle_name" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "patient", + "demographics", + "personal" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "last_name", + "synonyms": [ + "last name", + "surname", + "family name", + "patient last name", + "legal last name", + "lname", + "last_name", + "family_name" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "patient", + "demographics", + "personal", + "identity" + ], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "date_of_birth", + "synonyms": [ + "date of birth", + "birth date", + "birthdate", + "dob", + "patient dob", + "born on", + "date_of_birth", + "birth_date" + ], + "expected_codec": { + "kind": "iso_date", + "date_order": "MDY", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "patient", + "demographics", + "personal" + ], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "administrative_sex", + "synonyms": [ + "administrative sex", + "sex at birth", + "biological sex", + "legal sex", + "sex", + "gender", + "sex_at_birth", + "administrative_sex" + ], + "expected_codec": { + "kind": "enum_map", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": { + "F": [ + "female", + "f" + ], + "M": [ + "male", + "m" + ], + "X": [ + "non-binary", + "other", + "unknown", + "x", + "u" + ] + } + }, + "section_hints": [ + "patient", + "demographics", + "personal" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "phone_number", + "synonyms": [ + "phone number", + "mobile phone", + "primary phone", + "contact phone", + "cell phone", + "telephone", + "phone", + "mobile", + "tel" + ], + "expected_codec": { + "kind": "phone_digits", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "contact", + "patient", + "demographics" + ], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "email_address", + "synonyms": [ + "email address", + "email", + "patient email", + "electronic mail", + "email_address" + ], + "expected_codec": { + "kind": "casefold", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "contact", + "patient", + "demographics" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "street_address", + "synonyms": [ + "street address", + "home address", + "address line 1", + "residential address", + "street", + "address1", + "addr_line1", + "street_address" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "address", + "contact", + "residence", + "demographics" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "city", + "synonyms": [ + "city", + "town", + "municipality", + "patient_city" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "address", + "contact", + "residence" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "state_code", + "synonyms": [ + "state", + "province", + "state code", + "region", + "state_code" + ], + "expected_codec": { + "kind": "upper", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "address", + "contact", + "residence" + ], + "required": false, + "triggers_re_render_hint": true + }, + { + "dossier_key": "postal_code", + "synonyms": [ + "postal code", + "zip code", + "zip", + "postcode", + "postal_code", + "zip_code" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "address", + "contact", + "residence" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "insurance_plan_name", + "synonyms": [ + "insurance plan", + "payer name", + "coverage plan", + "health plan", + "insurance carrier", + "payer", + "insurance_plan" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "insurance", + "coverage", + "billing", + "payer" + ], + "required": false, + "triggers_re_render_hint": true + }, + { + "dossier_key": "insurance_member_id", + "synonyms": [ + "member id", + "subscriber id", + "insurance member id", + "policy member number", + "member number", + "member_id", + "subscriber_id" + ], + "expected_codec": { + "kind": "upper", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "insurance", + "coverage", + "billing", + "payer" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "emergency_contact_name", + "synonyms": [ + "emergency contact name", + "emergency contact", + "next of kin name", + "emergency_contact_name", + "emerg_name" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "emergency", + "next of kin" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "emergency_contact_phone", + "synonyms": [ + "emergency contact phone", + "emergency phone", + "next of kin phone", + "emergency_contact_phone", + "emerg_phone" + ], + "expected_codec": { + "kind": "phone_digits", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "emergency", + "next of kin" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "consent_to_treat", + "synonyms": [ + "consent to treat", + "treatment consent", + "hipaa consent", + "patient consent", + "consent_to_treat" + ], + "expected_codec": { + "kind": "boolean_yes_no", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "consent", + "authorization", + "agreement" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "full_name", + "synonyms": [ + "patient full name", + "full name", + "legal full name", + "complete name" + ], + "expected_codec": { + "kind": "title_case", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "patient", + "demographics", + "personal" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "visit_type", + "synonyms": [ + "visit category", + "visit type", + "appointment type", + "visit_type" + ], + "expected_codec": { + "kind": "casefold", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "queue", + "visit", + "routing", + "appointment" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "clinical_department", + "synonyms": [ + "clinical department", + "department", + "specialty", + "clinic department" + ], + "expected_codec": { + "kind": "casefold", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "queue", + "department", + "routing" + ], + "required": false, + "triggers_re_render_hint": true + }, + { + "dossier_key": "consulting_provider", + "synonyms": [ + "consulting doctor", + "attending physician", + "consulting provider", + "attending doctor", + "consulting_doctor" + ], + "expected_codec": { + "kind": "casefold", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "queue", + "department", + "routing", + "provider" + ], + "required": false, + "triggers_re_render_hint": false + } + ], + "expected_outputs": [ + { + "dossier_key": "medical_record_number", + "synonyms": [ + "medical record number", + "mrn", + "patient id", + "chart number", + "registration number", + "record number", + "mrn_output" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [], + "required": true, + "triggers_re_render_hint": false + } + ] + } +} diff --git a/packages/flow/tests/test_form_tx_presets.py b/packages/flow/tests/test_form_tx_presets.py new file mode 100644 index 0000000000..741a413493 --- /dev/null +++ b/packages/flow/tests/test_form_tx_presets.py @@ -0,0 +1,74 @@ +"""form_tx domain presets are data, and each shim returns what the old code built. + +Each preset moved out of ``pack_compiler.py`` into ``form_tx/presets/*.json``. +The differential test rebuilds the old builder from git history (the commit +before the move) and compares it with the shim. CI's shallow checkout has no +history, so it skips there; the round-trip tests below still run. +""" + +from __future__ import annotations + +import json +import subprocess +from pathlib import Path + +import pytest + +from openadapt_flow.form_tx import pack_compiler +from openadapt_flow.form_tx.presets import ( + PRESET_SCHEMA, + load_domain_schema, + preset_names, +) + +ROOT = Path(__file__).resolve().parents[3] +SOURCE = "packages/flow/openadapt_flow/form_tx/pack_compiler.py" + +#: preset name: (shim, the commit before the move, the old function's name) +MOVED = { + "patient_registration": ( + pack_compiler.build_patient_registration_domain_schema, + "2f506d6c463aef8098fc8fcc2fd5bc46e55c1829", + "build_patient_registration_domain_schema", + ), +} + + +def _old_builder(sha: str, name: str): # type: ignore[no-untyped-def] + got = subprocess.run( + ["git", "-C", str(ROOT), "show", f"{sha}:{SOURCE}"], + capture_output=True, + text=True, + ) + if got.returncode != 0: + pytest.skip("no git history for the commit before the move") + text = got.stdout + start = text.index(f"def {name}(") + end = text.index("\ndef ", start + 1) + namespace = dict(vars(pack_compiler)) + exec(compile(text[start:end], f"{sha}:{SOURCE}", "exec"), namespace) # noqa: S102 + return namespace[name] + + +@pytest.mark.parametrize("preset", sorted(MOVED)) +def test_the_shim_returns_what_the_old_builder_built(preset: str) -> None: + shim, sha, name = MOVED[preset] + old = _old_builder(sha, name)() + assert shim() == old + assert shim().model_dump(mode="json") == old.model_dump(mode="json") + + +@pytest.mark.parametrize("preset", sorted(MOVED)) +def test_each_moved_preset_ships_as_json(preset: str) -> None: + assert preset in preset_names() + path = Path(pack_compiler.__file__).with_name("presets") / f"{preset}.json" + data = json.loads(path.read_text(encoding="utf-8")) + assert data["schema"] == PRESET_SCHEMA + assert load_domain_schema(preset).model_dump(mode="json") == data["preset"] + + +def test_an_unknown_preset_is_refused() -> None: + with pytest.raises(ValueError): + load_domain_schema("no_such_preset") + with pytest.raises(ValueError): + load_domain_schema("../pack_compiler") From e548843204a3384a4331d855c58c30a20ac1f4d0 Mon Sep 17 00:00:00 2001 From: abrichr Date: Sat, 21 Nov 2026 22:19:10 -0500 Subject: [PATCH 2/2] refactor(form-tx): move the property insurance quote preset to JSON Like the patient registration preset, the property quote domain schema is vocabulary for one kind of form. It now ships as form_tx/presets/property_quote.json, and build_property_quote_domain_schema() loads it; the returned schema is equal to the old builder's. --- .../openadapt_flow/form_tx/pack_compiler.py | 257 +---------- .../form_tx/presets/property_quote.json | 410 ++++++++++++++++++ packages/flow/tests/test_form_tx_presets.py | 9 +- 3 files changed, 422 insertions(+), 254 deletions(-) create mode 100644 packages/flow/openadapt_flow/form_tx/presets/property_quote.json diff --git a/packages/flow/openadapt_flow/form_tx/pack_compiler.py b/packages/flow/openadapt_flow/form_tx/pack_compiler.py index acde36f3ed..9799163833 100644 --- a/packages/flow/openadapt_flow/form_tx/pack_compiler.py +++ b/packages/flow/openadapt_flow/form_tx/pack_compiler.py @@ -24,7 +24,6 @@ FieldAnchorSpec, FieldBindingSpec, FieldCodecSpec, - FormCodecKind, FormPack, FormSnapshot, ObservedControl, @@ -149,258 +148,10 @@ def build_patient_registration_domain_schema() -> CanonicalDomainSchema: def build_property_quote_domain_schema() -> CanonicalDomainSchema: - """Build generic residential/commercial property quote domain schema.""" - return CanonicalDomainSchema( - schema_id="property_quote.v1", - fields=( - CanonicalFieldSpec( - dossier_key="applicant_first_name", - synonyms=( - "applicant first name", - "insured first name", - "primary insured first name", - "first name", - "given name", - "applicant_first_name", - "insured_fname", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("applicant", "insured", "policyholder", "client"), - required=True, - ), - CanonicalFieldSpec( - dossier_key="applicant_last_name", - synonyms=( - "applicant last name", - "insured last name", - "primary insured last name", - "last name", - "surname", - "applicant_last_name", - "insured_lname", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("applicant", "insured", "policyholder", "client"), - required=True, - ), - CanonicalFieldSpec( - dossier_key="effective_date", - synonyms=( - "effective date", - "policy effective date", - "coverage start date", - "inception date", - "effective_date", - ), - expected_codec=FieldCodecSpec( - kind=FormCodecKind.ISO_DATE, date_order="MDY" - ), - section_hints=("policy", "term", "coverage", "quote"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="property_street_address", - synonyms=( - "property street address", - "property address", - "risk address", - "location address", - "dwelling address", - "street address", - "premises address", - "property_address", - "risk_street", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("property", "location", "risk", "dwelling", "premises"), - required=True, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="property_city", - synonyms=( - "property city", - "risk city", - "location city", - "city", - "property_city", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("property", "location", "risk", "dwelling"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="property_state", - synonyms=( - "property state", - "risk state", - "location state", - "state", - "province", - "property_state", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.UPPER), - section_hints=("property", "location", "risk", "dwelling"), - required=False, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="property_postal_code", - synonyms=( - "property postal code", - "property zip", - "risk zip", - "location zip", - "postal code", - "zip code", - "zip", - "property_postal_code", - "risk_zip", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - section_hints=("property", "location", "risk", "dwelling"), - required=True, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="year_built", - synonyms=( - "year built", - "construction year", - "yr built", - "year of construction", - "year_built", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.INTEGER), - section_hints=( - "construction", - "building", - "dwelling", - "property", - "risk", - ), - required=True, - ), - CanonicalFieldSpec( - dossier_key="square_footage", - synonyms=( - "square footage", - "total square feet", - "living area", - "heated sq ft", - "sq ft", - "sqft", - "square_footage", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.INTEGER), - section_hints=("construction", "building", "dwelling", "property"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="construction_type", - synonyms=( - "construction type", - "frame type", - "building construction", - "structure type", - "exterior construction", - "construction_type", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CASEFOLD), - section_hints=("construction", "building", "dwelling"), - required=False, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="roof_material", - synonyms=( - "roof material", - "roof type", - "roof covering", - "roofing", - "roof_material", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CASEFOLD), - section_hints=("construction", "roof", "building", "dwelling"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="dwelling_coverage_amount", - synonyms=( - "dwelling coverage amount", - "dwelling coverage", - "coverage a limit", - "coverage a", - "replacement cost", - "building limit", - "insured value", - "dwelling limit", - "dwelling_coverage", - "coverage_amount", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CURRENCY_CENTS), - section_hints=("coverage", "limits", "rating", "valuation"), - required=True, - triggers_re_render_hint=True, - ), - CanonicalFieldSpec( - dossier_key="deductible_amount", - synonyms=( - "deductible amount", - "all peril deductible", - "policy deductible", - "deductible", - "deductible_amount", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CURRENCY_CENTS), - section_hints=("coverage", "limits", "deductible", "rating"), - required=False, - ), - CanonicalFieldSpec( - dossier_key="monitored_alarm", - synonyms=( - "monitored alarm", - "central station alarm", - "protective alarm", - "security alarm", - "burglar alarm", - "monitored_alarm", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.BOOLEAN_YES_NO), - section_hints=("discounts", "protection", "safety", "property"), - required=False, - ), - ), - expected_outputs=( - CanonicalFieldSpec( - dossier_key="quote_number", - synonyms=( - "quote number", - "quote id", - "proposal number", - "quote reference", - "quote_number", - "quote_id", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.WHITESPACE_COLLAPSE), - required=True, - ), - CanonicalFieldSpec( - dossier_key="annual_premium", - synonyms=( - "annual premium", - "total premium", - "quoted premium", - "estimated annual premium", - "premium", - "annual_premium", - "quoted_premium", - ), - expected_codec=FieldCodecSpec(kind=FormCodecKind.CURRENCY_CENTS), - required=True, - ), - ), - ) + """The property insurance quote preset (``presets/property_quote.json``).""" + from openadapt_flow.form_tx.presets import load_domain_schema + + return load_domain_schema("property_quote") def _scrub_snapshot_values(snapshot: FormSnapshot) -> FormSnapshot: diff --git a/packages/flow/openadapt_flow/form_tx/presets/property_quote.json b/packages/flow/openadapt_flow/form_tx/presets/property_quote.json new file mode 100644 index 0000000000..a4aedef7ba --- /dev/null +++ b/packages/flow/openadapt_flow/form_tx/presets/property_quote.json @@ -0,0 +1,410 @@ +{ + "schema": "openadapt.form-domain-preset/v1", + "preset": { + "schema_id": "property_quote.v1", + "fields": [ + { + "dossier_key": "applicant_first_name", + "synonyms": [ + "applicant first name", + "insured first name", + "primary insured first name", + "first name", + "given name", + "applicant_first_name", + "insured_fname" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "applicant", + "insured", + "policyholder", + "client" + ], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "applicant_last_name", + "synonyms": [ + "applicant last name", + "insured last name", + "primary insured last name", + "last name", + "surname", + "applicant_last_name", + "insured_lname" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "applicant", + "insured", + "policyholder", + "client" + ], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "effective_date", + "synonyms": [ + "effective date", + "policy effective date", + "coverage start date", + "inception date", + "effective_date" + ], + "expected_codec": { + "kind": "iso_date", + "date_order": "MDY", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "policy", + "term", + "coverage", + "quote" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "property_street_address", + "synonyms": [ + "property street address", + "property address", + "risk address", + "location address", + "dwelling address", + "street address", + "premises address", + "property_address", + "risk_street" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "property", + "location", + "risk", + "dwelling", + "premises" + ], + "required": true, + "triggers_re_render_hint": true + }, + { + "dossier_key": "property_city", + "synonyms": [ + "property city", + "risk city", + "location city", + "city", + "property_city" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "property", + "location", + "risk", + "dwelling" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "property_state", + "synonyms": [ + "property state", + "risk state", + "location state", + "state", + "province", + "property_state" + ], + "expected_codec": { + "kind": "upper", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "property", + "location", + "risk", + "dwelling" + ], + "required": false, + "triggers_re_render_hint": true + }, + { + "dossier_key": "property_postal_code", + "synonyms": [ + "property postal code", + "property zip", + "risk zip", + "location zip", + "postal code", + "zip code", + "zip", + "property_postal_code", + "risk_zip" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "property", + "location", + "risk", + "dwelling" + ], + "required": true, + "triggers_re_render_hint": true + }, + { + "dossier_key": "year_built", + "synonyms": [ + "year built", + "construction year", + "yr built", + "year of construction", + "year_built" + ], + "expected_codec": { + "kind": "integer", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "construction", + "building", + "dwelling", + "property", + "risk" + ], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "square_footage", + "synonyms": [ + "square footage", + "total square feet", + "living area", + "heated sq ft", + "sq ft", + "sqft", + "square_footage" + ], + "expected_codec": { + "kind": "integer", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "construction", + "building", + "dwelling", + "property" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "construction_type", + "synonyms": [ + "construction type", + "frame type", + "building construction", + "structure type", + "exterior construction", + "construction_type" + ], + "expected_codec": { + "kind": "casefold", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "construction", + "building", + "dwelling" + ], + "required": false, + "triggers_re_render_hint": true + }, + { + "dossier_key": "roof_material", + "synonyms": [ + "roof material", + "roof type", + "roof covering", + "roofing", + "roof_material" + ], + "expected_codec": { + "kind": "casefold", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "construction", + "roof", + "building", + "dwelling" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "dwelling_coverage_amount", + "synonyms": [ + "dwelling coverage amount", + "dwelling coverage", + "coverage a limit", + "coverage a", + "replacement cost", + "building limit", + "insured value", + "dwelling limit", + "dwelling_coverage", + "coverage_amount" + ], + "expected_codec": { + "kind": "currency_cents", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "coverage", + "limits", + "rating", + "valuation" + ], + "required": true, + "triggers_re_render_hint": true + }, + { + "dossier_key": "deductible_amount", + "synonyms": [ + "deductible amount", + "all peril deductible", + "policy deductible", + "deductible", + "deductible_amount" + ], + "expected_codec": { + "kind": "currency_cents", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "coverage", + "limits", + "deductible", + "rating" + ], + "required": false, + "triggers_re_render_hint": false + }, + { + "dossier_key": "monitored_alarm", + "synonyms": [ + "monitored alarm", + "central station alarm", + "protective alarm", + "security alarm", + "burglar alarm", + "monitored_alarm" + ], + "expected_codec": { + "kind": "boolean_yes_no", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [ + "discounts", + "protection", + "safety", + "property" + ], + "required": false, + "triggers_re_render_hint": false + } + ], + "expected_outputs": [ + { + "dossier_key": "quote_number", + "synonyms": [ + "quote number", + "quote id", + "proposal number", + "quote reference", + "quote_number", + "quote_id" + ], + "expected_codec": { + "kind": "whitespace_collapse", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [], + "required": true, + "triggers_re_render_hint": false + }, + { + "dossier_key": "annual_premium", + "synonyms": [ + "annual premium", + "total premium", + "quoted premium", + "estimated annual premium", + "premium", + "annual_premium", + "quoted_premium" + ], + "expected_codec": { + "kind": "currency_cents", + "date_order": "YMD", + "currency_symbol": null, + "enum_aliases": {} + }, + "section_hints": [], + "required": true, + "triggers_re_render_hint": false + } + ] + } +} diff --git a/packages/flow/tests/test_form_tx_presets.py b/packages/flow/tests/test_form_tx_presets.py index 741a413493..78217b9775 100644 --- a/packages/flow/tests/test_form_tx_presets.py +++ b/packages/flow/tests/test_form_tx_presets.py @@ -15,6 +15,7 @@ import pytest from openadapt_flow.form_tx import pack_compiler +from openadapt_flow.form_tx import types as form_types from openadapt_flow.form_tx.presets import ( PRESET_SCHEMA, load_domain_schema, @@ -31,6 +32,11 @@ "2f506d6c463aef8098fc8fcc2fd5bc46e55c1829", "build_patient_registration_domain_schema", ), + "property_quote": ( + pack_compiler.build_property_quote_domain_schema, + "2f506d6c463aef8098fc8fcc2fd5bc46e55c1829", + "build_property_quote_domain_schema", + ), } @@ -45,7 +51,8 @@ def _old_builder(sha: str, name: str): # type: ignore[no-untyped-def] text = got.stdout start = text.index(f"def {name}(") end = text.index("\ndef ", start + 1) - namespace = dict(vars(pack_compiler)) + # The old builder named form_tx types that pack_compiler may no longer import. + namespace = {**vars(form_types), **vars(pack_compiler)} exec(compile(text[start:end], f"{sha}:{SOURCE}", "exec"), namespace) # noqa: S102 return namespace[name]