mirror of https://github.com/tiangolo/fastapi.git
make tests compatible with Python 3.8 and 3.9
Signed-off-by: merlinz01 <158784988+merlinz01@users.noreply.github.com>
This commit is contained in:
parent
38610a8fd4
commit
826f11a611
|
|
@ -1,4 +1,5 @@
|
||||||
from typing import Annotated, Optional, Union
|
import sys
|
||||||
|
from typing import Optional, Union
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi import Body, FastAPI
|
from fastapi import Body, FastAPI
|
||||||
|
|
@ -7,16 +8,30 @@ from fastapi.testclient import TestClient
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
DEFAULT = 1234567890
|
DEFAULT = 1234567890
|
||||||
|
|
||||||
|
endpoints = []
|
||||||
|
|
||||||
|
if sys.hexversion >= 0x31000000:
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
@app.post("/api1")
|
@app.post("/api1")
|
||||||
def api1(integer_or_null: Annotated[int | None, Body(embed=True)] = DEFAULT) -> dict:
|
def api1(
|
||||||
|
integer_or_null: Annotated[int | None, Body(embed=True)] = DEFAULT,
|
||||||
|
) -> dict:
|
||||||
return {"received": integer_or_null}
|
return {"received": integer_or_null}
|
||||||
|
|
||||||
|
endpoints.append("/api1")
|
||||||
|
|
||||||
|
|
||||||
|
if sys.hexversion >= 0x30900000:
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
@app.post("/api2")
|
@app.post("/api2")
|
||||||
def api2(integer_or_null: Annotated[Optional[int], Body(embed=True)] = DEFAULT) -> dict:
|
def api2(
|
||||||
|
integer_or_null: Annotated[Optional[int], Body(embed=True)] = DEFAULT,
|
||||||
|
) -> dict:
|
||||||
return {"received": integer_or_null}
|
return {"received": integer_or_null}
|
||||||
|
|
||||||
|
endpoints.append("/api2")
|
||||||
|
|
||||||
@app.post("/api3")
|
@app.post("/api3")
|
||||||
def api3(
|
def api3(
|
||||||
|
|
@ -24,16 +39,21 @@ def api3(
|
||||||
) -> dict:
|
) -> dict:
|
||||||
return {"received": integer_or_null}
|
return {"received": integer_or_null}
|
||||||
|
|
||||||
|
endpoints.append("/api3")
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api4")
|
@app.post("/api4")
|
||||||
def api4(integer_or_null: Optional[int] = Body(embed=True, default=DEFAULT)) -> dict:
|
def api4(integer_or_null: Optional[int] = Body(embed=True, default=DEFAULT)) -> dict:
|
||||||
return {"received": integer_or_null}
|
return {"received": integer_or_null}
|
||||||
|
|
||||||
|
|
||||||
|
endpoints.append("/api4")
|
||||||
|
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("api", ["/api1", "/api2", "/api3", "/api4"])
|
@pytest.mark.parametrize("api", endpoints)
|
||||||
def test_api1_integer(api):
|
def test_api1_integer(api):
|
||||||
response = client.post(api, json={"integer_or_null": 100})
|
response = client.post(api, json={"integer_or_null": 100})
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue