Add sub-app to generate open api spec

This commit is contained in:
Vincent Maillol
2020-10-23 19:44:44 +02:00
parent 1ffde607c9
commit d6b5fc26f3
24 changed files with 932 additions and 124 deletions

View File

@@ -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}