bef7a72799
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
from auto_reverse.doc.openapi import build_openapi
|
|
from auto_reverse.models import EndpointRecord, Signature
|
|
|
|
|
|
def _record(method: str, template: str, **kw) -> EndpointRecord:
|
|
rec = EndpointRecord(signature=Signature(method, "ex.com", template, "2xx"))
|
|
for k, v in kw.items():
|
|
setattr(rec, k, v)
|
|
return rec
|
|
|
|
|
|
def test_builds_paths_and_methods():
|
|
records = [
|
|
_record("GET", "/api/users", summary="List users",
|
|
response_schema={"type": "array"}),
|
|
_record("POST", "/api/users", summary="Create user",
|
|
request_schema={"type": "object"}),
|
|
]
|
|
spec = build_openapi(records, title="ex.com API")
|
|
assert spec["openapi"].startswith("3.")
|
|
assert spec["info"]["title"] == "ex.com API"
|
|
assert set(spec["paths"]["/api/users"]) == {"get", "post"}
|
|
assert spec["paths"]["/api/users"]["get"]["summary"] == "List users"
|
|
|
|
|
|
def test_path_param_declared_for_template():
|
|
rec = _record("GET", "/api/users/{id}", summary="Get user")
|
|
spec = build_openapi([rec], title="x")
|
|
params = spec["paths"]["/api/users/{id}"]["get"]["parameters"]
|
|
assert any(p["name"] == "id" and p["in"] == "path" for p in params)
|
|
|
|
|
|
def test_request_body_included_when_schema_present():
|
|
rec = _record("POST", "/api/x", request_schema={"type": "object"})
|
|
op = build_openapi([rec], title="x")["paths"]["/api/x"]["post"]
|
|
assert op["requestBody"]["content"]["application/json"]["schema"] == {"type": "object"}
|