test: upgrade backend test suite, implement unit tests, and modernize CRUD patterns
Build Backend / build (push) Successful in 6m39s
Build Backend / build (push) Successful in 6m39s
This commit is contained in:
@@ -1,4 +1,54 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from app.main import app
|
||||
from datetime import datetime, timezone
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_session(api_client):
|
||||
async with api_client as ac:
|
||||
start_time = datetime.now(timezone.utc).isoformat()
|
||||
payload = {"start_time": start_time}
|
||||
response = await ac.post("/api/sessions", json=payload)
|
||||
assert response.status_code == 201
|
||||
data = response.json()
|
||||
assert datetime.fromisoformat(data["start_time"]).replace(
|
||||
tzinfo=None
|
||||
) == datetime.fromisoformat(start_time).replace(tzinfo=None)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_session_already_open(api_client):
|
||||
async with api_client as ac:
|
||||
start_time = datetime.now(timezone.utc).isoformat()
|
||||
await ac.post("/api/sessions", json={"start_time": start_time})
|
||||
response = await ac.post("/api/sessions", json={"start_time": start_time})
|
||||
assert response.status_code == 409
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_end_session(api_client):
|
||||
async with api_client as ac:
|
||||
start_time = datetime.now(timezone.utc).isoformat()
|
||||
await ac.post("/api/sessions", json={"start_time": start_time})
|
||||
|
||||
end_time = datetime.now(timezone.utc).isoformat()
|
||||
response = await ac.patch("/api/sessions/1/end", json={"end_time": end_time})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert datetime.fromisoformat(response.json()["end_time"]).replace(
|
||||
tzinfo=None
|
||||
) == datetime.fromisoformat(end_time).replace(tzinfo=None)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_sessions(api_client):
|
||||
async with api_client as ac:
|
||||
start_time = datetime.now(timezone.utc).isoformat()
|
||||
await ac.post("/api/sessions", json={"start_time": start_time})
|
||||
response = await ac.get("/api/sessions")
|
||||
assert response.status_code == 200
|
||||
assert len(response.json()["sessions"]) >= 1
|
||||
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from app.main import app
|
||||
from app.database import engine, init_db
|
||||
|
||||
Reference in New Issue
Block a user