Skip to content

Commit

Permalink
tests/fastapi - invalid use of parameter
Browse files Browse the repository at this point in the history
`Cannot use `Query` for path param 'pet_id'`
  • Loading branch information
commonism committed Apr 5, 2023
1 parent ae8837c commit 663acfa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
10 changes: 5 additions & 5 deletions tests/api/v1/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Optional

import starlette.status
from fastapi import FastAPI, APIRouter, Query, Body, Response
from fastapi import FastAPI, APIRouter, Body, Response, Path
from fastapi.responses import JSONResponse

from .schema import Pets, Pet, PetCreate, Error
Expand Down Expand Up @@ -66,8 +66,8 @@ def listPet(limit: Optional[int] = None) -> Pets:
return list(ZOO.values())


@router.get("/pets/{pet_id}", operation_id="getPet", response_model=Pet, responses={404: {"model": Error}})
def getPet(pet_id: int = Query(..., alias="petId")) -> Pets:
@router.get("/pets/{petId}", operation_id="getPet", response_model=Pet, responses={404: {"model": Error}})
def getPet(pet_id: int = Path(..., alias="petId")) -> Pets:
for k, v in ZOO.items():
if pet_id == v.id:
return v
Expand All @@ -78,8 +78,8 @@ def getPet(pet_id: int = Query(..., alias="petId")) -> Pets:
)


@router.delete("/pets/{pet_id}", operation_id="deletePet", responses={204: {"model": None}, 404: {"model": Error}})
def deletePet(response: Response, pet_id: int = Query(..., alias="petId")) -> Pets:
@router.delete("/pets/{petId}", operation_id="deletePet", responses={204: {"model": None}, 404: {"model": Error}})
def deletePet(response: Response, pet_id: int = Path(..., alias="petId")) -> Pets:
for k, v in ZOO.items():
if pet_id == v.id:
del ZOO[k]
Expand Down
12 changes: 5 additions & 7 deletions tests/api/v2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional

import starlette.status
from fastapi import Query, Body, Response, APIRouter
from fastapi import Body, Response, APIRouter, Path
from fastapi.responses import JSONResponse
from fastapi_versioning import version

Expand Down Expand Up @@ -54,10 +54,8 @@ def listPet(limit: Optional[int] = None) -> schema.Pets:
return list(ZOO.values())


@router.get(
"/pets/{pet_id}", operation_id="getPet", response_model=schema.Pet, responses={404: {"model": schema.Error}}
)
def getPet(pet_id: str = Query(..., alias="petId")) -> schema.Pets:
@router.get("/pets/{petId}", operation_id="getPet", response_model=schema.Pet, responses={404: {"model": schema.Error}})
def getPet(pet_id: str = Path(..., alias="petId")) -> schema.Pets:
for k, v in ZOO.items():
if pet_id == v.identifier:
return v
Expand All @@ -69,9 +67,9 @@ def getPet(pet_id: str = Query(..., alias="petId")) -> schema.Pets:


@router.delete(
"/pets/{pet_id}", operation_id="deletePet", responses={204: {"model": None}, 404: {"model": schema.Error}}
"/pets/{petId}", operation_id="deletePet", responses={204: {"model": None}, 404: {"model": schema.Error}}
)
def deletePet(response: Response, pet_id: int = Query(..., alias="petId")) -> None:
def deletePet(response: Response, pet_id: int = Path(..., alias="petId")) -> None:
for k, v in ZOO.items():
if pet_id == v.identifier:
del ZOO[k]
Expand Down
8 changes: 4 additions & 4 deletions tests/apiv1_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ async def test_listPet(event_loop, server, client):
@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires asyncio.to_thread")
async def test_getPet(event_loop, server, client):
h, pet = await asyncio.to_thread(client._.createPet, **randomPet(uuid.uuid4()))
r = await asyncio.to_thread(client._.getPet, parameters={"pet_id": pet.id})
r = await asyncio.to_thread(client._.getPet, parameters={"petId": pet.id})
assert type(r).schema() == type(pet).schema()
assert r.id == pet.id

r = await asyncio.to_thread(client._.getPet, parameters={"pet_id": -1})
r = await asyncio.to_thread(client._.getPet, parameters={"petId": -1})
assert type(r).schema() == client.components.schemas["Error"].get_type().schema()


@pytest.mark.asyncio
@pytest.mark.skipif(sys.version_info < (3, 9), reason="requires asyncio.to_thread")
async def test_deletePet(event_loop, server, client):
r = await asyncio.to_thread(client._.deletePet, parameters={"pet_id": -1})
r = await asyncio.to_thread(client._.deletePet, parameters={"petId": -1})
assert type(r).schema() == client.components.schemas["Error"].get_type().schema()

await asyncio.to_thread(client._.createPet, **randomPet(uuid.uuid4()))
zoo = await asyncio.to_thread(client._.listPet)
for pet in zoo:
await asyncio.to_thread(client._.deletePet, parameters={"pet_id": pet.id})
await asyncio.to_thread(client._.deletePet, parameters={"petId": pet.id})
8 changes: 4 additions & 4 deletions tests/apiv2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,24 @@ async def test_listPet(event_loop, server, client):
@pytest.mark.asyncio
async def test_getPet(event_loop, server, client):
pet = await client._.createPet(data=randomPet(client, str(uuid.uuid4())))
r = await client._.getPet(parameters={"pet_id": pet.__root__.identifier})
r = await client._.getPet(parameters={"petId": pet.__root__.identifier})
assert type(r.__root__).schema() == type(pet.__root__).schema()

r = await client._.getPet(parameters={"pet_id": "-1"})
r = await client._.getPet(parameters={"petId": "-1"})
assert type(r).schema() == client.components.schemas["Error"].get_type().schema()


@pytest.mark.asyncio
async def test_deletePet(event_loop, server, client):
r = await client._.deletePet(parameters={"pet_id": -1})
r = await client._.deletePet(parameters={"petId": -1})
assert type(r).schema() == client.components.schemas["Error"].get_type().schema()

await client._.createPet(data=randomPet(client, str(uuid.uuid4())))
zoo = await client._.listPet()
for pet in zoo:
while hasattr(pet, "__root__"):
pet = pet.__root__
await client._.deletePet(parameters={"pet_id": pet.identifier})
await client._.deletePet(parameters={"petId": pet.identifier})


@pytest.mark.asyncio
Expand Down

0 comments on commit 663acfa

Please sign in to comment.