03-oct: superguardado automático
This commit is contained in:
73
tests/test_archivo.py
Normal file
73
tests/test_archivo.py
Normal file
@@ -0,0 +1,73 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Tests de `monitor_estado.archivo` — ficheros del dia, reescritura y poda (RF-5,7,9,10,18)."""
|
||||
|
||||
from datetime import date, timedelta
|
||||
|
||||
import monitor_estado.archivo as archivo
|
||||
|
||||
|
||||
def _render(m):
|
||||
return f"- **{m['id']}**: {m['t']}"
|
||||
|
||||
|
||||
def test_archivo_reescribe_sin_duplicados(tmp_path, monkeypatch):
|
||||
"""El fichero del dia contiene todo lo de hoy y nada duplicado (RF-9)."""
|
||||
monkeypatch.setattr(archivo, "MONITOR_DIR", tmp_path)
|
||||
hoy = date(2026, 10, 3)
|
||||
|
||||
p = archivo.escribe_dia(
|
||||
"telegram/test", hoy,
|
||||
[{"id": 1, "t": "a"}, {"id": 2, "t": "b"}, {"id": 2, "t": "b"}],
|
||||
id_fn=lambda m: m["id"], render_fn=_render,
|
||||
)
|
||||
contenido = p.read_text(encoding="utf-8")
|
||||
assert contenido.count("- **1**") == 1
|
||||
assert contenido.count("- **2**") == 1
|
||||
|
||||
# Nuevo ciclo con lo del dia completo: el fichero se reescribe con el estado actual.
|
||||
archivo.escribe_dia(
|
||||
"telegram/test", hoy,
|
||||
[{"id": 1, "t": "a"}, {"id": 2, "t": "b"}, {"id": 3, "t": "c"}],
|
||||
id_fn=lambda m: m["id"], render_fn=_render,
|
||||
)
|
||||
contenido = p.read_text(encoding="utf-8")
|
||||
assert "- **3**" in contenido
|
||||
assert contenido.count("- **1**") == 1
|
||||
|
||||
|
||||
def test_archivo_poda_30_dias_y_conserva_recientes(tmp_path, monkeypatch):
|
||||
"""Se borran los ficheros de mas de 30 dias y se conservan los recientes (RF-10)."""
|
||||
monkeypatch.setattr(archivo, "MONITOR_DIR", tmp_path)
|
||||
hoy = date(2026, 10, 3)
|
||||
reciente = tmp_path / "telegram" / "test"
|
||||
reciente.mkdir(parents=True)
|
||||
f_hoy = reciente / f"{hoy.isoformat()}.md"
|
||||
f_10 = reciente / f"{(hoy - timedelta(days=10)).isoformat()}.md"
|
||||
f_40 = reciente / f"{(hoy - timedelta(days=40)).isoformat()}.md"
|
||||
for f in (f_hoy, f_10, f_40):
|
||||
f.write_text("x", encoding="utf-8")
|
||||
|
||||
borrados = archivo.poda(retencion_dias=30, hoy=hoy)
|
||||
|
||||
assert f_hoy.exists() and f_10.exists()
|
||||
assert not f_40.exists()
|
||||
assert f_40 in borrados
|
||||
|
||||
|
||||
def test_escribe_resumen(tmp_path, monkeypatch):
|
||||
"""El resumen se guarda en md y json consultables (RF-18)."""
|
||||
monkeypatch.setattr(archivo, "MONITOR_DIR", tmp_path)
|
||||
resumen = {"resumen_md": "Todo ok", "requiere_atencion": [], "errores": []}
|
||||
archivo.escribe_resumen(resumen)
|
||||
|
||||
assert (tmp_path / "resumen_actual.md").read_text(encoding="utf-8") == "Todo ok"
|
||||
assert '"resumen_md": "Todo ok"' in (tmp_path / "resumen_actual.json").read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def test_escribe_dia_de_fuente_sin_novedad(tmp_path, monkeypatch):
|
||||
"""Una fuente sin elementos genera un fichero vacio (no falla) (RF-5)."""
|
||||
monkeypatch.setattr(archivo, "MONITOR_DIR", tmp_path)
|
||||
p = archivo.escribe_dia(
|
||||
"correo", date(2026, 10, 3), [], id_fn=lambda m: m, render_fn=lambda m: str(m)
|
||||
)
|
||||
assert p.exists()
|
||||
Reference in New Issue
Block a user