|
5 | 5 | from uuid import uuid4 |
6 | 6 |
|
7 | 7 | import pytest |
8 | | -from policyengine_simulation_persistence import ReportKind, ReportRecord |
| 8 | +from policyengine_simulation_persistence import ( |
| 9 | + ReportKind, |
| 10 | + ReportRecord, |
| 11 | + create_simulation_engine, |
| 12 | +) |
| 13 | +from policyengine_simulation_persistence.reports import reports |
9 | 14 | from public_api_support import superuser_engine |
10 | 15 | from report_support import ( |
11 | 16 | economy_pair, |
|
14 | 19 | report_store, |
15 | 20 | ) |
16 | 21 | from simulation_support import economy_simulation, simulation_store |
17 | | -from sqlalchemy import text |
| 22 | +from sqlalchemy import insert, text |
18 | 23 | from sqlalchemy.exc import IntegrityError |
19 | 24 |
|
20 | 25 | pytestmark = pytest.mark.postgres |
@@ -188,3 +193,37 @@ def test_a_migrated_report_needs_its_legacy_id( |
188 | 193 |
|
189 | 194 | def test_an_unknown_report_reads_as_none(migrated_database) -> None: |
190 | 195 | assert report_store(migrated_database).get(uuid4()) is None |
| 196 | + |
| 197 | + |
| 198 | +@pytest.mark.parametrize( |
| 199 | + ("column", "value", "constraint"), |
| 200 | + [ |
| 201 | + ("kind", "microsimulation", "ck_reports_kind"), |
| 202 | + ("country", "GB", "ck_reports_country"), |
| 203 | + ("legacy_report_id", -4, "ck_reports_legacy_report_id"), |
| 204 | + ("reform_simulation_id", None, "ck_reports_economy_has_reform"), |
| 205 | + ], |
| 206 | +) |
| 207 | +def test_the_database_checks_each_report_field( |
| 208 | + migrated_database, public_api_rows, column, value, constraint |
| 209 | +) -> None: |
| 210 | + """The database checks what NewReport also checks, for any other writer.""" |
| 211 | + baseline, reform = economy_pair(migrated_database, public_api_rows) |
| 212 | + values = { |
| 213 | + "id": uuid4(), |
| 214 | + "kind": "economy", |
| 215 | + "country": "us", |
| 216 | + "baseline_simulation_id": baseline.id, |
| 217 | + "reform_simulation_id": reform.id, |
| 218 | + "year": 2026, |
| 219 | + column: value, |
| 220 | + } |
| 221 | + engine = create_simulation_engine(migrated_database.runtime_url) |
| 222 | + try: |
| 223 | + with ( |
| 224 | + pytest.raises(IntegrityError, match=constraint), |
| 225 | + engine.begin() as connection, |
| 226 | + ): |
| 227 | + connection.execute(insert(reports).values(**values)) |
| 228 | + finally: |
| 229 | + engine.dispose() |
0 commit comments