|
| 1 | +"""Migration tests comparing PHP and Python API responses for run trace endpoints.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from http import HTTPStatus |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +import deepdiff |
| 8 | +import httpx |
| 9 | +import pytest |
| 10 | + |
| 11 | +from core.conversions import nested_num_to_str |
| 12 | + |
| 13 | +_SERVER_RUNS = [*range(24, 40), *range(134, 140), 999_999_999] |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("run_id", _SERVER_RUNS) |
| 17 | +async def test_get_run_trace_equal( |
| 18 | + run_id: int, |
| 19 | + py_api: httpx.AsyncClient, |
| 20 | + php_api: httpx.AsyncClient, |
| 21 | +) -> None: |
| 22 | + """Test that Python and PHP run trace responses are equivalent after normalization.""" |
| 23 | + py_response, php_response = await asyncio.gather( |
| 24 | + py_api.get(f"/run/trace/{run_id}"), |
| 25 | + php_api.get(f"/run/trace/{run_id}"), |
| 26 | + ) |
| 27 | + if php_response.status_code == HTTPStatus.OK: |
| 28 | + _assert_trace_response_success(py_response, php_response) |
| 29 | + return |
| 30 | + |
| 31 | + assert php_response.status_code == HTTPStatus.PRECONDITION_FAILED |
| 32 | + assert py_response.status_code == HTTPStatus.NOT_FOUND |
| 33 | + |
| 34 | + php_error = php_response.json()["error"] |
| 35 | + py_error = py_response.json() |
| 36 | + assert php_error["code"] == py_error["code"] |
| 37 | + if php_error["code"] == "571": |
| 38 | + assert php_error["message"] == "Run not found." |
| 39 | + assert py_error["detail"] == f"Run {run_id} not found." |
| 40 | + elif php_error["code"] == "572": |
| 41 | + assert php_error["message"] == "No successful trace associated with this run." |
| 42 | + assert py_error["detail"] == f"No trace found for run {run_id}." |
| 43 | + else: |
| 44 | + msg = f"Unknown error code {php_error['code']} for run {run_id}." |
| 45 | + raise AssertionError(msg) |
| 46 | + |
| 47 | + |
| 48 | +def _assert_trace_response_success( |
| 49 | + py_response: httpx.Response, php_response: httpx.Response |
| 50 | +) -> None: |
| 51 | + assert py_response.status_code == HTTPStatus.OK |
| 52 | + assert php_response.status_code == HTTPStatus.OK |
| 53 | + |
| 54 | + new_json = py_response.json() |
| 55 | + |
| 56 | + # PHP nests response under "trace" key — match that structure |
| 57 | + new_json = {"trace": new_json} |
| 58 | + |
| 59 | + # PHP uses "trace_iteration" key, Python uses "trace" |
| 60 | + new_json["trace"]["trace_iteration"] = new_json["trace"].pop("trace") |
| 61 | + |
| 62 | + # PHP returns all numeric values as strings — normalize Python response |
| 63 | + new_json = nested_num_to_str(new_json) |
| 64 | + |
| 65 | + def _sort_trace(payload: dict[str, Any]) -> dict[str, Any]: |
| 66 | + """Sort trace iterations by (repeat, fold, iteration) for order-sensitive comparison.""" |
| 67 | + copied = payload.copy() |
| 68 | + copied["trace"] = copied["trace"].copy() |
| 69 | + copied["trace"]["trace_iteration"] = sorted( |
| 70 | + copied["trace"]["trace_iteration"], |
| 71 | + key=lambda row: (int(row["repeat"]), int(row["fold"]), int(row["iteration"])), |
| 72 | + ) |
| 73 | + return copied |
| 74 | + |
| 75 | + differences = deepdiff.diff.DeepDiff( |
| 76 | + _sort_trace(new_json), |
| 77 | + _sort_trace(php_response.json()), |
| 78 | + ignore_order=False, |
| 79 | + ) |
| 80 | + assert not differences |
0 commit comments