77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
"""Lettura di config.toml e del CSV: errori attesi."""
|
|
|
|
import pytest
|
|
from conftest import RADICE
|
|
|
|
from fitseg import config, dati
|
|
|
|
|
|
def test_config_reale_valida():
|
|
assert config.leggi(RADICE / "config.toml")["dati"]["serie"] == ["T_C", "dT_C"]
|
|
|
|
|
|
def test_chiave_mancante(tmp_path):
|
|
(tmp_path / "c.toml").write_text("[dati]\ncolonna_tempo='t'\n")
|
|
with pytest.raises(config.ErroreConfig, match="chiavi mancanti"):
|
|
config.leggi(tmp_path / "c.toml")
|
|
|
|
|
|
def test_modelli_per_tipo_incompleto(tmp_path):
|
|
testo = (RADICE / "config.toml").read_text().replace('raffreddamento = ["esp_decad"]', "raffreddamento = []")
|
|
(tmp_path / "c.toml").write_text(testo)
|
|
with pytest.raises(config.ErroreConfig, match="raffreddamento"):
|
|
config.leggi(tmp_path / "c.toml")
|
|
|
|
|
|
def test_config_inesistente():
|
|
with pytest.raises(config.ErroreConfig, match="impossibile leggere"):
|
|
config.leggi("/non/esiste.toml")
|
|
|
|
|
|
def test_csv_valido(cfg, tmp_path):
|
|
(tmp_path / "d.csv").write_text("t_ms,T_C,dT_C\n100,20.0,0.1\n250,20.1,0.2\n")
|
|
t, serie = dati.leggi_csv(tmp_path / "d.csv", cfg)
|
|
assert t.tolist() == [0.1, 0.25] and serie["T_C"].tolist() == [20.0, 20.1]
|
|
|
|
|
|
def test_csv_colonna_mancante(cfg, tmp_path):
|
|
(tmp_path / "d.csv").write_text("t_ms,T_C\n100,20.0\n")
|
|
with pytest.raises(dati.ErroreDati, match="dT_C"):
|
|
dati.leggi_csv(tmp_path / "d.csv", cfg)
|
|
|
|
|
|
def test_csv_tempo_non_crescente(cfg, tmp_path):
|
|
(tmp_path / "d.csv").write_text("t_ms,T_C,dT_C\n200,20,0\n100,20,0\n")
|
|
with pytest.raises(dati.ErroreDati, match="crescente"):
|
|
dati.leggi_csv(tmp_path / "d.csv", cfg)
|
|
|
|
|
|
def test_toml_non_valido(tmp_path):
|
|
(tmp_path / "c.toml").write_text("[dati\nrotto")
|
|
with pytest.raises(config.ErroreConfig, match="TOML non valido"):
|
|
config.leggi(tmp_path / "c.toml")
|
|
|
|
|
|
@pytest.mark.parametrize(("vecchio", "nuovo", "messaggio"), [
|
|
('unita_tempo = "ms"', 'unita_tempo = "min"', "unita_tempo"),
|
|
('serie_riferimento = "T_C"', 'serie_riferimento = "X"', "serie_riferimento"),
|
|
])
|
|
def test_valori_non_ammessi(tmp_path, vecchio, nuovo, messaggio):
|
|
testo = (RADICE / "config.toml").read_text()
|
|
assert vecchio in testo
|
|
(tmp_path / "c.toml").write_text(testo.replace(vecchio, nuovo, 1))
|
|
with pytest.raises(config.ErroreConfig, match=messaggio):
|
|
config.leggi(tmp_path / "c.toml")
|
|
|
|
|
|
def test_csv_senza_righe_di_dati(cfg, tmp_path):
|
|
(tmp_path / "d.csv").write_text("t_ms,T_C,dT_C\n")
|
|
with pytest.raises(dati.ErroreDati, match="vuoti o non numerici"):
|
|
dati.leggi_csv(tmp_path / "d.csv", cfg)
|
|
|
|
|
|
def test_csv_con_valori_non_numerici(cfg, tmp_path):
|
|
(tmp_path / "d.csv").write_text("t_ms,T_C,dT_C\n100,abc,0.1\n250,20.1,0.2\n")
|
|
with pytest.raises(dati.ErroreDati, match="vuoti o non numerici"):
|
|
dati.leggi_csv(tmp_path / "d.csv", cfg)
|