Tests should check that there are different models in the schema

This commit is contained in:
Philipp Eisen 2024-02-26 14:08:12 +01:00
parent 7f588fe219
commit 438195abeb
No known key found for this signature in database
6 changed files with 63 additions and 9 deletions

View File

@ -12,12 +12,14 @@ def get_client() -> TestClient:
return client
@needs_pydanticv2
def test_create_item(client: TestClient) -> None:
response = client.post("/items/", json={"name": "Foo"})
assert response.status_code == 200, response.text
assert response.json() == {"name": "Foo", "description": None}
@needs_pydanticv2
def test_read_items(client: TestClient) -> None:
response = client.get("/items/")
assert response.status_code == 200, response.text
@ -48,7 +50,9 @@ def test_openapi_schema(client: TestClient) -> None:
"content": {
"application/json": {
"schema": {
"items": {"$ref": "#/components/schemas/Item"},
"items": {
"$ref": "#/components/schemas/Item-Output"
},
"type": "array",
"title": "Response Read Items Items Get",
}
@ -63,7 +67,7 @@ def test_openapi_schema(client: TestClient) -> None:
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Item"}
"schema": {"$ref": "#/components/schemas/Item-Input"}
}
},
"required": True,
@ -100,7 +104,7 @@ def test_openapi_schema(client: TestClient) -> None:
"type": "object",
"title": "HTTPValidationError",
},
"Item": {
"Item-Input": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
@ -112,6 +116,18 @@ def test_openapi_schema(client: TestClient) -> None:
"required": ["name"],
"title": "Item",
},
"Item-Output": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Description",
},
},
"type": "object",
"required": ["name", "description"],
"title": "Item",
},
"ValidationError": {
"properties": {
"loc": {

View File

@ -13,6 +13,7 @@ def get_client() -> TestClient:
@needs_py310
@needs_pydanticv2
def test_create_item(client: TestClient) -> None:
response = client.post("/items/", json={"name": "Foo"})
assert response.status_code == 200, response.text
@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None:
@needs_py310
@needs_pydanticv2
def test_read_items(client: TestClient) -> None:
response = client.get("/items/")
assert response.status_code == 200, response.text
@ -51,7 +53,9 @@ def test_openapi_schema(client: TestClient) -> None:
"content": {
"application/json": {
"schema": {
"items": {"$ref": "#/components/schemas/Item"},
"items": {
"$ref": "#/components/schemas/Item-Output"
},
"type": "array",
"title": "Response Read Items Items Get",
}
@ -66,7 +70,7 @@ def test_openapi_schema(client: TestClient) -> None:
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Item"}
"schema": {"$ref": "#/components/schemas/Item-Input"}
}
},
"required": True,
@ -103,7 +107,7 @@ def test_openapi_schema(client: TestClient) -> None:
"type": "object",
"title": "HTTPValidationError",
},
"Item": {
"Item-Input": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
@ -115,6 +119,18 @@ def test_openapi_schema(client: TestClient) -> None:
"required": ["name"],
"title": "Item",
},
"Item-Output": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Description",
},
},
"type": "object",
"required": ["name", "description"],
"title": "Item",
},
"ValidationError": {
"properties": {
"loc": {

View File

@ -13,6 +13,7 @@ def get_client() -> TestClient:
@needs_py39
@needs_pydanticv2
def test_create_item(client: TestClient) -> None:
response = client.post("/items/", json={"name": "Foo"})
assert response.status_code == 200, response.text
@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None:
@needs_py39
@needs_pydanticv2
def test_read_items(client: TestClient) -> None:
response = client.get("/items/")
assert response.status_code == 200, response.text
@ -51,7 +53,9 @@ def test_openapi_schema(client: TestClient) -> None:
"content": {
"application/json": {
"schema": {
"items": {"$ref": "#/components/schemas/Item"},
"items": {
"$ref": "#/components/schemas/Item-Output"
},
"type": "array",
"title": "Response Read Items Items Get",
}
@ -66,7 +70,7 @@ def test_openapi_schema(client: TestClient) -> None:
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Item"}
"schema": {"$ref": "#/components/schemas/Item-Input"}
}
},
"required": True,
@ -103,7 +107,7 @@ def test_openapi_schema(client: TestClient) -> None:
"type": "object",
"title": "HTTPValidationError",
},
"Item": {
"Item-Input": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
@ -115,6 +119,18 @@ def test_openapi_schema(client: TestClient) -> None:
"required": ["name"],
"title": "Item",
},
"Item-Output": {
"properties": {
"name": {"type": "string", "title": "Name"},
"description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Description",
},
},
"type": "object",
"required": ["name", "description"],
"title": "Item",
},
"ValidationError": {
"properties": {
"loc": {

View File

@ -12,12 +12,14 @@ def get_client() -> TestClient:
return client
@needs_pydanticv2
def test_create_item(client: TestClient) -> None:
response = client.post("/items/", json={"name": "Foo"})
assert response.status_code == 200, response.text
assert response.json() == {"name": "Foo", "description": None}
@needs_pydanticv2
def test_read_items(client: TestClient) -> None:
response = client.get("/items/")
assert response.status_code == 200, response.text

View File

@ -13,6 +13,7 @@ def get_client() -> TestClient:
@needs_py310
@needs_pydanticv2
def test_create_item(client: TestClient) -> None:
response = client.post("/items/", json={"name": "Foo"})
assert response.status_code == 200, response.text
@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None:
@needs_py310
@needs_pydanticv2
def test_read_items(client: TestClient) -> None:
response = client.get("/items/")
assert response.status_code == 200, response.text

View File

@ -13,6 +13,7 @@ def get_client() -> TestClient:
@needs_py39
@needs_pydanticv2
def test_create_item(client: TestClient) -> None:
response = client.post("/items/", json={"name": "Foo"})
assert response.status_code == 200, response.text
@ -20,6 +21,7 @@ def test_create_item(client: TestClient) -> None:
@needs_py39
@needs_pydanticv2
def test_read_items(client: TestClient) -> None:
response = client.get("/items/")
assert response.status_code == 200, response.text