|
| 1 | +import json |
| 2 | +import time |
| 3 | +from http import HTTPStatus |
| 4 | + |
1 | 5 | import httpx |
2 | 6 | import pytest |
3 | 7 | from pydantic_settings import BaseSettings, SettingsConfigDict |
4 | 8 |
|
5 | 9 | from policyengine_api_simulation_client import AuthenticatedClient, Client |
| 10 | +from policyengine_api_simulation_client.api.default import ( |
| 11 | + get_budget_window_job_status_budget_window_jobs_batch_job_id_get, |
| 12 | + submit_budget_window_batch_simulate_economy_budget_window_post, |
| 13 | +) |
| 14 | +from policyengine_api_simulation_client.models import ( |
| 15 | + BudgetWindowBatchRequest, |
| 16 | + BudgetWindowBatchStatusResponse, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +BUDGET_WINDOW_YEARS = ["2026", "2027"] |
| 21 | +BUDGET_WINDOW_REFORM = { |
| 22 | + "gov.irs.credits.ctc.refundable.fully_refundable": {"2023-01-01.2100-12-31": True} |
| 23 | +} |
| 24 | +BUDGET_WINDOW_DATASET = "gs://policyengine-us-data/enhanced_cps_2024.h5" |
| 25 | +BUDGET_WINDOW_REGION = "us" |
| 26 | +BUDGET_WINDOW_SUBSAMPLE = 200 |
| 27 | +BUDGET_WINDOW_MAX_PARALLEL = 2 |
6 | 28 |
|
7 | 29 |
|
8 | 30 | class Settings(BaseSettings): |
@@ -49,3 +71,122 @@ def poll_interval() -> float: |
49 | 71 | def max_wait_seconds() -> float: |
50 | 72 | """Return max wait time in seconds.""" |
51 | 73 | return settings.timeout_in_millis / 1000 |
| 74 | + |
| 75 | + |
| 76 | +def _decode_response_content(content: bytes) -> str: |
| 77 | + try: |
| 78 | + return json.dumps(json.loads(content), sort_keys=True) |
| 79 | + except (json.JSONDecodeError, UnicodeDecodeError): |
| 80 | + return content.decode("utf-8", errors="replace") |
| 81 | + |
| 82 | + |
| 83 | +def _poll_budget_window_batch( |
| 84 | + *, |
| 85 | + client: Client | AuthenticatedClient, |
| 86 | + batch_job_id: str, |
| 87 | + max_wait_seconds: float, |
| 88 | + poll_interval: float, |
| 89 | +) -> BudgetWindowBatchStatusResponse: |
| 90 | + deadline = time.monotonic() + max_wait_seconds |
| 91 | + last_status_code: HTTPStatus | None = None |
| 92 | + last_content = b"" |
| 93 | + |
| 94 | + while time.monotonic() < deadline: |
| 95 | + response = get_budget_window_job_status_budget_window_jobs_batch_job_id_get.sync_detailed( |
| 96 | + batch_job_id=batch_job_id, client=client |
| 97 | + ) |
| 98 | + last_status_code = response.status_code |
| 99 | + last_content = response.content |
| 100 | + |
| 101 | + if response.status_code == HTTPStatus.ACCEPTED: |
| 102 | + time.sleep(poll_interval) |
| 103 | + continue |
| 104 | + |
| 105 | + if response.status_code == HTTPStatus.OK: |
| 106 | + assert isinstance(response.parsed, BudgetWindowBatchStatusResponse), ( |
| 107 | + f"Unexpected response type: {type(response.parsed)}" |
| 108 | + ) |
| 109 | + assert response.parsed.status == "complete", ( |
| 110 | + f"Unexpected budget-window status: {response.parsed}" |
| 111 | + ) |
| 112 | + return response.parsed |
| 113 | + |
| 114 | + if response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR: |
| 115 | + raise AssertionError( |
| 116 | + "Budget-window batch failed: " |
| 117 | + f"{_decode_response_content(response.content)}" |
| 118 | + ) |
| 119 | + |
| 120 | + raise AssertionError( |
| 121 | + "Unexpected budget-window poll status " |
| 122 | + f"{response.status_code}: {_decode_response_content(response.content)}" |
| 123 | + ) |
| 124 | + |
| 125 | + raise TimeoutError( |
| 126 | + f"Budget-window batch {batch_job_id} did not complete within " |
| 127 | + f"{max_wait_seconds}s; last response was " |
| 128 | + f"{last_status_code}: {_decode_response_content(last_content)}" |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +@pytest.fixture() |
| 133 | +def budget_window_years() -> list[str]: |
| 134 | + """Return the annual rows expected from the staging budget-window smoke run.""" |
| 135 | + return list(BUDGET_WINDOW_YEARS) |
| 136 | + |
| 137 | + |
| 138 | +@pytest.fixture() |
| 139 | +def budget_window_request(us_model_version: str) -> BudgetWindowBatchRequest: |
| 140 | + """Build the standard staging budget-window smoke request.""" |
| 141 | + return BudgetWindowBatchRequest.from_dict( |
| 142 | + { |
| 143 | + "country": "us", |
| 144 | + "version": us_model_version, |
| 145 | + "region": BUDGET_WINDOW_REGION, |
| 146 | + "scope": "macro", |
| 147 | + "reform": BUDGET_WINDOW_REFORM, |
| 148 | + "subsample": BUDGET_WINDOW_SUBSAMPLE, |
| 149 | + "data": BUDGET_WINDOW_DATASET, |
| 150 | + "start_year": BUDGET_WINDOW_YEARS[0], |
| 151 | + "window_size": len(BUDGET_WINDOW_YEARS), |
| 152 | + "max_parallel": BUDGET_WINDOW_MAX_PARALLEL, |
| 153 | + } |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +@pytest.fixture() |
| 158 | +def decode_response_content(): |
| 159 | + """Return a compact formatter for non-OK HTTP response payloads.""" |
| 160 | + return _decode_response_content |
| 161 | + |
| 162 | + |
| 163 | +@pytest.fixture() |
| 164 | +def submit_budget_window_batch(client: Client | AuthenticatedClient): |
| 165 | + """Submit a budget-window batch through the generated client.""" |
| 166 | + |
| 167 | + def submit(request: BudgetWindowBatchRequest): |
| 168 | + return submit_budget_window_batch_simulate_economy_budget_window_post.sync_detailed( |
| 169 | + client=client, |
| 170 | + body=request, |
| 171 | + ) |
| 172 | + |
| 173 | + return submit |
| 174 | + |
| 175 | + |
| 176 | +@pytest.fixture() |
| 177 | +def poll_budget_window_batch( |
| 178 | + client: Client | AuthenticatedClient, |
| 179 | + max_wait_seconds: float, |
| 180 | + poll_interval: float, |
| 181 | +): |
| 182 | + """Poll a budget-window batch through the generated client.""" |
| 183 | + |
| 184 | + def poll(batch_job_id: str) -> BudgetWindowBatchStatusResponse: |
| 185 | + return _poll_budget_window_batch( |
| 186 | + client=client, |
| 187 | + batch_job_id=batch_job_id, |
| 188 | + max_wait_seconds=max_wait_seconds, |
| 189 | + poll_interval=poll_interval, |
| 190 | + ) |
| 191 | + |
| 192 | + return poll |
0 commit comments