feat: update pydantic
This commit is contained in:
@@ -3,21 +3,21 @@ from __future__ import annotations
|
||||
from typing import Iterator, List, Optional
|
||||
|
||||
from aiohttp import web
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, RootModel
|
||||
|
||||
from aiohttp_pydantic import PydanticView
|
||||
|
||||
|
||||
class ArticleModel(BaseModel):
|
||||
name: str
|
||||
nb_page: Optional[int]
|
||||
nb_page: Optional[int] = None
|
||||
|
||||
|
||||
class ArticleModels(BaseModel):
|
||||
__root__: List[ArticleModel]
|
||||
class ArticleModels(RootModel):
|
||||
root: List[ArticleModel]
|
||||
|
||||
def __iter__(self) -> Iterator[ArticleModel]:
|
||||
return iter(self.__root__)
|
||||
return iter(self.root)
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
@@ -29,7 +29,7 @@ class ArticleView(PydanticView):
|
||||
|
||||
|
||||
async def test_post_an_article_without_required_field_should_return_an_error_message(
|
||||
aiohttp_client, event_loop
|
||||
aiohttp_client, event_loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
@@ -38,18 +38,21 @@ async def test_post_an_article_without_required_field_should_return_an_error_mes
|
||||
resp = await client.post("/article", json={})
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == "application/json"
|
||||
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"in": "body",
|
||||
"loc": ["name"],
|
||||
"msg": "field required",
|
||||
"type": "value_error.missing",
|
||||
'input': {},
|
||||
'loc': ['name'],
|
||||
'loc_in': 'body',
|
||||
'msg': 'Field required',
|
||||
'type': 'missing',
|
||||
'url': 'https://errors.pydantic.dev/2.1.2/v/missing'
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_post_an_article_with_wrong_type_field_should_return_an_error_message(
|
||||
aiohttp_client, event_loop
|
||||
aiohttp_client, event_loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
@@ -60,10 +63,12 @@ async def test_post_an_article_with_wrong_type_field_should_return_an_error_mess
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"in": "body",
|
||||
"loc": ["nb_page"],
|
||||
"msg": "value is not a valid integer",
|
||||
"type": "type_error.integer",
|
||||
'input': 'foo',
|
||||
'loc': ['nb_page'],
|
||||
'loc_in': 'body',
|
||||
'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'
|
||||
}
|
||||
]
|
||||
|
||||
@@ -81,7 +86,7 @@ async def test_post_an_array_json_is_supported(aiohttp_client, event_loop):
|
||||
|
||||
|
||||
async def test_post_an_array_json_to_an_object_model_should_return_an_error(
|
||||
aiohttp_client, event_loop
|
||||
aiohttp_client, event_loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
@@ -101,7 +106,7 @@ async def test_post_an_array_json_to_an_object_model_should_return_an_error(
|
||||
|
||||
|
||||
async def test_post_an_object_json_to_a_list_model_should_return_an_error(
|
||||
aiohttp_client, event_loop
|
||||
aiohttp_client, event_loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
@@ -110,12 +115,15 @@ async def test_post_an_object_json_to_a_list_model_should_return_an_error(
|
||||
resp = await client.put("/article", json={"name": "foo", "nb_page": 3})
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == "application/json"
|
||||
|
||||
assert await resp.json() == [
|
||||
{
|
||||
"in": "body",
|
||||
"loc": ["__root__"],
|
||||
"msg": "value is not a valid list",
|
||||
"type": "type_error.list",
|
||||
'input': {'name': 'foo', 'nb_page': 3},
|
||||
'loc': [],
|
||||
'loc_in': 'body',
|
||||
'msg': 'Input should be a valid list',
|
||||
'type': 'list_type',
|
||||
'url': 'https://errors.pydantic.dev/2.1.2/v/list_type'
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user