test: lock training workflow api contract
This commit is contained in:
@@ -60,6 +60,8 @@ def route_request(
|
||||
if _is_training_progress_post(method, path):
|
||||
_validate_progress_identifier_body(body or {})
|
||||
return 404, {"error": "not_found"}
|
||||
if _is_training_attempt_create_post(method, path):
|
||||
_validate_attempt_create_body(body or {})
|
||||
|
||||
deps = dependencies or build_api_dependencies()
|
||||
|
||||
@@ -254,6 +256,10 @@ def _is_training_progress_post(method: str, path: str) -> bool:
|
||||
return method == "POST" and path == "/training/progress"
|
||||
|
||||
|
||||
def _is_training_attempt_create_post(method: str, path: str) -> bool:
|
||||
return method == "POST" and path == "/training/attempts"
|
||||
|
||||
|
||||
def _path_parts(path: str) -> list[str]:
|
||||
return [part for part in path.strip("/").split("/") if part]
|
||||
|
||||
@@ -284,6 +290,27 @@ def _validate_progress_identifier_body(body: dict) -> None:
|
||||
_parse_body_identifier(body[key], key)
|
||||
|
||||
|
||||
def _validate_attempt_create_body(body: dict) -> None:
|
||||
_parse_required_json_identifier(body, "case_id")
|
||||
|
||||
|
||||
def _parse_required_json_identifier(body: dict, key: str) -> int:
|
||||
if not isinstance(body, dict):
|
||||
raise ValueError("invalid_json_body")
|
||||
if body.get(key) is None:
|
||||
raise ValueError(f"missing_required_json_field:{key}")
|
||||
value = body[key]
|
||||
if isinstance(value, bool):
|
||||
raise ValueError(f"invalid_json_field:{key}")
|
||||
try:
|
||||
parsed = int(value)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ValueError(f"invalid_json_field:{key}") from exc
|
||||
if parsed < 1:
|
||||
raise ValueError(f"invalid_json_field:{key}")
|
||||
return parsed
|
||||
|
||||
|
||||
def _parse_body_identifier(value, key: str) -> int:
|
||||
if isinstance(value, bool):
|
||||
raise ValueError(f"invalid_body_field:{key}")
|
||||
|
||||
Reference in New Issue
Block a user