Skip to content

Commit

Permalink
implement pydantic_factory for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grillazz committed Oct 17, 2024
1 parent 0b301b7 commit b0cfe92
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 76 deletions.
1 change: 0 additions & 1 deletion app/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext.asyncio import AsyncSession

from functools import wraps

Expand Down
105 changes: 30 additions & 75 deletions tests/api/test_stuff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,118 +5,73 @@
from dirty_equals import IsUUID

from polyfactory.factories.pydantic_factory import ModelFactory
from polyfactory.pytest_plugin import register_fixture

from app.schemas.stuff import StuffSchema


pytestmark = pytest.mark.anyio


class StuffFactory(ModelFactory[StuffSchema]):
__model__ = StuffSchema


async def test_add_stuff(client: AsyncClient):

_stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")[0],

response = await client.post("/stuff", json=_stuff)
print(response.json())
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
response = await client.post("/stuff", json=stuff)
assert response.status_code == status.HTTP_201_CREATED


assert response.json() == snapshot(
{
"id": IsUUID(4),
"name": _stuff["name"],
"description": _stuff["description"],
"name": stuff["name"],
"description": stuff["description"],
}
)


@pytest.mark.parametrize(
"payload, status_code",
(
(
{"name": "motorhead-0", "description": "we play rock and roll"},
status.HTTP_200_OK,
),
),
)
async def test_get_stuff(client: AsyncClient, payload: dict, status_code: int):
await client.post("/stuff", json=payload)
name = payload["name"]
async def test_get_stuff(client: AsyncClient):
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
await client.post("/stuff", json=stuff)
name = stuff["name"]
response = await client.get(f"/stuff/{name}")
assert response.status_code == status_code
assert response.status_code == status.HTTP_200_OK
assert response.json() == snapshot(
{
"id": IsUUID(4),
"name": "motorhead-0",
"description": "we play rock and roll",
"name": stuff["name"],
"description": stuff["description"],
}
)





@pytest.mark.parametrize(
"payload, status_code",
(
(
{"name": "motorhead-1", "description": "we play rock and roll"},
status.HTTP_200_OK,
),
),
)
async def test_delete_stuff(client: AsyncClient, payload: dict, status_code: int):
response = await client.post("/stuff", json=payload)
name = response.json()["name"]
async def test_delete_stuff(client: AsyncClient):
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
await client.post("/stuff", json=stuff)
name = stuff["name"]
response = await client.delete(f"/stuff/{name}")
assert response.status_code == status_code
assert response.status_code == status.HTTP_200_OK
assert response.json() == snapshot(True)


@pytest.mark.parametrize(
"payload, status_code",
(
(
{"name": "motorhead-2", "description": "we play rock and roll"},
status.HTTP_200_OK,
),
),
)
@pytest.mark.parametrize(
"patch_payload, patch_status_code",
(
(
{"name": "motorhead-2", "description": "we play loud"},
status.HTTP_200_OK,
),
),
)
async def test_update_stuff(
client: AsyncClient,
payload: dict,
status_code: int,
patch_payload: dict,
patch_status_code: int,
):
response = await client.post("/stuff", json=payload)
async def test_update_stuff(client: AsyncClient):
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
response = await client.post("/stuff", json=stuff)
assert response.json() == snapshot(
{
"id": IsUUID(4),
"name": "motorhead-2",
"description": "we play rock and roll",
"name": stuff["name"],
"description": stuff["description"],
}
)
name = payload["name"]
response = await client.patch(f"/stuff/{name}", json=patch_payload)
name = stuff["name"]
response = await client.patch(
f"/stuff/{name}",
json={"name": stuff["name"], "description": "we play rock and roll"},
)
assert response.json() == snapshot(
{
"id": IsUUID(4),
"name": "motorhead-2",
"description": "we play loud",
"name": stuff["name"],
"description": "we play rock and roll",
}
)
assert response.status_code == patch_status_code
assert response.status_code == status.HTTP_200_OK

0 comments on commit b0cfe92

Please sign in to comment.