feat: update pydantic

This commit is contained in:
Georg K
2023-07-14 03:31:33 +03:00
parent 26fd6fa19f
commit a94c9d4863
9 changed files with 159 additions and 138 deletions

View File

@@ -11,11 +11,11 @@ from aiohttp_pydantic.injectors import Group
class ArticleView(PydanticView):
async def get(
self,
with_comments: bool,
age: Optional[int] = None,
nb_items: int = 7,
tags: List[str] = Field(default_factory=list),
self,
with_comments: bool,
age: Optional[int] = None,
nb_items: int = 7,
tags: List[str] = Field(default_factory=list),
):
return web.json_response(
{
@@ -42,9 +42,9 @@ class Pagination(Group):
class ArticleViewWithPaginationGroup(PydanticView):
async def get(
self,
with_comments: bool,
page: Pagination,
self,
with_comments: bool,
page: Pagination,
):
return web.json_response(
{
@@ -70,7 +70,7 @@ class ArticleViewWithEnumInQuery(PydanticView):
async def test_get_article_without_required_qs_should_return_an_error_message(
aiohttp_client, event_loop
aiohttp_client, event_loop
):
app = web.Application()
app.router.add_view("/article", ArticleView)
@@ -79,18 +79,21 @@ async def test_get_article_without_required_qs_should_return_an_error_message(
resp = await client.get("/article")
assert resp.status == 400
assert resp.content_type == "application/json"
assert await resp.json() == [
{
"in": "query string",
"loc": ["with_comments"],
"msg": "field required",
"type": "value_error.missing",
'input': {},
'loc': ['with_comments'],
'loc_in': 'query string',
'msg': 'Field required',
'type': 'missing',
'url': 'https://errors.pydantic.dev/2.1.2/v/missing'
}
]
async def test_get_article_with_wrong_qs_type_should_return_an_error_message(
aiohttp_client, event_loop
aiohttp_client, event_loop
):
app = web.Application()
app.router.add_view("/article", ArticleView)
@@ -101,16 +104,18 @@ async def test_get_article_with_wrong_qs_type_should_return_an_error_message(
assert resp.content_type == "application/json"
assert await resp.json() == [
{
"in": "query string",
"loc": ["with_comments"],
"msg": "value could not be parsed to a boolean",
"type": "type_error.bool",
'input': 'foo',
'loc': ['with_comments'],
'loc_in': 'query string',
'msg': 'Input should be a valid boolean, unable to interpret input',
'type': 'bool_parsing',
'url': 'https://errors.pydantic.dev/2.1.2/v/bool_parsing'
}
]
async def test_get_article_with_valid_qs_should_return_the_parsed_type(
aiohttp_client, event_loop
aiohttp_client, event_loop
):
app = web.Application()
app.router.add_view("/article", ArticleView)
@@ -129,7 +134,7 @@ async def test_get_article_with_valid_qs_should_return_the_parsed_type(
async def test_get_article_with_valid_qs_and_omitted_optional_should_return_default_value(
aiohttp_client, event_loop
aiohttp_client, event_loop
):
app = web.Application()
app.router.add_view("/article", ArticleView)
@@ -148,7 +153,7 @@ async def test_get_article_with_valid_qs_and_omitted_optional_should_return_defa
async def test_get_article_with_multiple_value_for_qs_age_must_failed(
aiohttp_client, event_loop
aiohttp_client, event_loop
):
app = web.Application()
app.router.add_view("/article", ArticleView)
@@ -158,10 +163,12 @@ async def test_get_article_with_multiple_value_for_qs_age_must_failed(
resp = await client.get("/article", params={"age": ["2", "3"], "with_comments": 1})
assert await resp.json() == [
{
"in": "query string",
"loc": ["age"],
"msg": "value is not a valid integer",
"type": "type_error.integer",
'input': ['2', '3'],
'loc': ['age'],
'loc_in': 'query string',
'msg': 'Input should be a valid integer',
'type': 'int_type',
'url': 'https://errors.pydantic.dev/2.1.2/v/int_type'
}
]
assert resp.status == 400
@@ -215,10 +222,12 @@ async def test_get_article_without_required_field_page(aiohttp_client, event_loo
resp = await client.get("/article", params={"with_comments": 1})
assert await resp.json() == [
{
"in": "query string",
"loc": ["page_num"],
"msg": "field required",
"type": "value_error.missing",
'input': {'with_comments': '1'},
'loc': ['page_num'],
'loc_in': 'query string',
'msg': 'Field required',
'type': 'missing',
'url': 'https://errors.pydantic.dev/2.1.2/v/missing'
}
]
assert resp.status == 400
@@ -276,10 +285,13 @@ async def test_get_article_with_page_and_wrong_page_size(aiohttp_client, event_l
)
assert await resp.json() == [
{
"in": "query string",
"loc": ["page_size"],
"msg": "value is not a valid integer",
"type": "type_error.integer",
'input': 'large',
'loc': ['page_size'],
'loc_in': 'query string',
'msg': 'Input should be a valid integer, unable to parse string as an '
'integer',
'type': 'int_parsing',
'url': 'https://errors.pydantic.dev/2.1.2/v/int_parsing'
}
]
assert resp.status == 400