mirror of https://github.com/tiangolo/fastapi.git
✅ Simplify tests for extra_models (#13178)
This commit is contained in:
parent
ae93ec140a
commit
3d017824ba
|
|
@ -1,12 +1,27 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
from dirty_equals import IsOneOf
|
from dirty_equals import IsOneOf
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from docs_src.extra_models.tutorial003 import app
|
from ...utils import needs_py310
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_car():
|
@pytest.fixture(
|
||||||
|
name="client",
|
||||||
|
params=[
|
||||||
|
"tutorial003",
|
||||||
|
pytest.param("tutorial003_py310", marks=needs_py310),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
|
||||||
|
|
||||||
|
client = TestClient(mod.app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_car(client: TestClient):
|
||||||
response = client.get("/items/item1")
|
response = client.get("/items/item1")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
|
|
@ -15,7 +30,7 @@ def test_get_car():
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_get_plane():
|
def test_get_plane(client: TestClient):
|
||||||
response = client.get("/items/item2")
|
response = client.get("/items/item2")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
|
|
@ -25,7 +40,7 @@ def test_get_plane():
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
def test_openapi_schema(client: TestClient):
|
||||||
response = client.get("/openapi.json")
|
response = client.get("/openapi.json")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
|
|
|
||||||
|
|
@ -1,144 +0,0 @@
|
||||||
import pytest
|
|
||||||
from dirty_equals import IsOneOf
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py310
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.extra_models.tutorial003_py310 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_get_car(client: TestClient):
|
|
||||||
response = client.get("/items/item1")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"description": "All my friends drive a low rider",
|
|
||||||
"type": "car",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_get_plane(client: TestClient):
|
|
||||||
response = client.get("/items/item2")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"description": "Music is my aeroplane, it's my aeroplane",
|
|
||||||
"type": "plane",
|
|
||||||
"size": 5,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py310
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/{item_id}": {
|
|
||||||
"get": {
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"title": "Response Read Item Items Item Id Get",
|
|
||||||
"anyOf": [
|
|
||||||
{"$ref": "#/components/schemas/PlaneItem"},
|
|
||||||
{"$ref": "#/components/schemas/CarItem"},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"422": {
|
|
||||||
"description": "Validation Error",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"$ref": "#/components/schemas/HTTPValidationError"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"summary": "Read Item",
|
|
||||||
"operationId": "read_item_items__item_id__get",
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"required": True,
|
|
||||||
"schema": {"title": "Item Id", "type": "string"},
|
|
||||||
"name": "item_id",
|
|
||||||
"in": "path",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"PlaneItem": {
|
|
||||||
"title": "PlaneItem",
|
|
||||||
"required": IsOneOf(
|
|
||||||
["description", "type", "size"],
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
["description", "size"],
|
|
||||||
),
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"description": {"title": "Description", "type": "string"},
|
|
||||||
"type": {"title": "Type", "type": "string", "default": "plane"},
|
|
||||||
"size": {"title": "Size", "type": "integer"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"CarItem": {
|
|
||||||
"title": "CarItem",
|
|
||||||
"required": IsOneOf(
|
|
||||||
["description", "type"],
|
|
||||||
# TODO: remove when deprecating Pydantic v1
|
|
||||||
["description"],
|
|
||||||
),
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"description": {"title": "Description", "type": "string"},
|
|
||||||
"type": {"title": "Type", "type": "string", "default": "car"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"ValidationError": {
|
|
||||||
"title": "ValidationError",
|
|
||||||
"required": ["loc", "msg", "type"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"loc": {
|
|
||||||
"title": "Location",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"msg": {"title": "Message", "type": "string"},
|
|
||||||
"type": {"title": "Error Type", "type": "string"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"HTTPValidationError": {
|
|
||||||
"title": "HTTPValidationError",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"detail": {
|
|
||||||
"title": "Detail",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +1,26 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from docs_src.extra_models.tutorial004 import app
|
from ...utils import needs_py39
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_items():
|
@pytest.fixture(
|
||||||
|
name="client",
|
||||||
|
params=[
|
||||||
|
"tutorial004",
|
||||||
|
pytest.param("tutorial004_py39", marks=needs_py39),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
|
||||||
|
|
||||||
|
client = TestClient(mod.app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_items(client: TestClient):
|
||||||
response = client.get("/items/")
|
response = client.get("/items/")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == [
|
assert response.json() == [
|
||||||
|
|
@ -14,7 +29,7 @@ def test_get_items():
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
def test_openapi_schema(client: TestClient):
|
||||||
response = client.get("/openapi.json")
|
response = client.get("/openapi.json")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
|
|
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
import pytest
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py39
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.extra_models.tutorial004_py39 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py39
|
|
||||||
def test_get_items(client: TestClient):
|
|
||||||
response = client.get("/items/")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == [
|
|
||||||
{"name": "Foo", "description": "There comes my hero"},
|
|
||||||
{"name": "Red", "description": "It's my aeroplane"},
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py39
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/": {
|
|
||||||
"get": {
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"title": "Response Read Items Items Get",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"$ref": "#/components/schemas/Item"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"summary": "Read Items",
|
|
||||||
"operationId": "read_items_items__get",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "description"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"description": {"title": "Description", "type": "string"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
@ -1,17 +1,32 @@
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from docs_src.extra_models.tutorial005 import app
|
from ...utils import needs_py39
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_items():
|
@pytest.fixture(
|
||||||
|
name="client",
|
||||||
|
params=[
|
||||||
|
"tutorial005",
|
||||||
|
pytest.param("tutorial005_py39", marks=needs_py39),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def get_client(request: pytest.FixtureRequest):
|
||||||
|
mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
|
||||||
|
|
||||||
|
client = TestClient(mod.app)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_items(client: TestClient):
|
||||||
response = client.get("/keyword-weights/")
|
response = client.get("/keyword-weights/")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {"foo": 2.3, "bar": 3.4}
|
assert response.json() == {"foo": 2.3, "bar": 3.4}
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
def test_openapi_schema(client: TestClient):
|
||||||
response = client.get("/openapi.json")
|
response = client.get("/openapi.json")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
import pytest
|
|
||||||
from fastapi.testclient import TestClient
|
|
||||||
|
|
||||||
from ...utils import needs_py39
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="client")
|
|
||||||
def get_client():
|
|
||||||
from docs_src.extra_models.tutorial005_py39 import app
|
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
return client
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py39
|
|
||||||
def test_get_items(client: TestClient):
|
|
||||||
response = client.get("/keyword-weights/")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {"foo": 2.3, "bar": 3.4}
|
|
||||||
|
|
||||||
|
|
||||||
@needs_py39
|
|
||||||
def test_openapi_schema(client: TestClient):
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200, response.text
|
|
||||||
assert response.json() == {
|
|
||||||
"openapi": "3.1.0",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/keyword-weights/": {
|
|
||||||
"get": {
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {
|
|
||||||
"title": "Response Read Keyword Weights Keyword Weights Get",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": {"type": "number"},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"summary": "Read Keyword Weights",
|
|
||||||
"operationId": "read_keyword_weights_keyword_weights__get",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue