Browse Source

skip orjson tests when it's not installed

pull/15149/head
svlandeg 4 months ago
parent
commit
60c2fe7524
  1. 8
      tests/test_default_response_class.py
  2. 4
      tests/test_deprecated_responses.py
  3. 3
      tests/test_orjson_response_class.py
  4. 2
      tests/test_tutorial/test_custom_response/test_tutorial001b.py
  5. 3
      tests/test_tutorial/test_custom_response/test_tutorial009c.py
  6. 6
      tests/utils.py

8
tests/test_default_response_class.py

@ -1,15 +1,18 @@
from typing import Any from typing import Any
import orjson
from fastapi import APIRouter, FastAPI from fastapi import APIRouter, FastAPI
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from tests.utils import needs_orjson
class ORJSONResponse(JSONResponse): class ORJSONResponse(JSONResponse):
media_type = "application/x-orjson" media_type = "application/x-orjson"
def render(self, content: Any) -> bytes: def render(self, content: Any) -> bytes:
import orjson
return orjson.dumps(content) return orjson.dumps(content)
@ -118,6 +121,7 @@ html_type = "text/html; charset=utf-8"
override_type = "application/x-override" override_type = "application/x-override"
@needs_orjson
def test_app(): def test_app():
with client: with client:
response = client.get("/") response = client.get("/")
@ -132,6 +136,7 @@ def test_app_override():
assert response.headers["content-type"] == text_type assert response.headers["content-type"] == text_type
@needs_orjson
def test_router_a(): def test_router_a():
with client: with client:
response = client.get("/a") response = client.get("/a")
@ -146,6 +151,7 @@ def test_router_a_override():
assert response.headers["content-type"] == text_type assert response.headers["content-type"] == text_type
@needs_orjson
def test_router_a_a(): def test_router_a_a():
with client: with client:
response = client.get("/a/a") response = client.get("/a/a")

4
tests/test_deprecated_responses.py

@ -7,6 +7,8 @@ from fastapi.responses import ORJSONResponse, UJSONResponse
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from pydantic import BaseModel from pydantic import BaseModel
from tests.utils import needs_orjson
class Item(BaseModel): class Item(BaseModel):
name: str name: str
@ -28,6 +30,7 @@ def _make_orjson_app() -> FastAPI:
return app return app
@needs_orjson
def test_orjson_response_returns_correct_data(): def test_orjson_response_returns_correct_data():
app = _make_orjson_app() app = _make_orjson_app()
client = TestClient(app) client = TestClient(app)
@ -38,6 +41,7 @@ def test_orjson_response_returns_correct_data():
assert response.json() == {"name": "widget", "price": 9.99} assert response.json() == {"name": "widget", "price": 9.99}
@needs_orjson
def test_orjson_response_emits_deprecation_warning(): def test_orjson_response_emits_deprecation_warning():
with pytest.warns(FastAPIDeprecationWarning, match="ORJSONResponse is deprecated"): with pytest.warns(FastAPIDeprecationWarning, match="ORJSONResponse is deprecated"):
ORJSONResponse(content={"hello": "world"}) ORJSONResponse(content={"hello": "world"})

3
tests/test_orjson_response_class.py

@ -1,11 +1,14 @@
import warnings import warnings
import pytest
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.exceptions import FastAPIDeprecationWarning from fastapi.exceptions import FastAPIDeprecationWarning
from fastapi.responses import ORJSONResponse from fastapi.responses import ORJSONResponse
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from sqlalchemy.sql.elements import quoted_name from sqlalchemy.sql.elements import quoted_name
pytest.importorskip("orjson")
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore", FastAPIDeprecationWarning) warnings.simplefilter("ignore", FastAPIDeprecationWarning)
app = FastAPI(default_response_class=ORJSONResponse) app = FastAPI(default_response_class=ORJSONResponse)

2
tests/test_tutorial/test_custom_response/test_tutorial001b.py

@ -11,6 +11,8 @@ with warnings.catch_warnings():
client = TestClient(app) client = TestClient(app)
pytest.importorskip("orjson")
@pytest.mark.filterwarnings("ignore::fastapi.exceptions.FastAPIDeprecationWarning") @pytest.mark.filterwarnings("ignore::fastapi.exceptions.FastAPIDeprecationWarning")
def test_get_custom_response(): def test_get_custom_response():

3
tests/test_tutorial/test_custom_response/test_tutorial009c.py

@ -1,5 +1,8 @@
import pytest
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
pytest.importorskip("orjson")
from docs_src.custom_response.tutorial009c_py310 import app from docs_src.custom_response.tutorial009c_py310 import app
client = TestClient(app) client = TestClient(app)

6
tests/utils.py

@ -1,3 +1,4 @@
import importlib
import sys import sys
import pytest import pytest
@ -9,6 +10,11 @@ needs_py314 = pytest.mark.skipif(
sys.version_info < (3, 14), reason="requires python3.14+" sys.version_info < (3, 14), reason="requires python3.14+"
) )
needs_orjson = pytest.mark.skipif(
importlib.util.find_spec("orjson") is None,
reason="requires orjson",
)
workdir_lock = pytest.mark.xdist_group("workdir_lock") workdir_lock = pytest.mark.xdist_group("workdir_lock")

Loading…
Cancel
Save