From dce90e6621446c20f2a6a8ccbb945694f09bcef0 Mon Sep 17 00:00:00 2001 From: Colin Goutte Date: Fri, 1 Oct 2021 15:58:51 +0200 Subject: [PATCH] Add testecase and rename --- papi/main.py | 33 +++++++++++++++++++++++++++++++++ tests/test_papi.py | 10 +++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/papi/main.py b/papi/main.py index 6ccdf87..3726154 100644 --- a/papi/main.py +++ b/papi/main.py @@ -195,3 +195,36 @@ def historique(idsonde, db: Session = Depends(get_db)): for d in diff["changements"]: line = date, *utils.clean_state(d) yield " ".join(line) + + +@app.get("/sonde/{idsonde}/historique/text") +def list_notification_as_text( + request: Request, idsonde: str, db: Session = Depends(get_db) +): + def coloriser(message): + d = {"error": "red", "warning": "orange", "pending": "yellow", "ok": "green"} + for k, v in d.items(): + if message.endswith(k): + return "color: %s;" % v + return "" + + if not (sonde := crud.get_sonde(db, idsonde)): + return + mesures = sonde.mesures + + for previous, present in zip(mesures, mesures[1:]): + date = present.content["date"] + previous = utils.prepare(previous.content) + present = utils.prepare(present.content) + all_channels = sorted(set((*previous["channels"], *present["channels"]))) + + diff = utils.compare(all_channels, previous, present) + res = [] + if diff: + for d in diff["changements"]: + line = date, *utils.clean_state(d) + res.append(" ".join(line)) + + return templates.TemplateResponse( + "historique.html", context={"request": request, "lines": res} + ) diff --git a/tests/test_papi.py b/tests/test_papi.py index 9cba1c8..4eb0f77 100644 --- a/tests/test_papi.py +++ b/tests/test_papi.py @@ -112,6 +112,14 @@ def test_historique(): assert perte_supervision[-1] == f"{data['date']} test_historique error absent" +def test_historique_rendering(): + idsonde = "masonde_001" + notifs_text = client.get(f"/sonde/{idsonde}/historique/text") + assert notifs_text.ok + content = notifs_text + assert f"absent" in content.text.lower() + + def test_onchange_notification(): id_sonde = "masonde_notif" histo = {"identifiant": id_sonde, "nom": "Test notification"} @@ -148,7 +156,7 @@ def test_onchange_notification(): assert len(notifs.json()) == 1 -def test_template_rendering(): +def test_notif_rendering(): idsonde = "masonde_001" notifs_text = client.get(f"/notifications/{idsonde}/text") assert notifs_text.ok