import pytest from sqlalchemy import delete from app.models.todos import TodoItem @pytest.fixture(autouse=True) async def cleanup(db_session): yield await db_session.execute(delete(TodoItem).where(TodoItem.title.like("TEST_%"))) await db_session.commit() async def test_creer_todo(client): resp = await client.post("/api/todos/", json={ "title": "TEST_tâche simple", "domains": ["informatique"], "priority": "high", }) assert resp.status_code == 201 data = resp.json() assert data["title"] == "TEST_tâche simple" assert data["status"] == "pending" assert data["postponed_count"] == 0 assert data["tags"] == [] assert data["domains"] == ["informatique"] async def test_lister_todos_filtre_status(client): await client.post("/api/todos/", json={"title": "TEST_en cours", "status": "pending"}) await client.post("/api/todos/", json={"title": "TEST_terminée", "status": "done"}) resp = await client.get("/api/todos/?status=pending") assert resp.status_code == 200 titres = [t["title"] for t in resp.json()] assert "TEST_en cours" in titres assert "TEST_terminée" not in titres async def test_lister_todos_filtre_domaine(client): await client.post("/api/todos/", json={"title": "TEST_info", "domains": ["informatique"]}) await client.post("/api/todos/", json={"title": "TEST_jardin", "domains": ["jardin"]}) resp = await client.get("/api/todos/?domain=informatique&status=") assert resp.status_code == 200 titres = [t["title"] for t in resp.json()] assert "TEST_info" in titres assert "TEST_jardin" not in titres async def test_mettre_a_jour_todo(client): create_resp = await client.post("/api/todos/", json={"title": "TEST_avant"}) item_id = create_resp.json()["id"] resp = await client.patch(f"/api/todos/{item_id}", json={"title": "TEST_après", "status": "done"}) assert resp.status_code == 200 assert resp.json()["title"] == "TEST_après" assert resp.json()["status"] == "done" assert resp.json()["updated_at"] is not None async def test_mettre_a_jour_todo_inexistant(client): resp = await client.patch( "/api/todos/00000000-0000-0000-0000-000000000000", json={"title": "TEST_ghost"}, ) assert resp.status_code == 404 async def test_supprimer_todo(client): create_resp = await client.post("/api/todos/", json={"title": "TEST_à supprimer"}) item_id = create_resp.json()["id"] resp = await client.delete(f"/api/todos/{item_id}") assert resp.status_code == 204 resp2 = await client.patch(f"/api/todos/{item_id}", json={"title": "TEST_fantôme"}) assert resp2.status_code == 404 async def test_reporter_todo_1_jour(client): due = "2026-06-01T10:00:00+00:00" create_resp = await client.post("/api/todos/", json={"title": "TEST_reporter", "due_date": due}) item_id = create_resp.json()["id"] resp = await client.post(f"/api/todos/{item_id}/postpone", json={"days": 1}) assert resp.status_code == 200 data = resp.json() assert data["postponed_count"] == 1 assert data["due_date"].startswith("2026-06-02") async def test_reporter_todo_1_semaine(client): due = "2026-06-01T10:00:00+00:00" create_resp = await client.post("/api/todos/", json={"title": "TEST_reporter7", "due_date": due}) item_id = create_resp.json()["id"] resp = await client.post(f"/api/todos/{item_id}/postpone", json={"days": 7}) assert resp.status_code == 200 data = resp.json() assert data["postponed_count"] == 1 assert data["due_date"].startswith("2026-06-08") async def test_reporter_jours_invalides(client): create_resp = await client.post("/api/todos/", json={"title": "TEST_invalide"}) item_id = create_resp.json()["id"] resp = await client.post(f"/api/todos/{item_id}/postpone", json={"days": 3}) assert resp.status_code == 422