Add sub-app to generate open api spec
This commit is contained in:
0
tests/test_oas/__init__.py
Normal file
0
tests/test_oas/__init__.py
Normal file
129
tests/test_oas/test_view.py
Normal file
129
tests/test_oas/test_view.py
Normal file
@@ -0,0 +1,129 @@
|
||||
from pydantic.main import BaseModel
|
||||
from aiohttp_pydantic import PydanticView, oas
|
||||
from aiohttp import web
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class Pet(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
|
||||
class PetCollectionView(PydanticView):
|
||||
async def get(self):
|
||||
return web.json_response()
|
||||
|
||||
async def post(self, pet: Pet):
|
||||
return web.json_response()
|
||||
|
||||
|
||||
class PetItemView(PydanticView):
|
||||
async def get(self, id: int, /):
|
||||
return web.json_response()
|
||||
|
||||
async def put(self, id: int, /, pet: Pet):
|
||||
return web.json_response()
|
||||
|
||||
async def delete(self, id: int, /):
|
||||
return web.json_response()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def generated_oas(aiohttp_client, loop) -> web.Application:
|
||||
app = web.Application()
|
||||
app.router.add_view("/pets", PetCollectionView)
|
||||
app.router.add_view("/pets/{id}", PetItemView)
|
||||
oas.setup(app)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
response = await client.get("/oas/spec")
|
||||
assert response.status == 200
|
||||
assert response.content_type == "application/json"
|
||||
return await response.json()
|
||||
|
||||
|
||||
async def test_generated_oas_should_have_pets_paths(generated_oas):
|
||||
assert "/pets" in generated_oas["paths"]
|
||||
|
||||
|
||||
async def test_pets_route_should_have_get_method(generated_oas):
|
||||
assert generated_oas["paths"]["/pets"]["get"] == {}
|
||||
|
||||
|
||||
async def test_pets_route_should_have_post_method(generated_oas):
|
||||
assert generated_oas["paths"]["/pets"]["post"] == {
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"properties": {
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"name": {"title": "Name", "type": "string"},
|
||||
},
|
||||
"required": ["id", "name"],
|
||||
"title": "Pet",
|
||||
"type": "object",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async def test_generated_oas_should_have_pets_id_paths(generated_oas):
|
||||
assert "/pets/{id}" in generated_oas["paths"]
|
||||
|
||||
|
||||
async def test_pets_id_route_should_have_delete_method(generated_oas):
|
||||
assert generated_oas["paths"]["/pets/{id}"]["delete"] == {
|
||||
"parameters": [
|
||||
{
|
||||
"in": "path",
|
||||
"name": "id",
|
||||
"required": True,
|
||||
"schema": {"type": "integer"},
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
async def test_pets_id_route_should_have_get_method(generated_oas):
|
||||
assert generated_oas["paths"]["/pets/{id}"]["get"] == {
|
||||
"parameters": [
|
||||
{
|
||||
"in": "path",
|
||||
"name": "id",
|
||||
"required": True,
|
||||
"schema": {"type": "integer"},
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
async def test_pets_id_route_should_have_put_method(generated_oas):
|
||||
assert generated_oas["paths"]["/pets/{id}"]["put"] == {
|
||||
"parameters": [
|
||||
{
|
||||
"in": "path",
|
||||
"name": "id",
|
||||
"required": True,
|
||||
"schema": {"type": "integer"},
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"properties": {
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"name": {"title": "Name", "type": "string"},
|
||||
},
|
||||
"required": ["id", "name"],
|
||||
"title": "Pet",
|
||||
"type": "object",
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -9,7 +9,6 @@ class User(BaseModel):
|
||||
|
||||
|
||||
def test_parse_func_signature():
|
||||
|
||||
def body_only(self, user: User):
|
||||
pass
|
||||
|
||||
@@ -37,13 +36,32 @@ def test_parse_func_signature():
|
||||
def path_body_qs_and_header(self, id: str, /, user: User, page: int, *, auth: UUID):
|
||||
pass
|
||||
|
||||
assert _parse_func_signature(body_only) == ({}, {'user': User}, {}, {})
|
||||
assert _parse_func_signature(path_only) == ({'id': str}, {}, {}, {})
|
||||
assert _parse_func_signature(qs_only) == ({}, {}, {'page': int}, {})
|
||||
assert _parse_func_signature(header_only) == ({}, {}, {}, {'auth': UUID})
|
||||
assert _parse_func_signature(path_and_qs) == ({'id': str}, {}, {'page': int}, {})
|
||||
assert _parse_func_signature(path_and_header) == ({'id': str}, {}, {}, {'auth': UUID})
|
||||
assert _parse_func_signature(qs_and_header) == ({}, {}, {'page': int}, {'auth': UUID})
|
||||
assert _parse_func_signature(path_qs_and_header) == ({'id': str}, {}, {'page': int}, {'auth': UUID})
|
||||
assert _parse_func_signature(path_body_qs_and_header) == ({'id': str}, {'user': User}, {'page': int}, {'auth': UUID})
|
||||
|
||||
assert _parse_func_signature(body_only) == ({}, {"user": User}, {}, {})
|
||||
assert _parse_func_signature(path_only) == ({"id": str}, {}, {}, {})
|
||||
assert _parse_func_signature(qs_only) == ({}, {}, {"page": int}, {})
|
||||
assert _parse_func_signature(header_only) == ({}, {}, {}, {"auth": UUID})
|
||||
assert _parse_func_signature(path_and_qs) == ({"id": str}, {}, {"page": int}, {})
|
||||
assert _parse_func_signature(path_and_header) == (
|
||||
{"id": str},
|
||||
{},
|
||||
{},
|
||||
{"auth": UUID},
|
||||
)
|
||||
assert _parse_func_signature(qs_and_header) == (
|
||||
{},
|
||||
{},
|
||||
{"page": int},
|
||||
{"auth": UUID},
|
||||
)
|
||||
assert _parse_func_signature(path_qs_and_header) == (
|
||||
{"id": str},
|
||||
{},
|
||||
{"page": int},
|
||||
{"auth": UUID},
|
||||
)
|
||||
assert _parse_func_signature(path_body_qs_and_header) == (
|
||||
{"id": str},
|
||||
{"user": User},
|
||||
{"page": int},
|
||||
{"auth": UUID},
|
||||
)
|
||||
|
||||
@@ -10,43 +10,50 @@ class ArticleModel(BaseModel):
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
|
||||
async def post(self, article: ArticleModel):
|
||||
return web.json_response(article.dict())
|
||||
|
||||
|
||||
async def test_post_an_article_without_required_field_should_return_an_error_message(aiohttp_client, loop):
|
||||
async def test_post_an_article_without_required_field_should_return_an_error_message(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.post('/article', json={})
|
||||
resp = await client.post("/article", json={})
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == [{'loc': ['name'],
|
||||
'msg': 'field required',
|
||||
'type': 'value_error.missing'}]
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == [
|
||||
{"loc": ["name"], "msg": "field required", "type": "value_error.missing"}
|
||||
]
|
||||
|
||||
|
||||
async def test_post_an_article_with_wrong_type_field_should_return_an_error_message(aiohttp_client, loop):
|
||||
async def test_post_an_article_with_wrong_type_field_should_return_an_error_message(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.post('/article', json={'name': 'foo', 'nb_page': 'foo'})
|
||||
resp = await client.post("/article", json={"name": "foo", "nb_page": "foo"})
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == [{'loc': ['nb_page'],
|
||||
'msg': 'value is not a valid integer',
|
||||
'type': 'type_error.integer'}]
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"loc": ["nb_page"],
|
||||
"msg": "value is not a valid integer",
|
||||
"type": "type_error.integer",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_post_a_valid_article_should_return_the_parsed_type(aiohttp_client, loop):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.post('/article', json={'name': 'foo', 'nb_page': 3})
|
||||
resp = await client.post("/article", json={"name": "foo", "nb_page": 3})
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'name': 'foo', 'nb_page': 3}
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == {"name": "foo", "nb_page": 3}
|
||||
|
||||
@@ -5,7 +5,6 @@ import json
|
||||
|
||||
|
||||
class JSONEncoder(json.JSONEncoder):
|
||||
|
||||
def default(self, o):
|
||||
if isinstance(o, datetime):
|
||||
return o.isoformat()
|
||||
@@ -14,54 +13,75 @@ class JSONEncoder(json.JSONEncoder):
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
|
||||
async def get(self, *, signature_expired: datetime):
|
||||
return web.json_response({'signature': signature_expired}, dumps=JSONEncoder().encode)
|
||||
return web.json_response(
|
||||
{"signature": signature_expired}, dumps=JSONEncoder().encode
|
||||
)
|
||||
|
||||
|
||||
async def test_get_article_without_required_header_should_return_an_error_message(aiohttp_client, loop):
|
||||
async def test_get_article_without_required_header_should_return_an_error_message(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article', headers={})
|
||||
resp = await client.get("/article", headers={})
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == [{'loc': ['signature_expired'],
|
||||
'msg': 'field required',
|
||||
'type': 'value_error.missing'}]
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"loc": ["signature_expired"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_get_article_with_wrong_header_type_should_return_an_error_message(aiohttp_client, loop):
|
||||
async def test_get_article_with_wrong_header_type_should_return_an_error_message(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article', headers={'signature_expired': 'foo'})
|
||||
resp = await client.get("/article", headers={"signature_expired": "foo"})
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == [{'loc': ['signature_expired'],
|
||||
'msg': 'invalid datetime format',
|
||||
'type': 'value_error.datetime'}]
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"loc": ["signature_expired"],
|
||||
"msg": "invalid datetime format",
|
||||
"type": "value_error.datetime",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_get_article_with_valid_header_should_return_the_parsed_type(aiohttp_client, loop):
|
||||
async def test_get_article_with_valid_header_should_return_the_parsed_type(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article', headers={'signature_expired': '2020-10-04T18:01:00'})
|
||||
resp = await client.get(
|
||||
"/article", headers={"signature_expired": "2020-10-04T18:01:00"}
|
||||
)
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'signature': '2020-10-04T18:01:00'}
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == {"signature": "2020-10-04T18:01:00"}
|
||||
|
||||
|
||||
async def test_get_article_with_valid_header_containing_hyphen_should_be_returned(aiohttp_client, loop):
|
||||
async def test_get_article_with_valid_header_containing_hyphen_should_be_returned(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article', headers={'Signature-Expired': '2020-10-04T18:01:00'})
|
||||
resp = await client.get(
|
||||
"/article", headers={"Signature-Expired": "2020-10-04T18:01:00"}
|
||||
)
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'signature': '2020-10-04T18:01:00'}
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == {"signature": "2020-10-04T18:01:00"}
|
||||
|
||||
@@ -3,18 +3,18 @@ from aiohttp_pydantic import PydanticView
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
|
||||
async def get(self, author_id: str, tag: str, date: int, /):
|
||||
return web.json_response({'path': [author_id, tag, date]})
|
||||
async def get(self, author_id: str, tag: str, date: int, /):
|
||||
return web.json_response({"path": [author_id, tag, date]})
|
||||
|
||||
|
||||
async def test_get_article_without_required_qs_should_return_an_error_message(aiohttp_client, loop):
|
||||
async def test_get_article_without_required_qs_should_return_an_error_message(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article/{author_id}/tag/{tag}/before/{date}', ArticleView)
|
||||
app.router.add_view("/article/{author_id}/tag/{tag}/before/{date}", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article/1234/tag/music/before/1980')
|
||||
resp = await client.get("/article/1234/tag/music/before/1980")
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'path': ['1234', 'music', 1980]}
|
||||
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == {"path": ["1234", "music", 1980]}
|
||||
|
||||
@@ -3,43 +3,56 @@ from aiohttp_pydantic import PydanticView
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
|
||||
async def get(self, with_comments: bool):
|
||||
return web.json_response({'with_comments': with_comments})
|
||||
return web.json_response({"with_comments": with_comments})
|
||||
|
||||
|
||||
async def test_get_article_without_required_qs_should_return_an_error_message(aiohttp_client, loop):
|
||||
async def test_get_article_without_required_qs_should_return_an_error_message(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article')
|
||||
resp = await client.get("/article")
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == [{'loc': ['with_comments'],
|
||||
'msg': 'field required',
|
||||
'type': 'value_error.missing'}]
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"loc": ["with_comments"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_get_article_with_wrong_qs_type_should_return_an_error_message(aiohttp_client, loop):
|
||||
async def test_get_article_with_wrong_qs_type_should_return_an_error_message(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article', params={'with_comments': 'foo'})
|
||||
resp = await client.get("/article", params={"with_comments": "foo"})
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == [{'loc': ['with_comments'],
|
||||
'msg': 'value could not be parsed to a boolean',
|
||||
'type': 'type_error.bool'}]
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"loc": ["with_comments"],
|
||||
"msg": "value could not be parsed to a boolean",
|
||||
"type": "type_error.bool",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_get_article_with_valid_qs_should_return_the_parsed_type(aiohttp_client, loop):
|
||||
async def test_get_article_with_valid_qs_should_return_the_parsed_type(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article', params={'with_comments': 'yes'})
|
||||
resp = await client.get("/article", params={"with_comments": "yes"})
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'with_comments': True}
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == {"with_comments": True}
|
||||
|
||||
Reference in New Issue
Block a user