mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
refactor tests
This commit is contained in:
parent
c2975fd260
commit
121246ce18
@ -3,6 +3,9 @@ from uuid import UUID
|
|||||||
import pytest
|
import pytest
|
||||||
from fastapi import status
|
from fastapi import status
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
from inline_snapshot import snapshot
|
||||||
|
from dirty_equals import IsStr, IsUUID, IsPositiveFloat
|
||||||
|
|
||||||
|
|
||||||
pytestmark = pytest.mark.anyio
|
pytestmark = pytest.mark.anyio
|
||||||
|
|
||||||
@ -19,7 +22,13 @@ pytestmark = pytest.mark.anyio
|
|||||||
async def test_add_stuff(client: AsyncClient, payload: dict, status_code: int):
|
async def test_add_stuff(client: AsyncClient, payload: dict, status_code: int):
|
||||||
response = await client.post("/stuff", json=payload)
|
response = await client.post("/stuff", json=payload)
|
||||||
assert response.status_code == status_code
|
assert response.status_code == status_code
|
||||||
assert payload["name"] == response.json()["name"]
|
assert response.json() == snapshot(
|
||||||
|
{
|
||||||
|
"id": IsUUID(4),
|
||||||
|
"name": "motorhead",
|
||||||
|
"description": "we play rock and roll",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -36,8 +45,13 @@ async def test_get_stuff(client: AsyncClient, payload: dict, status_code: int):
|
|||||||
name = payload["name"]
|
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_code
|
||||||
assert payload["name"] == response.json()["name"]
|
assert response.json() == snapshot(
|
||||||
assert UUID(response.json()["id"])
|
{
|
||||||
|
"id": IsUUID(4),
|
||||||
|
"name": "motorhead-0",
|
||||||
|
"description": "we play rock and roll",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -51,17 +65,17 @@ async def test_get_stuff(client: AsyncClient, payload: dict, status_code: int):
|
|||||||
)
|
)
|
||||||
async def test_delete_stuff(client: AsyncClient, payload: dict, status_code: int):
|
async def test_delete_stuff(client: AsyncClient, payload: dict, status_code: int):
|
||||||
response = await client.post("/stuff", json=payload)
|
response = await client.post("/stuff", json=payload)
|
||||||
print(response.json())
|
|
||||||
name = response.json()["name"]
|
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_code
|
||||||
|
assert response.json() == snapshot(True)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"payload, status_code",
|
"payload, status_code",
|
||||||
(
|
(
|
||||||
(
|
(
|
||||||
{"name": "motorhead", "description": "we play rock and roll"},
|
{"name": "motorhead-2", "description": "we play rock and roll"},
|
||||||
status.HTTP_200_OK,
|
status.HTTP_200_OK,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -70,7 +84,7 @@ async def test_delete_stuff(client: AsyncClient, payload: dict, status_code: int
|
|||||||
"patch_payload, patch_status_code",
|
"patch_payload, patch_status_code",
|
||||||
(
|
(
|
||||||
(
|
(
|
||||||
{"name": "motorhead", "description": "we play loud"},
|
{"name": "motorhead-2", "description": "we play loud"},
|
||||||
status.HTTP_200_OK,
|
status.HTTP_200_OK,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -82,9 +96,21 @@ async def test_update_stuff(
|
|||||||
patch_payload: dict,
|
patch_payload: dict,
|
||||||
patch_status_code: int,
|
patch_status_code: int,
|
||||||
):
|
):
|
||||||
await client.post("/stuff", json=payload)
|
response = await client.post("/stuff", json=payload)
|
||||||
|
assert response.json() == snapshot(
|
||||||
|
{
|
||||||
|
"id": IsUUID(4),
|
||||||
|
"name": "motorhead-2",
|
||||||
|
"description": "we play rock and roll",
|
||||||
|
}
|
||||||
|
)
|
||||||
name = payload["name"]
|
name = payload["name"]
|
||||||
response = await client.patch(f"/stuff/{name}", json=patch_payload)
|
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
|
assert response.status_code == patch_status_code
|
||||||
response = await client.get(f"/stuff/{name}")
|
|
||||||
assert patch_payload["description"] == response.json()["description"]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user