Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XGaEeUbVSdKNfpFRgWfCXa
153 lines
8.1 KiB
Python
153 lines
8.1 KiB
Python
"""Onglet « À suivre » : marqueur manuel, sans limite de date, réversible, et couleur propre à chaque catégorie."""
|
|
import sqlite3
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.db import init_db, add_category, list_categories, update_category, delete_category
|
|
from app.main import app
|
|
|
|
M1 = {"message_id": "m-1", "expediteur": "Ali <ali@exemple.tn>", "objet": "Courses Tunis", "date_mail": "2026-10-02T09:00:00+01:00", "categorie": "Tunis"}
|
|
M2 = {"message_id": "m-2", "expediteur": "Sana <sana@exemple.tn>", "objet": "Marché RLA", "date_mail": "2026-11-18T14:30:00+01:00", "categorie": "RLA"}
|
|
M3 = {"message_id": "m-3", "expediteur": "Omar <omar@exemple.tn>", "objet": "Câbles", "date_mail": "2026-11-10T08:00:00+01:00", "categorie": "Câbles"}
|
|
|
|
|
|
def test_exige_le_jeton():
|
|
c = TestClient(app)
|
|
assert c.get("/api/v1/a-suivre").status_code == 401
|
|
assert c.post("/api/v1/a-suivre/marquer", json=M1).status_code == 401
|
|
assert c.post("/api/v1/a-suivre/retirer", json={"ids": ["m-1"]}).status_code == 401
|
|
|
|
|
|
def test_marquer_garde_la_copie_et_ne_depend_pas_de_la_date(client):
|
|
r = client.post("/api/v1/a-suivre/marquer", json=M1)
|
|
assert r.status_code == 200
|
|
d = r.json()
|
|
assert d["statut"] == "actif" and d["objet"] == "Courses Tunis" and d["categorie"] == "Tunis" and d["jours"] == 0
|
|
liste = client.get("/api/v1/a-suivre?statut=actif").json()
|
|
assert [i["message_id"] for i in liste["items"]] == ["m-1"] # mail du 02/10 toujours là
|
|
assert liste["compte"] == {"actif": 1, "termine": 0}
|
|
|
|
|
|
def test_double_marquage_garde_la_date_de_marquage(client):
|
|
a = client.post("/api/v1/a-suivre/marquer", json=M1).json()
|
|
b = client.post("/api/v1/a-suivre/marquer", json=M1).json()
|
|
assert a["marque_le"] == b["marque_le"]
|
|
assert len(client.get("/api/v1/a-suivre").json()["items"]) == 1
|
|
|
|
|
|
def test_les_plus_recents_en_premier(client):
|
|
for m in (M1, M2, M3):
|
|
client.post("/api/v1/a-suivre/marquer", json=m)
|
|
ordre = [i["message_id"] for i in client.get("/api/v1/a-suivre?statut=actif").json()["items"]]
|
|
assert ordre == ["m-2", "m-3", "m-1"]
|
|
|
|
|
|
def test_retirer_ne_supprime_rien_et_se_defait(client):
|
|
for m in (M1, M2):
|
|
client.post("/api/v1/a-suivre/marquer", json=m)
|
|
r = client.post("/api/v1/a-suivre/retirer", json={"ids": ["m-1", "m-1", "inconnu"]}).json()
|
|
assert r["ids"] == ["m-1"]
|
|
assert client.get("/api/v1/a-suivre?statut=actif").json()["compte"] == {"actif": 1, "termine": 1}
|
|
term = client.get("/api/v1/a-suivre?statut=termine").json()["items"]
|
|
assert [i["message_id"] for i in term] == ["m-1"] and term[0]["termine_le"]
|
|
avant = term[0]["marque_le"]
|
|
r = client.post("/api/v1/a-suivre/remettre", json={"ids": ["m-1"]}).json()
|
|
assert r["ids"] == ["m-1"]
|
|
back = [i for i in client.get("/api/v1/a-suivre?statut=actif").json()["items"] if i["message_id"] == "m-1"][0]
|
|
assert back["marque_le"] == avant and back["termine_le"] is None
|
|
|
|
|
|
def test_remarquer_un_mail_retire_le_reactive(client):
|
|
client.post("/api/v1/a-suivre/marquer", json=M1)
|
|
client.post("/api/v1/a-suivre/retirer", json={"ids": ["m-1"]})
|
|
client.post("/api/v1/a-suivre/marquer", json=M1)
|
|
assert client.get("/api/v1/a-suivre").json()["compte"] == {"actif": 1, "termine": 0}
|
|
|
|
|
|
def test_retirer_toute_une_categorie_en_un_appel(client):
|
|
for i in range(5):
|
|
client.post("/api/v1/a-suivre/marquer", json={**M1, "message_id": f"t-{i}", "date_mail": f"2026-11-0{i+1}T09:00:00+01:00"})
|
|
client.post("/api/v1/a-suivre/marquer", json=M2)
|
|
ids = [i["message_id"] for i in client.get("/api/v1/a-suivre?statut=actif").json()["items"] if i["categorie"] == "Tunis"]
|
|
assert len(ids) == 5
|
|
assert len(client.post("/api/v1/a-suivre/retirer", json={"ids": ids}).json()["ids"]) == 5
|
|
assert client.get("/api/v1/a-suivre").json()["compte"] == {"actif": 1, "termine": 5}
|
|
|
|
|
|
def test_la_categorie_affichee_est_la_categorie_actuelle(client):
|
|
client.post("/api/v1/a-suivre/marquer", json=M1)
|
|
client.post("/api/v1/classements/m-1", json={"categorie": "CI", "mail_info": {"message_id": "m-1", "expediteur": "Ali", "objet": "Courses Tunis", "date": "2026-10-02T09:00:00+01:00"}})
|
|
assert client.get("/api/v1/a-suivre").json()["items"][0]["categorie"] == "CI"
|
|
|
|
|
|
def test_categorie_inconnue_devient_a_classer(client):
|
|
d = client.post("/api/v1/a-suivre/marquer", json={"message_id": "x-1", "objet": "Sans info"}).json()
|
|
assert d["categorie"] == "À classer"
|
|
|
|
|
|
def test_renommer_une_categorie_met_a_jour_les_copies(client, temp_db):
|
|
cat = add_category("Projet X", temp_db)
|
|
client.post("/api/v1/a-suivre/marquer", json={**M1, "categorie": "Projet X", "message_id": "p-1"})
|
|
update_category(cat["id"], "Projet Y", temp_db)
|
|
assert client.get("/api/v1/a-suivre").json()["items"][0]["categorie"] == "Projet Y"
|
|
delete_category(cat["id"], temp_db)
|
|
assert client.get("/api/v1/a-suivre").json()["items"][0]["categorie"] == "À classer"
|
|
|
|
|
|
def test_validation(client):
|
|
assert client.post("/api/v1/a-suivre/marquer", json={}).status_code == 422
|
|
assert client.post("/api/v1/a-suivre/retirer", json={"ids": []}).status_code == 422
|
|
assert client.post("/api/v1/a-suivre/retirer", json={"ids": [""]}).status_code == 422
|
|
assert client.get("/api/v1/a-suivre?statut=nimporte").status_code == 422
|
|
|
|
|
|
def test_chaque_categorie_a_sa_propre_couleur(temp_db):
|
|
for n in ("RLA", "Box 5G", "Pb Stock", "Pb PO", "Drapeau", "Projet X", "Projet Y"):
|
|
add_category(n, temp_db)
|
|
couleurs = {c["name"]: c["couleur"] for c in list_categories(temp_db)}
|
|
assert couleurs["À classer"] == 0 and couleurs["Tunis"] == 1 and couleurs["Sans objet"] == 5
|
|
perso = [couleurs[n] for n in ("RLA", "Box 5G", "Pb Stock", "Pb PO", "Drapeau", "Projet X", "Projet Y")]
|
|
assert len(set(perso)) == 7 and all(6 <= c <= 12 for c in perso)
|
|
# une couleur ne change jamais quand on renomme
|
|
cat = [c for c in list_categories(temp_db) if c["name"] == "RLA"][0]
|
|
update_category(cat["id"], "RLA 2026", temp_db)
|
|
assert [c for c in list_categories(temp_db) if c["name"] == "RLA 2026"][0]["couleur"] == cat["couleur"]
|
|
|
|
|
|
def test_migration_d_une_base_existante_sans_couleur(tmp_path):
|
|
chemin = str(tmp_path / "ancienne.db")
|
|
c = sqlite3.connect(chemin)
|
|
c.execute("CREATE TABLE categories (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, created_at TEXT NOT NULL)")
|
|
for i, n in ((1, "Tunis"), (6, "À classer"), (73, "Box 5G"), (74, "Pb Stock"), (99, "RLA"), (142, "Pb PO"), (143, "Drapeau")):
|
|
c.execute("INSERT INTO categories (id, name, created_at) VALUES (?, ?, 'x')", (i, n))
|
|
c.commit(); c.close()
|
|
init_db(chemin)
|
|
couleurs = {x["name"]: x["couleur"] for x in list_categories(chemin)}
|
|
assert couleurs["RLA"] == 6 and couleurs["Box 5G"] == 7 and couleurs["Pb Stock"] == 8 # teintes habituelles conservées
|
|
perso = [couleurs[n] for n in ("RLA", "Box 5G", "Pb Stock", "Pb PO", "Drapeau")]
|
|
assert len(set(perso)) == 5 # plus aucune collision
|
|
init_db(chemin) # idempotent
|
|
assert {x["name"]: x["couleur"] for x in list_categories(chemin)} == couleurs
|
|
|
|
def test_supprimer_termine_seulement(client):
|
|
for i in ("m1", "m2", "m3"):
|
|
client.post("/api/v1/a-suivre/marquer", json={"message_id": i, "objet": i})
|
|
client.post("/api/v1/a-suivre/retirer", json={"ids": ["m1", "m2"]})
|
|
r = client.post("/api/v1/a-suivre/supprimer", json={"ids": ["m1", "m3"]})
|
|
assert r.status_code == 200
|
|
assert r.json()["ids"] == ["m1"] # m3 est actif : jamais supprimé
|
|
tout = client.get("/api/v1/a-suivre?statut=tous").json()
|
|
assert sorted(i["message_id"] for i in tout["items"]) == ["m2", "m3"]
|
|
assert tout["compte"] == {"actif": 1, "termine": 1}
|
|
|
|
|
|
def test_supprimer_puis_remarquer(client):
|
|
client.post("/api/v1/a-suivre/marquer", json={"message_id": "x", "objet": "x"})
|
|
client.post("/api/v1/a-suivre/retirer", json={"ids": ["x"]})
|
|
client.post("/api/v1/a-suivre/supprimer", json={"ids": ["x"]})
|
|
assert client.post("/api/v1/a-suivre/marquer", json={"message_id": "x", "objet": "x"}).json()["statut"] == "actif"
|
|
|
|
|
|
def test_supprimer_validation(client):
|
|
assert client.post("/api/v1/a-suivre/supprimer", json={"ids": []}).status_code == 422
|