|
| 1 | +import pytest |
| 2 | +from pydantic import BaseModel, Field |
| 3 | + |
| 4 | +from flask_openapi3 import OpenAPI |
| 5 | + |
| 6 | +app = OpenAPI(__name__) |
| 7 | +app.config["TESTING"] = True |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def client(): |
| 12 | + client = app.test_client() |
| 13 | + return client |
| 14 | + |
| 15 | + |
| 16 | +class LoginRequest(BaseModel): |
| 17 | + email: str = Field(..., description="User email") |
| 18 | + password: str = Field(..., description="User password") |
| 19 | + |
| 20 | + |
| 21 | +@app.post("/login") |
| 22 | +def login(body: LoginRequest): |
| 23 | + return {"message": f"Login successful for {body.email}"} |
| 24 | + |
| 25 | + |
| 26 | +def test_pydantic_validation_error_schema(client): |
| 27 | + resp = client.get("/openapi/openapi.json") |
| 28 | + assert resp.status_code == 200 |
| 29 | + |
| 30 | + schemas = resp.json["components"]["schemas"] |
| 31 | + assert "ValidationErrorModel" in schemas |
| 32 | + |
| 33 | + validation_error_schema = schemas["ValidationErrorModel"] |
| 34 | + properties = validation_error_schema["properties"] |
| 35 | + |
| 36 | + assert "type" in properties |
| 37 | + assert "loc" in properties |
| 38 | + assert "msg" in properties |
| 39 | + assert "input" in properties |
| 40 | + assert "url" in properties |
| 41 | + assert "ctx" in properties |
| 42 | + |
| 43 | + assert properties["type"]["type"] == "string" |
| 44 | + assert properties["loc"]["type"] == "array" |
| 45 | + assert properties["msg"]["type"] == "string" |
| 46 | + |
| 47 | + required_fields = validation_error_schema.get("required", []) |
| 48 | + assert "type" in required_fields |
| 49 | + assert "loc" in required_fields |
| 50 | + assert "msg" in required_fields |
| 51 | + assert "input" in required_fields |
| 52 | + assert "ctx" not in required_fields |
| 53 | + |
| 54 | + |
| 55 | +def test_pydantic_validation_error_response(client): |
| 56 | + resp = client.post( |
| 57 | + "/login", |
| 58 | + json={"invalid_field": "test"}, |
| 59 | + content_type="application/json", |
| 60 | + ) |
| 61 | + |
| 62 | + assert resp.status_code == 422 |
| 63 | + |
| 64 | + errors = resp.json |
| 65 | + assert isinstance(errors, list) |
| 66 | + assert len(errors) > 0 |
| 67 | + |
| 68 | + error = errors[0] |
| 69 | + assert "type" in error |
| 70 | + assert "loc" in error |
| 71 | + assert "msg" in error |
| 72 | + assert "input" in error |
| 73 | + |
| 74 | + assert isinstance(error["type"], str) |
| 75 | + assert isinstance(error["loc"], list) |
| 76 | + assert isinstance(error["msg"], str) |
| 77 | + assert error["type"] in ["missing", "string_type", "value_error", "extra_forbidden"] |
| 78 | + |
| 79 | + |
| 80 | +def test_pydantic_missing_field_error_response(client): |
| 81 | + resp = client.post( |
| 82 | + "/login", |
| 83 | + json={}, |
| 84 | + content_type="application/json", |
| 85 | + ) |
| 86 | + |
| 87 | + assert resp.status_code == 422 |
| 88 | + |
| 89 | + errors = resp.json |
| 90 | + assert len(errors) >= 2 |
| 91 | + |
| 92 | + email_error = None |
| 93 | + for error in errors: |
| 94 | + if error.get("loc") == ["email"]: |
| 95 | + email_error = error |
| 96 | + break |
| 97 | + |
| 98 | + assert email_error is not None |
| 99 | + assert email_error["type"] == "missing" |
| 100 | + assert email_error["msg"] == "Field required" |
| 101 | + assert email_error["input"] == {} |
0 commit comments