diff --git a/changelog.d/d1-9-required-report-type.changed.md b/changelog.d/d1-9-required-report-type.changed.md new file mode 100644 index 000000000..af0a79468 --- /dev/null +++ b/changelog.d/d1-9-required-report-type.changed.md @@ -0,0 +1 @@ +Sim's model of `reports` declares `type` as required, with API v1's check `ck_reports_type`: a report's type is `economy`, `household` or `budget_window`, `household` exactly when it names a household and `budget_window` exactly when it has a window size. The API v1 commit the models are compared with moves to the one that adds them. diff --git a/libs/policyengine-simulation-persistence/README.md b/libs/policyengine-simulation-persistence/README.md index c046d8729..2cb3c3f86 100644 --- a/libs/policyengine-simulation-persistence/README.md +++ b/libs/policyengine-simulation-persistence/README.md @@ -86,9 +86,12 @@ year's report run names that row and its year, and belongs to the same report. A report's `type` names its kind: `economy` for a single-year economy report (`ECONOMY_REPORT_TYPE`), `budget_window` for a budget window (`BUDGET_WINDOW_REPORT_TYPE`) and `household` for a household report -(`HOUSEHOLD_REPORT_TYPE`). The service writes it on every report it creates; -a report's definition key keeps its own `report_type`, `economy` for a budget -window too, so the type changes no key. +(`HOUSEHOLD_REPORT_TYPE`). The service writes it on every report it creates, +and API v1's table requires it: `ck_reports_type` allows only those three, and +requires `household` exactly when the report names a household and +`budget_window` exactly when it has a `budget_window_size`. A report's +definition key keeps its own `report_type`, `economy` for a budget window too, +so the type changes no key. `CliffImpact` is one cliff calculation of a simulation run. The service inserts a row pending and records the Modal call it starts; the Modal worker role claims @@ -308,9 +311,13 @@ version, dataset and region it calculates on. record calculates: its kind (`economy`, `budget_window`, `household` or `unsupported`), country, year, its region's code and dataset's name, and each simulation's policy and dynamic. A report's kind is its `type`; a - type that disagrees with its window size or household, such as a budget - window stored as `economy` before the service wrote `budget_window`, is - `unsupported`, so it is not re-run. + type the service doesn't write, no type, or a type that disagrees with its + window size or household, such as a budget window stored as `economy` before + the service wrote `budget_window`, is `unsupported`, so it is not re-run. + Such a row exists only where API v1's revision `24b571d7d079` has not run + yet: it sets every report whose type differs from the one its household and + window size give to that type, and its check `ck_reports_type` refuses such + a row from then on. - `insert_report_rerun` joins the report's run on the re-run's versions that hasn't failed and wasn't forced (pending, running or succeeded), creating nothing, or else creates the report's new run, each side joining diff --git a/libs/policyengine-simulation-persistence/public-api-commit b/libs/policyengine-simulation-persistence/public-api-commit index 0495d55fa..b46e921b9 100644 --- a/libs/policyengine-simulation-persistence/public-api-commit +++ b/libs/policyengine-simulation-persistence/public-api-commit @@ -1 +1 @@ -41fc0151aac124aaf8829ba7f98bb0ad9a8898ad +57202c04991ec4239ef135dc80d1e223a9e6b076 diff --git a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/tables.py b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/tables.py index b9252d652..9f2fa7e03 100644 --- a/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/tables.py +++ b/libs/policyengine-simulation-persistence/src/policyengine_simulation_persistence/tables.py @@ -262,7 +262,13 @@ class Simulation(ServiceBase): class Report(ServiceBase): - """What one report compares; its calculations are ``ReportRun`` rows.""" + """What one report compares; its calculations are ``ReportRun`` rows. + + ``type`` names what it compares: ``economy``, ``budget_window`` or + ``household``. The service writes it on every report, and + ``ck_reports_type`` keeps it in step with ``household_id`` and + ``budget_window_size``. + """ __tablename__ = "reports" __table_args__: ClassVar = ( @@ -315,6 +321,17 @@ class Report(ServiceBase): f"year IS NOT NULL AND year + budget_window_size - 1 <= {LATEST_YEAR})", name="ck_reports_budget_window_size", ), + # API v1's check: a report's type is one of the three the service + # writes (``ECONOMY_REPORT_TYPE``, ``HOUSEHOLD_REPORT_TYPE`` and + # ``BUDGET_WINDOW_REPORT_TYPE``), ``household`` exactly when the + # report names a household, and ``budget_window`` exactly when it has + # a window size. + CheckConstraint( + "type IN ('economy', 'household', 'budget_window') " + "AND (type = 'household') = (household_id IS NOT NULL) " + "AND (type = 'budget_window') = (budget_window_size IS NOT NULL)", + name="ck_reports_type", + ), _year_between("reports", nullable=True), *_key_checks("reports", "definition_key", "dataset_independent_key"), CheckConstraint( @@ -339,7 +356,7 @@ class Report(ServiceBase): created_at: Mapped[datetime] = server_timestamp_column() updated_at: Mapped[datetime] = server_timestamp_column() country: Mapped[str] = mapped_column(String(16)) - type: Mapped[str | None] = mapped_column(String(128)) + type: Mapped[str] = mapped_column(String(128)) tax_benefit_model_id: Mapped[UUID] = mapped_column(Uuid) policy_id: Mapped[UUID | None] = mapped_column(Uuid) baseline_simulation_id: Mapped[UUID | None] = mapped_column(Uuid) diff --git a/libs/policyengine-simulation-persistence/tests/economy_rows.py b/libs/policyengine-simulation-persistence/tests/economy_rows.py index 40028c3f0..f7d5e1800 100644 --- a/libs/policyengine-simulation-persistence/tests/economy_rows.py +++ b/libs/policyengine-simulation-persistence/tests/economy_rows.py @@ -274,7 +274,7 @@ def insert_report_of( # noqa: PLR0913 # the report's sides and what else it ma inputs: dict[str, Any] | None = None, budget_window_size: int | None = None, household_id: UUID | None = None, - report_type: str | None = "economy", + report_type: str = "economy", ) -> UUID: """Insert an economy report naming the two runs' simulations, as the superuser. diff --git a/libs/policyengine-simulation-persistence/tests/service_rows.py b/libs/policyengine-simulation-persistence/tests/service_rows.py index d19acf936..d41667c6d 100644 --- a/libs/policyengine-simulation-persistence/tests/service_rows.py +++ b/libs/policyengine-simulation-persistence/tests/service_rows.py @@ -163,6 +163,7 @@ def insert_service_rows( insert(Report) .values( country="us", + type="budget_window", tax_benefit_model_id=public_api_rows.tax_benefit_model_id, baseline_simulation_id=baseline_simulation_id, reform_simulation_id=reform_simulation_id, diff --git a/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py b/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py index 2f37920a3..be8a88b9c 100644 --- a/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py +++ b/libs/policyengine-simulation-persistence/tests/test_economy_reruns.py @@ -209,54 +209,6 @@ def test_a_budget_window_or_household_report_is_read_as_its_stored_type( assert (window.kind, household.kind) == ("budget_window", "household") -@pytest.mark.parametrize( - ("report_type", "budget_window_size", "names_a_household"), - [ - # A budget window stored before sim wrote ``budget_window``, until - # API v1's revision rewrites it. - pytest.param("economy", 3, False, id="economy-with-a-window"), - pytest.param("budget_window", None, False, id="window-without-a-window"), - pytest.param("household", None, False, id="household-without-a-household"), - pytest.param("economy", None, True, id="economy-with-a-household"), - pytest.param(None, None, True, id="no-type-with-a-household"), - ], -) -def test_a_report_whose_type_disagrees_with_its_columns_is_unsupported( - public_api_database_url, - report_type: str | None, - budget_window_size: int | None, - names_a_household: bool, # noqa: FBT001 # a parametrized case's flag -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - population = economy.population() - sides = ( - insert_simulation_run(connection, population), - insert_simulation_run(connection, population), - ) - household_id = ( - insert_household( - connection, country="us", default_year=2026, document={"people": {}} - ) - if names_a_household - else None - ) - report_id = insert_report_of( - connection, - economy, - sides, - budget_window_size=budget_window_size, - household_id=household_id, - report_type=report_type, - ) - act_as_the_service(connection) - - definition = in_session(connection, read_report_definition, report_id) - - assert definition is not None - assert definition.kind == "unsupported" - - def test_an_unknown_report_or_simulation_has_no_definition( public_api_database_url, ) -> None: @@ -831,24 +783,6 @@ def _insert_catalog_of(connection: Connection, model_id: UUID) -> EconomyCatalog return EconomyCatalog(catalog=catalog, dataset_id=dataset_id, region_id=region_id) -def test_a_report_of_another_type_is_read_as_unsupported( - public_api_database_url, -) -> None: - with rolled_back_transaction(public_api_database_url) as connection: - economy = insert_economy_catalog(connection) - stored = _stored_report(connection, economy) - connection.execute( - text("UPDATE public.reports SET type = 'custom_total' WHERE id = :id"), - {"id": stored.report_id}, - ) - act_as_the_service(connection) - - definition = in_session(connection, read_report_definition, stored.report_id) - - assert definition is not None - assert definition.kind == "unsupported" - - def test_a_rerun_of_a_deleted_report_or_simulation_says_the_record_is_gone( public_api_database_url, ) -> None: diff --git a/libs/policyengine-simulation-persistence/tests/test_report_kinds.py b/libs/policyengine-simulation-persistence/tests/test_report_kinds.py new file mode 100644 index 000000000..f63af0256 --- /dev/null +++ b/libs/policyengine-simulation-persistence/tests/test_report_kinds.py @@ -0,0 +1,49 @@ +"""How a re-run reads a stored report's kind from its ``reports.type``. + +A report of a type the service doesn't write, or whose type disagrees with its +window size or household, is ``unsupported``, so it is not re-run. API v1's +``ck_reports_type`` refuses such a row once its revision ``24b571d7d079`` has +run, so these tests call the function that reads the kind directly, with no +database. Until that revision runs, a budget window the service stored before +it wrote ``budget_window`` still names ``economy``, and a report may have no +type or another one. +""" + +from __future__ import annotations + +from uuid import UUID + +import pytest +from policyengine_simulation_persistence.rerun_definitions import _report_kind + +HOUSEHOLD_ID = UUID("33333333-4444-5555-6666-777777777777") + + +@pytest.mark.parametrize( + ("report_type", "budget_window_size", "household_id"), + [ + # A budget window stored before the service wrote ``budget_window``, + # until API v1's revision rewrites it. + pytest.param("economy", 3, None, id="economy-with-a-window"), + pytest.param("budget_window", None, None, id="window-without-a-window"), + pytest.param("household", None, None, id="household-without-a-household"), + pytest.param("economy", None, HOUSEHOLD_ID, id="economy-with-a-household"), + pytest.param(None, None, HOUSEHOLD_ID, id="no-type-with-a-household"), + ], +) +def test_a_report_whose_type_disagrees_with_its_columns_is_unsupported( + report_type: str | None, + budget_window_size: int | None, + household_id: UUID | None, +) -> None: + assert _report_kind(report_type, household_id, budget_window_size) == ( + "unsupported" + ) + + +def test_a_report_of_another_type_is_read_as_unsupported() -> None: + assert _report_kind("custom_total", None, None) == "unsupported" + + +def test_a_report_with_no_type_is_read_as_unsupported() -> None: + assert _report_kind(None, None, None) == "unsupported" diff --git a/libs/policyengine-stage12-persistence/tests/budget_window_database.py b/libs/policyengine-stage12-persistence/tests/budget_window_database.py index 11e7ecb7b..5dff1b644 100644 --- a/libs/policyengine-stage12-persistence/tests/budget_window_database.py +++ b/libs/policyengine-stage12-persistence/tests/budget_window_database.py @@ -89,6 +89,7 @@ def seed_window(self, first_year: int = 2026, size: int = 7) -> SeededWindow: insert(table("reports")).values( id=report_id, country="us", + type="budget_window", tax_benefit_model_id=model_id, baseline_simulation_id=simulation_ids[0], reform_simulation_id=simulation_ids[1], diff --git a/libs/policyengine-stage12-persistence/tests/report_run_database.py b/libs/policyengine-stage12-persistence/tests/report_run_database.py index 450e78466..de5dcb610 100644 --- a/libs/policyengine-stage12-persistence/tests/report_run_database.py +++ b/libs/policyengine-stage12-persistence/tests/report_run_database.py @@ -127,6 +127,7 @@ def seed_report_run(self) -> SeededReportRun: insert(self.table("reports")).values( id=report_id, country="us", + type="economy", tax_benefit_model_id=model_id, baseline_simulation_id=simulation_ids[0], reform_simulation_id=simulation_ids[1],