Create aiohttp_pydantic
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
52
tests/test_validation_body.py
Normal file
52
tests/test_validation_body.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from aiohttp import web
|
||||
from aiohttp_pydantic import PydanticView
|
||||
|
||||
|
||||
class ArticleModel(BaseModel):
|
||||
name: str
|
||||
nb_page: Optional[int]
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
|
||||
async def post(self, article: ArticleModel):
|
||||
return web.Response(article.dict())
|
||||
|
||||
|
||||
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)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
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'}]
|
||||
|
||||
|
||||
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)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
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'}]
|
||||
|
||||
|
||||
async def test_post_a_valid_article_should_return_the_parsed_type(aiohttp_client, loop):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
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}
|
||||
45
tests/test_validation_query_string.py
Normal file
45
tests/test_validation_query_string.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from aiohttp import web
|
||||
from aiohttp_pydantic import PydanticView
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
|
||||
async def get(self, with_comments: bool):
|
||||
return web.json_response({'with_comments': with_comments})
|
||||
|
||||
|
||||
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)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
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'}]
|
||||
|
||||
|
||||
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)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
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'}]
|
||||
|
||||
|
||||
async def test_get_article_with_valide_qs_should_return_the_parsed_type(aiohttp_client, loop):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
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}
|
||||
Reference in New Issue
Block a user