|
5 | 5 | from dirty_equals import IsUUID |
6 | 6 |
|
7 | 7 | from polyfactory.factories.pydantic_factory import ModelFactory |
8 | | -from polyfactory.pytest_plugin import register_fixture |
9 | 8 |
|
10 | 9 | from app.schemas.stuff import StuffSchema |
11 | 10 |
|
12 | | - |
13 | 11 | pytestmark = pytest.mark.anyio |
14 | 12 |
|
| 13 | + |
15 | 14 | class StuffFactory(ModelFactory[StuffSchema]): |
16 | 15 | __model__ = StuffSchema |
17 | 16 |
|
18 | 17 |
|
19 | 18 | 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) |
25 | 21 | assert response.status_code == status.HTTP_201_CREATED |
26 | | - |
27 | | - |
28 | 22 | assert response.json() == snapshot( |
29 | 23 | { |
30 | 24 | "id": IsUUID(4), |
31 | | - "name": _stuff["name"], |
32 | | - "description": _stuff["description"], |
| 25 | + "name": stuff["name"], |
| 26 | + "description": stuff["description"], |
33 | 27 | } |
34 | 28 | ) |
35 | 29 |
|
36 | 30 |
|
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"] |
49 | 35 | response = await client.get(f"/stuff/{name}") |
50 | | - assert response.status_code == status_code |
| 36 | + assert response.status_code == status.HTTP_200_OK |
51 | 37 | assert response.json() == snapshot( |
52 | 38 | { |
53 | 39 | "id": IsUUID(4), |
54 | | - "name": "motorhead-0", |
55 | | - "description": "we play rock and roll", |
| 40 | + "name": stuff["name"], |
| 41 | + "description": stuff["description"], |
56 | 42 | } |
57 | 43 | ) |
58 | 44 |
|
59 | 45 |
|
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"] |
75 | 50 | response = await client.delete(f"/stuff/{name}") |
76 | | - assert response.status_code == status_code |
| 51 | + assert response.status_code == status.HTTP_200_OK |
77 | 52 | assert response.json() == snapshot(True) |
78 | 53 |
|
79 | 54 |
|
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) |
106 | 58 | assert response.json() == snapshot( |
107 | 59 | { |
108 | 60 | "id": IsUUID(4), |
109 | | - "name": "motorhead-2", |
110 | | - "description": "we play rock and roll", |
| 61 | + "name": stuff["name"], |
| 62 | + "description": stuff["description"], |
111 | 63 | } |
112 | 64 | ) |
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 | + ) |
115 | 70 | assert response.json() == snapshot( |
116 | 71 | { |
117 | 72 | "id": IsUUID(4), |
118 | | - "name": "motorhead-2", |
119 | | - "description": "we play loud", |
| 73 | + "name": stuff["name"], |
| 74 | + "description": "we play rock and roll", |
120 | 75 | } |
121 | 76 | ) |
122 | | - assert response.status_code == patch_status_code |
| 77 | + assert response.status_code == status.HTTP_200_OK |
0 commit comments