Compare commits

...

2 Commits

Author SHA1 Message Date
Kevin Han
60baf5de16
Merge a8102f1fc7 into 7e7c1c4c0a 2026-07-28 16:04:05 +00:00
Kevin
a8102f1fc7 add test for validating OAS not match case 2026-07-28 17:03:58 +01:00

View File

@ -249,3 +249,67 @@ def test_mock_server_ignores_readonly_fields_in_nested_request_objects(manifest)
)
assert status == 201
assert json.loads(body.decode("utf-8")) == {"ruleId": "1"}
def test_integration_error_assertion_fails_when_deserialized_error_does_not_match_oas_example():
"""Generated error-path tests compare ApiException.data to the OAS error example."""
import unittest
import urllib.error
import urllib.request
from pydantic import BaseModel, ConfigDict
class Error(BaseModel):
title: str
status: int
model_config = ConfigDict(extra="allow")
def to_json(self) -> str:
return self.model_dump_json()
mismatched_manifest = {
"createAlertRule": OperationExpectation(
operation_id="createAlertRule",
method="POST",
path="/alerts/rules",
request_body_example={"ruleName": "Example"},
success_status=201,
success_body={"ruleId": "1"},
error_responses={
"400": ErrorResponseExpectation(
status=400,
body={"title": "Wrong Title", "status": 400},
)
},
),
}
oas_example = {"title": "Bad Request", "status": 400}
with MockApiServer(mismatched_manifest) as server:
request = urllib.request.Request(
server.base_url + "/alerts/rules",
data=json.dumps({"unexpected": True}).encode("utf-8"),
headers={
AUTHORIZATION_HEADER: "Bearer test-token",
OPERATION_ID_HEADER: "createAlertRule",
ERROR_STATUS_HEADER: "400",
"Content-Type": "application/json",
},
method="POST",
)
with pytest.raises(urllib.error.HTTPError) as http_error:
urllib.request.urlopen(request)
wire_body = json.loads(http_error.value.read().decode("utf-8"))
exception_data = Error.model_validate(wire_body)
def assert_constructed_model_matches_example_json(model, loaded_json):
test_case = unittest.TestCase()
test_case.assertEqual(
json.dumps(loaded_json, sort_keys=True),
json.dumps(json.loads(model.to_json()), sort_keys=True),
)
with pytest.raises(AssertionError):
assert_constructed_model_matches_example_json(exception_data, oas_example)