|
| 1 | +import pydantic |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pydantic_async_validation import AsyncValidationModelMixin, async_field_validator |
| 5 | + |
| 6 | +try: |
| 7 | + import fastapi |
| 8 | + from fastapi.testclient import TestClient |
| 9 | + |
| 10 | + from pydantic_async_validation.fastapi import ensure_request_validation_errors |
| 11 | +except ImportError: |
| 12 | + fastapi = None |
| 13 | + |
| 14 | + |
| 15 | +class SomethingModel(AsyncValidationModelMixin, pydantic.BaseModel): |
| 16 | + name: str |
| 17 | + |
| 18 | + @async_field_validator('name') |
| 19 | + async def validate_name(self, value: str) -> None: |
| 20 | + if value == "invalid": |
| 21 | + raise ValueError("Invalid name") |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def app(): |
| 26 | + app = fastapi.FastAPI() |
| 27 | + |
| 28 | + @app.get("/no-errors") |
| 29 | + async def all_valid(): |
| 30 | + instance = SomethingModel(name="valid") |
| 31 | + with ensure_request_validation_errors(): |
| 32 | + await instance.model_async_validate() |
| 33 | + |
| 34 | + @app.get("/without-request-validation-errors") |
| 35 | + async def without(): |
| 36 | + instance = SomethingModel(name="invalid") |
| 37 | + await instance.model_async_validate() |
| 38 | + |
| 39 | + @app.get("/with-request-validation-errors") |
| 40 | + async def with_context_manager(): |
| 41 | + instance = SomethingModel(name="invalid") |
| 42 | + with ensure_request_validation_errors(): |
| 43 | + await instance.model_async_validate() |
| 44 | + |
| 45 | + @app.get("/with-request-validation-errors-decorator") |
| 46 | + @ensure_request_validation_errors |
| 47 | + async def with_decorator(): |
| 48 | + instance = SomethingModel(name="invalid") |
| 49 | + await instance.model_async_validate() |
| 50 | + |
| 51 | + return app |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.skipif(fastapi is None, reason="fastapi not installed") |
| 55 | +def test_fastapi_without_error_just_works(app): |
| 56 | + with TestClient(app) as client: |
| 57 | + response = client.get("/no-errors") |
| 58 | + assert response.status_code == 200 |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.skipif(fastapi is None, reason="fastapi not installed") |
| 62 | +def test_fastapi_fails_without_handling(app): |
| 63 | + with TestClient(app) as client: |
| 64 | + with pytest.raises(pydantic.ValidationError): |
| 65 | + client.get("/without-request-validation-errors") |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.skipif(fastapi is None, reason="fastapi not installed") |
| 69 | +def test_fastapi_triggers_validation_response(app): |
| 70 | + with TestClient(app) as client: |
| 71 | + response = client.get("/with-request-validation-errors") |
| 72 | + assert response.status_code == 422 |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.skipif(fastapi is None, reason="fastapi not installed") |
| 76 | +def test_fastapi_triggers_validation_response_by_decorator(app): |
| 77 | + with TestClient(app) as client: |
| 78 | + response = client.get("/with-request-validation-errors-decorator") |
| 79 | + assert response.status_code == 422 |
0 commit comments