mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
implement pydantic_factory for tests
This commit is contained in:
parent
0b301b798e
commit
b0cfe92268
@ -1,5 +1,4 @@
|
|||||||
from sqlalchemy.dialects import postgresql
|
from sqlalchemy.dialects import postgresql
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
|
||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
@ -5,118 +5,73 @@ from inline_snapshot import snapshot
|
|||||||
from dirty_equals import IsUUID
|
from dirty_equals import IsUUID
|
||||||
|
|
||||||
from polyfactory.factories.pydantic_factory import ModelFactory
|
from polyfactory.factories.pydantic_factory import ModelFactory
|
||||||
from polyfactory.pytest_plugin import register_fixture
|
|
||||||
|
|
||||||
from app.schemas.stuff import StuffSchema
|
from app.schemas.stuff import StuffSchema
|
||||||
|
|
||||||
|
|
||||||
pytestmark = pytest.mark.anyio
|
pytestmark = pytest.mark.anyio
|
||||||
|
|
||||||
|
|
||||||
class StuffFactory(ModelFactory[StuffSchema]):
|
class StuffFactory(ModelFactory[StuffSchema]):
|
||||||
__model__ = StuffSchema
|
__model__ = StuffSchema
|
||||||
|
|
||||||
|
|
||||||
async def test_add_stuff(client: AsyncClient):
|
async def test_add_stuff(client: AsyncClient):
|
||||||
|
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
|
||||||
_stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")[0],
|
response = await client.post("/stuff", json=stuff)
|
||||||
|
|
||||||
response = await client.post("/stuff", json=_stuff)
|
|
||||||
print(response.json())
|
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
|
|
||||||
|
|
||||||
assert response.json() == snapshot(
|
assert response.json() == snapshot(
|
||||||
{
|
{
|
||||||
"id": IsUUID(4),
|
"id": IsUUID(4),
|
||||||
"name": _stuff["name"],
|
"name": stuff["name"],
|
||||||
"description": _stuff["description"],
|
"description": stuff["description"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
async def test_get_stuff(client: AsyncClient):
|
||||||
"payload, status_code",
|
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
|
||||||
(
|
await client.post("/stuff", json=stuff)
|
||||||
(
|
name = stuff["name"]
|
||||||
{"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"]
|
|
||||||
response = await client.get(f"/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(
|
assert response.json() == snapshot(
|
||||||
{
|
{
|
||||||
"id": IsUUID(4),
|
"id": IsUUID(4),
|
||||||
"name": "motorhead-0",
|
"name": stuff["name"],
|
||||||
"description": "we play rock and roll",
|
"description": stuff["description"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_delete_stuff(client: AsyncClient):
|
||||||
|
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
|
||||||
|
await client.post("/stuff", json=stuff)
|
||||||
@pytest.mark.parametrize(
|
name = stuff["name"]
|
||||||
"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"]
|
|
||||||
response = await client.delete(f"/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)
|
assert response.json() == snapshot(True)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
async def test_update_stuff(client: AsyncClient):
|
||||||
"payload, status_code",
|
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
|
||||||
(
|
response = await client.post("/stuff", json=stuff)
|
||||||
(
|
|
||||||
{"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)
|
|
||||||
assert response.json() == snapshot(
|
assert response.json() == snapshot(
|
||||||
{
|
{
|
||||||
"id": IsUUID(4),
|
"id": IsUUID(4),
|
||||||
"name": "motorhead-2",
|
"name": stuff["name"],
|
||||||
|
"description": stuff["description"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
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": stuff["name"],
|
||||||
"description": "we play rock and roll",
|
"description": "we play rock and roll",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
name = payload["name"]
|
assert response.status_code == status.HTTP_200_OK
|
||||||
response = await client.patch(f"/stuff/{name}", json=patch_payload)
|
|
||||||
assert response.json() == snapshot(
|
|
||||||
{
|
|
||||||
"id": IsUUID(4),
|
|
||||||
"name": "motorhead-2",
|
|
||||||
"description": "we play loud",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
assert response.status_code == patch_status_code
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user