Skip to content

Commit b0cfe92

Browse files
committed
implement pydantic_factory for tests
1 parent 0b301b7 commit b0cfe92

File tree

2 files changed

+30
-76
lines changed

2 files changed

+30
-76
lines changed

app/utils/decorators.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from sqlalchemy.dialects import postgresql
2-
from sqlalchemy.ext.asyncio import AsyncSession
32

43
from functools import wraps
54

tests/api/test_stuff.py

Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,118 +5,73 @@
55
from dirty_equals import IsUUID
66

77
from polyfactory.factories.pydantic_factory import ModelFactory
8-
from polyfactory.pytest_plugin import register_fixture
98

109
from app.schemas.stuff import StuffSchema
1110

12-
1311
pytestmark = pytest.mark.anyio
1412

13+
1514
class StuffFactory(ModelFactory[StuffSchema]):
1615
__model__ = StuffSchema
1716

1817

1918
async def test_add_stuff(client: AsyncClient):
20-
21-
_stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")[0],
22-
23-
response = await client.post("/stuff", json=_stuff)
24-
print(response.json())
19+
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
20+
response = await client.post("/stuff", json=stuff)
2521
assert response.status_code == status.HTTP_201_CREATED
26-
27-
2822
assert response.json() == snapshot(
2923
{
3024
"id": IsUUID(4),
31-
"name": _stuff["name"],
32-
"description": _stuff["description"],
25+
"name": stuff["name"],
26+
"description": stuff["description"],
3327
}
3428
)
3529

3630

37-
@pytest.mark.parametrize(
38-
"payload, status_code",
39-
(
40-
(
41-
{"name": "motorhead-0", "description": "we play rock and roll"},
42-
status.HTTP_200_OK,
43-
),
44-
),
45-
)
46-
async def test_get_stuff(client: AsyncClient, payload: dict, status_code: int):
47-
await client.post("/stuff", json=payload)
48-
name = payload["name"]
31+
async def test_get_stuff(client: AsyncClient):
32+
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
33+
await client.post("/stuff", json=stuff)
34+
name = stuff["name"]
4935
response = await client.get(f"/stuff/{name}")
50-
assert response.status_code == status_code
36+
assert response.status_code == status.HTTP_200_OK
5137
assert response.json() == snapshot(
5238
{
5339
"id": IsUUID(4),
54-
"name": "motorhead-0",
55-
"description": "we play rock and roll",
40+
"name": stuff["name"],
41+
"description": stuff["description"],
5642
}
5743
)
5844

5945

60-
61-
62-
63-
@pytest.mark.parametrize(
64-
"payload, status_code",
65-
(
66-
(
67-
{"name": "motorhead-1", "description": "we play rock and roll"},
68-
status.HTTP_200_OK,
69-
),
70-
),
71-
)
72-
async def test_delete_stuff(client: AsyncClient, payload: dict, status_code: int):
73-
response = await client.post("/stuff", json=payload)
74-
name = response.json()["name"]
46+
async def test_delete_stuff(client: AsyncClient):
47+
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
48+
await client.post("/stuff", json=stuff)
49+
name = stuff["name"]
7550
response = await client.delete(f"/stuff/{name}")
76-
assert response.status_code == status_code
51+
assert response.status_code == status.HTTP_200_OK
7752
assert response.json() == snapshot(True)
7853

7954

80-
@pytest.mark.parametrize(
81-
"payload, status_code",
82-
(
83-
(
84-
{"name": "motorhead-2", "description": "we play rock and roll"},
85-
status.HTTP_200_OK,
86-
),
87-
),
88-
)
89-
@pytest.mark.parametrize(
90-
"patch_payload, patch_status_code",
91-
(
92-
(
93-
{"name": "motorhead-2", "description": "we play loud"},
94-
status.HTTP_200_OK,
95-
),
96-
),
97-
)
98-
async def test_update_stuff(
99-
client: AsyncClient,
100-
payload: dict,
101-
status_code: int,
102-
patch_payload: dict,
103-
patch_status_code: int,
104-
):
105-
response = await client.post("/stuff", json=payload)
55+
async def test_update_stuff(client: AsyncClient):
56+
stuff = StuffFactory.build(factory_use_constructors=True).model_dump(mode="json")
57+
response = await client.post("/stuff", json=stuff)
10658
assert response.json() == snapshot(
10759
{
10860
"id": IsUUID(4),
109-
"name": "motorhead-2",
110-
"description": "we play rock and roll",
61+
"name": stuff["name"],
62+
"description": stuff["description"],
11163
}
11264
)
113-
name = payload["name"]
114-
response = await client.patch(f"/stuff/{name}", json=patch_payload)
65+
name = stuff["name"]
66+
response = await client.patch(
67+
f"/stuff/{name}",
68+
json={"name": stuff["name"], "description": "we play rock and roll"},
69+
)
11570
assert response.json() == snapshot(
11671
{
11772
"id": IsUUID(4),
118-
"name": "motorhead-2",
119-
"description": "we play loud",
73+
"name": stuff["name"],
74+
"description": "we play rock and roll",
12075
}
12176
)
122-
assert response.status_code == patch_status_code
77+
assert response.status_code == status.HTTP_200_OK

0 commit comments

Comments
 (0)