mirror of https://github.com/tiangolo/fastapi.git
✅ Refactor 2 tests, for consistency and simplification (#9504)
✅ Refactor tests, for consistency and simplification
This commit is contained in:
parent
028e7cad67
commit
f00f0de9ca
|
|
@ -33,36 +33,146 @@ async def create_shop(
|
||||||
pass # pragma: no cover
|
pass # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
create_product_request_body = {
|
|
||||||
"content": {
|
|
||||||
"application/vnd.api+json": {
|
|
||||||
"schema": {"$ref": "#/components/schemas/Body_create_product_products_post"}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
}
|
|
||||||
|
|
||||||
create_shop_request_body = {
|
|
||||||
"content": {
|
|
||||||
"application/vnd.api+json": {
|
|
||||||
"schema": {"$ref": "#/components/schemas/Body_create_shop_shops_post"}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": True,
|
|
||||||
}
|
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
def test_openapi_schema():
|
||||||
response = client.get("/openapi.json")
|
response = client.get("/openapi.json")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
openapi_schema = response.json()
|
# insert_assert(response.json())
|
||||||
assert (
|
assert response.json() == {
|
||||||
openapi_schema["paths"]["/products"]["post"]["requestBody"]
|
"openapi": "3.0.2",
|
||||||
== create_product_request_body
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||||
)
|
"paths": {
|
||||||
assert (
|
"/products": {
|
||||||
openapi_schema["paths"]["/shops"]["post"]["requestBody"]
|
"post": {
|
||||||
== create_shop_request_body
|
"summary": "Create Product",
|
||||||
)
|
"operationId": "create_product_products_post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/vnd.api+json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Body_create_product_products_post"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/shops": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Create Shop",
|
||||||
|
"operationId": "create_shop_shops_post",
|
||||||
|
"requestBody": {
|
||||||
|
"content": {
|
||||||
|
"application/vnd.api+json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Body_create_shop_shops_post"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": True,
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {"application/json": {"schema": {}}},
|
||||||
|
},
|
||||||
|
"422": {
|
||||||
|
"description": "Validation Error",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/HTTPValidationError"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"Body_create_product_products_post": {
|
||||||
|
"title": "Body_create_product_products_post",
|
||||||
|
"required": ["data"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {"data": {"$ref": "#/components/schemas/Product"}},
|
||||||
|
},
|
||||||
|
"Body_create_shop_shops_post": {
|
||||||
|
"title": "Body_create_shop_shops_post",
|
||||||
|
"required": ["data"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {"$ref": "#/components/schemas/Shop"},
|
||||||
|
"included": {
|
||||||
|
"title": "Included",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/Product"},
|
||||||
|
"default": [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"HTTPValidationError": {
|
||||||
|
"title": "HTTPValidationError",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"detail": {
|
||||||
|
"title": "Detail",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Product": {
|
||||||
|
"title": "Product",
|
||||||
|
"required": ["name", "price"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"title": "Name", "type": "string"},
|
||||||
|
"price": {"title": "Price", "type": "number"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"Shop": {
|
||||||
|
"title": "Shop",
|
||||||
|
"required": ["name"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {"name": {"title": "Name", "type": "string"}},
|
||||||
|
},
|
||||||
|
"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"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,71 +1,9 @@
|
||||||
from copy import deepcopy
|
|
||||||
|
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from docs_src.dataclasses.tutorial002 import app
|
from docs_src.dataclasses.tutorial002 import app
|
||||||
|
|
||||||
client = TestClient(app)
|
client = TestClient(app)
|
||||||
|
|
||||||
openapi_schema = {
|
|
||||||
"openapi": "3.0.2",
|
|
||||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
|
||||||
"paths": {
|
|
||||||
"/items/next": {
|
|
||||||
"get": {
|
|
||||||
"summary": "Read Next Item",
|
|
||||||
"operationId": "read_next_item_items_next_get",
|
|
||||||
"responses": {
|
|
||||||
"200": {
|
|
||||||
"description": "Successful Response",
|
|
||||||
"content": {
|
|
||||||
"application/json": {
|
|
||||||
"schema": {"$ref": "#/components/schemas/Item"}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"components": {
|
|
||||||
"schemas": {
|
|
||||||
"Item": {
|
|
||||||
"title": "Item",
|
|
||||||
"required": ["name", "price"],
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"name": {"title": "Name", "type": "string"},
|
|
||||||
"price": {"title": "Price", "type": "number"},
|
|
||||||
"tags": {
|
|
||||||
"title": "Tags",
|
|
||||||
"type": "array",
|
|
||||||
"items": {"type": "string"},
|
|
||||||
},
|
|
||||||
"description": {"title": "Description", "type": "string"},
|
|
||||||
"tax": {"title": "Tax", "type": "number"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
|
||||||
response = client.get("/openapi.json")
|
|
||||||
assert response.status_code == 200
|
|
||||||
# TODO: remove this once Pydantic 1.9 is released
|
|
||||||
# Ref: https://github.com/pydantic/pydantic/pull/2557
|
|
||||||
data = response.json()
|
|
||||||
alternative_data1 = deepcopy(data)
|
|
||||||
alternative_data2 = deepcopy(data)
|
|
||||||
alternative_data1["components"]["schemas"]["Item"]["required"] = ["name", "price"]
|
|
||||||
alternative_data2["components"]["schemas"]["Item"]["required"] = [
|
|
||||||
"name",
|
|
||||||
"price",
|
|
||||||
"tags",
|
|
||||||
]
|
|
||||||
assert alternative_data1 == openapi_schema or alternative_data2 == openapi_schema
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_item():
|
def test_get_item():
|
||||||
response = client.get("/items/next")
|
response = client.get("/items/next")
|
||||||
|
|
@ -77,3 +15,51 @@ def test_get_item():
|
||||||
"tags": ["breater"],
|
"tags": ["breater"],
|
||||||
"tax": None,
|
"tax": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_openapi_schema():
|
||||||
|
response = client.get("/openapi.json")
|
||||||
|
assert response.status_code == 200
|
||||||
|
data = response.json()
|
||||||
|
assert data == {
|
||||||
|
"openapi": "3.0.2",
|
||||||
|
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||||
|
"paths": {
|
||||||
|
"/items/next": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Read Next Item",
|
||||||
|
"operationId": "read_next_item_items_next_get",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Successful Response",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {"$ref": "#/components/schemas/Item"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"Item": {
|
||||||
|
"title": "Item",
|
||||||
|
"required": ["name", "price"],
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": {"title": "Name", "type": "string"},
|
||||||
|
"price": {"title": "Price", "type": "number"},
|
||||||
|
"tags": {
|
||||||
|
"title": "Tags",
|
||||||
|
"type": "array",
|
||||||
|
"items": {"type": "string"},
|
||||||
|
},
|
||||||
|
"description": {"title": "Description", "type": "string"},
|
||||||
|
"tax": {"title": "Tax", "type": "number"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue