query string accept multiple values for same parameter key (#11)
This commit is contained in:
@@ -59,7 +59,7 @@ class PetItemView(PydanticView):
|
||||
return web.json_response()
|
||||
|
||||
|
||||
class TestResponseReturnASimpleType(PydanticView):
|
||||
class ViewResponseReturnASimpleType(PydanticView):
|
||||
async def get(self) -> r200[int]:
|
||||
"""
|
||||
Status Codes:
|
||||
@@ -73,7 +73,7 @@ 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)
|
||||
app.router.add_view("/simple-type", TestResponseReturnASimpleType)
|
||||
app.router.add_view("/simple-type", ViewResponseReturnASimpleType)
|
||||
oas.setup(app)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
@@ -7,10 +7,19 @@ from aiohttp_pydantic import PydanticView
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
async def get(
|
||||
self, with_comments: bool, age: Optional[int] = None, nb_items: int = 7
|
||||
self,
|
||||
with_comments: bool,
|
||||
age: Optional[int] = None,
|
||||
nb_items: int = 7,
|
||||
tags: List[str] = [],
|
||||
):
|
||||
return web.json_response(
|
||||
{"with_comments": with_comments, "age": age, "nb_items": nb_items}
|
||||
{
|
||||
"with_comments": with_comments,
|
||||
"age": age,
|
||||
"nb_items": nb_items,
|
||||
"tags": tags,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -65,7 +74,12 @@ async def test_get_article_with_valid_qs_should_return_the_parsed_type(
|
||||
resp = await client.get("/article", params={"with_comments": "yes", "age": 3})
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == "application/json"
|
||||
assert await resp.json() == {"with_comments": True, "age": 3, "nb_items": 7}
|
||||
assert await resp.json() == {
|
||||
"with_comments": True,
|
||||
"age": 3,
|
||||
"nb_items": 7,
|
||||
"tags": [],
|
||||
}
|
||||
|
||||
|
||||
async def test_get_article_with_valid_qs_and_omitted_optional_should_return_default_value(
|
||||
@@ -77,6 +91,70 @@ async def test_get_article_with_valid_qs_and_omitted_optional_should_return_defa
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.get("/article", params={"with_comments": "yes"})
|
||||
assert await resp.json() == {"with_comments": True, "age": None, "nb_items": 7}
|
||||
assert await resp.json() == {
|
||||
"with_comments": True,
|
||||
"age": None,
|
||||
"nb_items": 7,
|
||||
"tags": [],
|
||||
}
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == "application/json"
|
||||
|
||||
|
||||
async def test_get_article_with_multiple_value_for_qs_age_must_failed(
|
||||
aiohttp_client, loop
|
||||
):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
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",
|
||||
}
|
||||
]
|
||||
assert resp.status == 400
|
||||
assert resp.content_type == "application/json"
|
||||
|
||||
|
||||
async def test_get_article_with_multiple_value_of_tags(aiohttp_client, loop):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.get(
|
||||
"/article", params={"age": 2, "with_comments": 1, "tags": ["aa", "bb"]}
|
||||
)
|
||||
assert await resp.json() == {
|
||||
"age": 2,
|
||||
"nb_items": 7,
|
||||
"tags": ["aa", "bb"],
|
||||
"with_comments": True,
|
||||
}
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == "application/json"
|
||||
|
||||
|
||||
async def test_get_article_with_one_value_of_tags_must_be_a_list(aiohttp_client, loop):
|
||||
app = web.Application()
|
||||
app.router.add_view("/article", ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
|
||||
resp = await client.get(
|
||||
"/article", params={"age": 2, "with_comments": 1, "tags": ["aa"]}
|
||||
)
|
||||
assert await resp.json() == {
|
||||
"age": 2,
|
||||
"nb_items": 7,
|
||||
"tags": ["aa"],
|
||||
"with_comments": True,
|
||||
}
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == "application/json"
|
||||
|
||||
Reference in New Issue
Block a user