Browse Source

Update test to reflect latest changes on master

pull/14299/head
Yurii Motov 6 months ago
parent
commit
6706439b69
  1. 31
      tests/test_pydantic_json_response_class.py

31
tests/test_pydantic_json_response_class.py

@ -1,16 +1,13 @@
import math import math
from typing import List, Optional from typing import Optional
import pytest import pytest
from dirty_equals import IsFloatNan from dirty_equals import IsFloatNan
from fastapi import FastAPI from fastapi import FastAPI
from fastapi._compat import PYDANTIC_V2
from fastapi.responses import PydanticJSONResponse from fastapi.responses import PydanticJSONResponse
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from .utils import needs_py_lt_314, needs_pydanticv2
app = FastAPI(default_response_class=PydanticJSONResponse) app = FastAPI(default_response_class=PydanticJSONResponse)
@ -63,7 +60,7 @@ def get_response_model_params():
class FloatsNone(BaseModel): class FloatsNone(BaseModel):
# pydantic converts inf/nan to None by default # pydantic converts inf/nan to None by default
numbers: List[float] numbers: list[float]
class FloatsNum(FloatsNone): class FloatsNum(FloatsNone):
@ -85,7 +82,6 @@ def get_floats():
client = TestClient(app) client = TestClient(app)
@needs_pydanticv2
@pytest.mark.parametrize( @pytest.mark.parametrize(
"path,expected_response", "path,expected_response",
[ [
@ -138,7 +134,6 @@ def test_response_model_params(path: str, expected_response: dict):
assert response.json() == expected_response assert response.json() == expected_response
@needs_pydanticv2
@pytest.mark.parametrize( @pytest.mark.parametrize(
"path,expected_numbers", "path,expected_numbers",
[ [
@ -153,30 +148,8 @@ def test_floats(path: str, expected_numbers: list):
assert response.json() == {"numbers": expected_numbers} assert response.json() == {"numbers": expected_numbers}
@needs_pydanticv2
def test_custom_response_class(): def test_custom_response_class():
response = client.get("/custom-class") response = client.get("/custom-class")
assert response.status_code == 200 assert response.status_code == 200
assert response.headers["content-type"] == "application/x-custom" assert response.headers["content-type"] == "application/x-custom"
assert response.json() == {"numbers": [3.14, "Infinity", "NaN", 2.72]} assert response.json() == {"numbers": [3.14, "Infinity", "NaN", 2.72]}
@needs_py_lt_314
def test_requires_pydantic_v2_model():
if PYDANTIC_V2:
from pydantic.v1 import BaseModel as BaseModelV1
else:
from pydantic import BaseModel as BaseModelV1
app = FastAPI(default_response_class=PydanticJSONResponse)
class ModelV1(BaseModelV1):
data: str
@app.get("/model-v1")
def get_model_v1() -> ModelV1:
return ModelV1(data="hello")
client = TestClient(app)
with pytest.raises(AssertionError, match="requires a pydantic v2 model"):
client.get("/model-v1")

Loading…
Cancel
Save