cuscino_watch: riconverti distingue parziale/fallita rileggendo la quota (#35 B10)
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -155,7 +155,15 @@ def test_un_tentativo_ogni_sei_ore():
|
||||
assert W.puo_riconvertire([{"ts": now - 1000, "riconversione": None}], now, True, 1.0, 0.99)[0]
|
||||
|
||||
|
||||
def _finto_lettore_quota(valori):
|
||||
"""Ritorna i valori dati in ordine, uno per chiamata (per monkeypatch di _leggi_quota_usde)."""
|
||||
it = iter(valori)
|
||||
return lambda: next(it)
|
||||
|
||||
|
||||
def test_riconverti_lancia_lo_script_vero_con_esegui(monkeypatch):
|
||||
# rc==0: "ok" a prescindere dalla quota (lo script vero ha gia' le sue guardie, P15) — la
|
||||
# quota qui non si muove, per mostrare che il caso "ok" non dipende dalla rilettura.
|
||||
chiamate = []
|
||||
|
||||
class R:
|
||||
@@ -166,10 +174,61 @@ def test_riconverti_lancia_lo_script_vero_con_esegui(monkeypatch):
|
||||
return R()
|
||||
|
||||
monkeypatch.setattr(W.subprocess, "run", finto_run)
|
||||
monkeypatch.setattr(W, "_leggi_quota_usde", _finto_lettore_quota([0.67, 0.67]))
|
||||
e = W.riconverti(0.67)
|
||||
assert e["ok"] and chiamate and chiamate[0][1].endswith("usde_convert.py")
|
||||
assert e["ok"] and e["esito"] == "ok" and chiamate and chiamate[0][1].endswith("usde_convert.py")
|
||||
assert "--esegui" in chiamate[0] and "0.6700" in chiamate[0]
|
||||
assert e["coda"][-1].startswith(" ESITO")
|
||||
assert e["quota_prima"] == 0.67 and e["quota_dopo"] == 0.67
|
||||
|
||||
|
||||
def test_riconverti_rc_diverso_da_zero_con_quota_mossa_e_parziale(monkeypatch):
|
||||
# #35 B10: rc!=0 (es. ordine parzialmente fillato poi rifiutato) ma la quota si e' mossa
|
||||
# per davvero -> "parziale", non "fallita": la vendita/acquisto e' avvenuto, solo non finito.
|
||||
class R:
|
||||
returncode, stdout, stderr = 1, "", "book illeggibile a meta' piano\n"
|
||||
|
||||
monkeypatch.setattr(W.subprocess, "run", lambda cmd, **kw: R())
|
||||
monkeypatch.setattr(W, "_leggi_quota_usde", _finto_lettore_quota([0.75, 0.70]))
|
||||
e = W.riconverti(0.64)
|
||||
assert e["esito"] == "parziale" and e["ok"] is False
|
||||
assert e["quota_prima"] == 0.75 and e["quota_dopo"] == 0.70
|
||||
|
||||
|
||||
def test_riconverti_rc_diverso_da_zero_con_quota_ferma_e_fallita(monkeypatch):
|
||||
class R:
|
||||
returncode, stdout, stderr = 1, "", "prezzo fuori banda: niente ordini\n"
|
||||
|
||||
monkeypatch.setattr(W.subprocess, "run", lambda cmd, **kw: R())
|
||||
monkeypatch.setattr(W, "_leggi_quota_usde", _finto_lettore_quota([0.75, 0.751]))
|
||||
e = W.riconverti(0.64)
|
||||
assert e["esito"] == "fallita" and e["ok"] is False
|
||||
|
||||
|
||||
def test_riconverti_timeout_con_quota_mossa_e_parziale(monkeypatch):
|
||||
# #35 B10: il subprocess viene ucciso a TIMEOUT_CONVERT_S ma alcuni ordini erano gia' fillati
|
||||
# prima del kill -> oggi (ok=rc==0) diventava "FALLITA", corretto e' "parziale".
|
||||
def finto_run(cmd, **kw):
|
||||
raise W.subprocess.TimeoutExpired(cmd, kw.get("timeout"))
|
||||
|
||||
monkeypatch.setattr(W.subprocess, "run", finto_run)
|
||||
monkeypatch.setattr(W, "_leggi_quota_usde", _finto_lettore_quota([0.80, 0.72]))
|
||||
e = W.riconverti(0.64)
|
||||
assert e["rc"] is None and e["esito"] == "parziale" and e["ok"] is False
|
||||
assert e["coda"][0].startswith("timeout dopo")
|
||||
|
||||
|
||||
def test_riconverti_rilettura_dopo_fallita_e_fallita_esplicita(monkeypatch):
|
||||
# se non si riesce a rileggere la quota dopo, non si puo' dire "parziale": si dichiara
|
||||
# l'incertezza invece di dedurre un movimento zero che non si e' verificato.
|
||||
class R:
|
||||
returncode, stdout, stderr = 1, "", "errore\n"
|
||||
|
||||
monkeypatch.setattr(W.subprocess, "run", lambda cmd, **kw: R())
|
||||
monkeypatch.setattr(W, "_leggi_quota_usde", _finto_lettore_quota([0.75, None]))
|
||||
e = W.riconverti(0.64)
|
||||
assert e["esito"] == "fallita (quota dopo non leggibile)" and e["ok"] is False
|
||||
assert e["quota_dopo"] is None
|
||||
|
||||
|
||||
def test_il_monitor_e_registrato_in_monitor_health():
|
||||
|
||||
Reference in New Issue
Block a user