Test scalda camere. Refs #108
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -271,3 +271,77 @@ def test_avvio_normale_registra_gli_strumenti_prima_del_server(avvio, tmp_path,
|
|||||||
assert avvio.main(["--senza-finestre", "--senza-browser"]) == 0
|
assert avvio.main(["--senza-finestre", "--senza-browser"]) == 0
|
||||||
dati = tmp_path / "Tielogic" / "TieMeasureFlow"
|
dati = tmp_path / "Tielogic" / "TieMeasureFlow"
|
||||||
assert avvio.chiamate == ["migrazioni", "strumenti", ("127.0.0.1", dati)]
|
assert avvio.chiamate == ["migrazioni", "strumenti", ("127.0.0.1", dati)]
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- scalda camere (#108)
|
||||||
|
|
||||||
|
def _db_camere(percorso):
|
||||||
|
# #108: db minimo con le due tabelle lette da camere_da_scaldare
|
||||||
|
con = sqlite3.connect(percorso)
|
||||||
|
con.execute("CREATE TABLE stations (id INTEGER PRIMARY KEY, camera_url TEXT, camera_source TEXT)")
|
||||||
|
con.execute("CREATE TABLE recipe_tasks (id INTEGER PRIMARY KEY, acquisition_json TEXT)")
|
||||||
|
con.executemany("INSERT INTO stations (camera_url, camera_source) VALUES (?, ?)", [
|
||||||
|
("http://127.0.0.1:8765", "camera:601020"), # valida
|
||||||
|
(None, None), # stazione senza camera
|
||||||
|
("http://127.0.0.1:8765", "camera:folder"), # non apre hardware
|
||||||
|
])
|
||||||
|
con.executemany("INSERT INTO recipe_tasks (acquisition_json) VALUES (?)", [
|
||||||
|
(json.dumps({"camera_url": "http://127.0.0.1:8765", "camera_source": "camera:601020"}),), # duplicato
|
||||||
|
(json.dumps({"camera_url": "http://10.0.0.5:8765/", "camera_source": "camera:777"}),), # valida
|
||||||
|
("{rotto",), # JSON non valido
|
||||||
|
(None,),
|
||||||
|
])
|
||||||
|
con.commit()
|
||||||
|
con.close()
|
||||||
|
|
||||||
|
|
||||||
|
def test_camere_da_scaldare_filtra_e_deduplica(avvio, tmp_path):
|
||||||
|
db = tmp_path / "tmf.db"
|
||||||
|
_db_camere(db)
|
||||||
|
assert avvio.camere_da_scaldare(db) == [
|
||||||
|
("http://127.0.0.1:8765", "camera:601020"),
|
||||||
|
("http://10.0.0.5:8765/", "camera:777"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_camere_da_scaldare_db_inesistente(avvio, tmp_path):
|
||||||
|
assert avvio.camere_da_scaldare(tmp_path / "non_esiste.db") == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_scalda_camere_una_chiamata_per_coppia(avvio, tmp_path, monkeypatch):
|
||||||
|
db = tmp_path / "tmf.db"
|
||||||
|
_db_camere(db)
|
||||||
|
monkeypatch.setattr(avvio, "_porta_aperta", lambda porta: True)
|
||||||
|
chiamate = []
|
||||||
|
|
||||||
|
class Risposta:
|
||||||
|
status = 200
|
||||||
|
def __enter__(self): return self
|
||||||
|
def __exit__(self, *a): return False
|
||||||
|
|
||||||
|
def finto_urlopen(richiesta, timeout=None):
|
||||||
|
chiamate.append((richiesta.full_url, json.loads(richiesta.data), richiesta.get_method(), timeout))
|
||||||
|
return Risposta()
|
||||||
|
|
||||||
|
monkeypatch.setattr(avvio.urllib.request, "urlopen", finto_urlopen)
|
||||||
|
avvio.scalda_camere(db)
|
||||||
|
corpo = {"trigger": "continuo", "timeout_ms": 5000}
|
||||||
|
assert chiamate == [
|
||||||
|
("http://127.0.0.1:8765/camera/grab", {"source": "camera:601020", **corpo}, "POST", 90),
|
||||||
|
("http://10.0.0.5:8765/camera/grab", {"source": "camera:777", **corpo}, "POST", 90),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_scalda_camere_eccezione_urlopen_non_esce(avvio, tmp_path, monkeypatch):
|
||||||
|
db = tmp_path / "tmf.db"
|
||||||
|
_db_camere(db)
|
||||||
|
monkeypatch.setattr(avvio, "_porta_aperta", lambda porta: True)
|
||||||
|
tentativi = []
|
||||||
|
|
||||||
|
def urlopen_rotto(richiesta, timeout=None):
|
||||||
|
tentativi.append(richiesta.full_url)
|
||||||
|
raise OSError("camera irraggiungibile")
|
||||||
|
|
||||||
|
monkeypatch.setattr(avvio.urllib.request, "urlopen", urlopen_rotto)
|
||||||
|
avvio.scalda_camere(db) # non solleva e passa alla camera successiva
|
||||||
|
assert len(tentativi) == 2
|
||||||
|
|||||||
Reference in New Issue
Block a user