-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tests to openapi with SimpleAPIView
- Loading branch information
Showing
1 changed file
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
from typing import Dict, Union | ||
|
||
import pytest | ||
from pydantic import BaseModel | ||
|
||
from esmerald import JSON, APIView, Gateway, Include, SimpleAPIView, get | ||
from esmerald.openapi.datastructures import OpenAPIResponse | ||
from esmerald.testclient import create_client | ||
|
||
|
||
class Item(BaseModel): | ||
sku: Union[int, str] | ||
|
||
|
||
@get() | ||
def read_people() -> Dict[str, str]: | ||
""" """ | ||
|
||
|
||
@pytest.mark.parametrize("value", [APIView, SimpleAPIView]) | ||
def test_add_include_to_openapi(test_client_factory, value): | ||
class MyAPI(value): | ||
if issubclass(value, SimpleAPIView): | ||
http_allowed_methods = ["read_item"] | ||
|
||
@get( | ||
"/item", | ||
description="Read an item", | ||
responses={ | ||
200: OpenAPIResponse(model=Item, description="The SKU information of an item") | ||
}, | ||
) | ||
async def read_item(self) -> JSON: | ||
""" """ | ||
|
||
with create_client( | ||
routes=[ | ||
Gateway(handler=read_people), | ||
Include("/child", routes=[Gateway(handler=MyAPI)]), | ||
] | ||
) as client: | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
|
||
assert response.json() == { | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "Esmerald", | ||
"summary": "Esmerald application", | ||
"description": "Highly scalable, performant, easy to learn and for every application.", | ||
"contact": {"name": "admin", "email": "[email protected]"}, | ||
"version": client.app.version, | ||
}, | ||
"servers": [{"url": "/"}], | ||
"paths": { | ||
"/child/item": { | ||
"get": { | ||
"summary": "Read Item", | ||
"description": "Read an item", | ||
"operationId": "read_item_item_get", | ||
"responses": { | ||
"200": { | ||
"description": "The SKU information of an item", | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/Item"} | ||
} | ||
}, | ||
} | ||
}, | ||
"deprecated": False, | ||
} | ||
}, | ||
"/": { | ||
"get": { | ||
"summary": "Read People", | ||
"operationId": "read_people__get", | ||
"responses": { | ||
"200": { | ||
"description": "Successful response", | ||
"content": {"application/json": {"schema": {"type": "string"}}}, | ||
} | ||
}, | ||
"deprecated": False, | ||
} | ||
}, | ||
}, | ||
"components": { | ||
"schemas": { | ||
"Item": { | ||
"properties": { | ||
"sku": { | ||
"anyOf": [{"type": "integer"}, {"type": "string"}], | ||
"title": "Sku", | ||
} | ||
}, | ||
"type": "object", | ||
"required": ["sku"], | ||
"title": "Item", | ||
} | ||
} | ||
}, | ||
} | ||
|
||
|
||
@pytest.mark.parametrize("value", [APIView, SimpleAPIView]) | ||
def test_include_no_include_in_schema(test_client_factory, value): | ||
class MyAPI(value): | ||
if issubclass(value, SimpleAPIView): | ||
http_allowed_methods = ["read_item"] | ||
|
||
@get( | ||
"/item", | ||
description="Read an item", | ||
responses={ | ||
200: OpenAPIResponse(model=Item, description="The SKU information of an item") | ||
}, | ||
) | ||
async def read_item(self) -> JSON: | ||
""" """ | ||
|
||
with create_client( | ||
routes=[ | ||
Gateway(handler=read_people), | ||
Include("/child", routes=[Gateway(handler=MyAPI)], include_in_schema=False), | ||
] | ||
) as client: | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
|
||
assert response.json() == { | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "Esmerald", | ||
"summary": "Esmerald application", | ||
"description": "Highly scalable, performant, easy to learn and for every application.", | ||
"contact": {"name": "admin", "email": "[email protected]"}, | ||
"version": client.app.version, | ||
}, | ||
"servers": [{"url": "/"}], | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"summary": "Read People", | ||
"operationId": "read_people__get", | ||
"responses": { | ||
"200": { | ||
"description": "Successful response", | ||
"content": {"application/json": {"schema": {"type": "string"}}}, | ||
} | ||
}, | ||
"deprecated": False, | ||
} | ||
}, | ||
}, | ||
} |