caudal/programa/pruebas/test_deshacer_borrado.py
esetheyers 7e18576f86 Caudal 0.2.0
Taxes for the whole of Spain (common territory, Navarre, Álava, Bizkaia and Gipuzkoa); flats, garages and land valued on their own; Treasury bills, bonds and deposits priced on BME; broker accounts; a guided start; a page per investment with its chart and the tax of selling today; net worth at the top; undo when deleting; Woodpecker CI for the tests and the Linux build.
2026-10-15 16:49:15 -05:00

54 lines
2 KiB
Python

"""
Pruebas de deshacer un borrado de operaciones (Cartera.restaurar_movimientos).
python3 -m unittest pruebas.test_deshacer_borrado -v
"""
from __future__ import annotations
import sys
import tempfile
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from cartera.datos import Datos, escribir, leer # noqa: E402
from cartera.operaciones import Cartera, ErrorOperacion # noqa: E402
class TestDeshacerBorrado(unittest.TestCase):
def setUp(self):
t = tempfile.TemporaryDirectory()
self.addCleanup(t.cleanup)
self.carpeta = Path(t.name) / "datos"
escribir(self.carpeta, Datos([], [], [], []))
self.c = Cartera(self.carpeta)
self.addCleanup(self.c.cerrar)
a = self.c.guardar_activo({"nombre": "Fondo", "tipo": "Fondo"})
self.ids = [self.c.guardar_movimiento({"activo": a["id"], "tipo": "Compra", "fecha": f"2025-0{i}-10", "importe": 100,
"cantidad": 1})["id"] for i in (1, 2, 3)]
def vivas(self):
return sorted(m["id"] for m in leer(self.carpeta).vivos("movimientos"))
def test_una_borrada_vuelve_igual(self):
r = self.c.borrar_movimiento(self.ids[0])
self.assertEqual(r["ids"], [self.ids[0]])
self.assertNotIn(self.ids[0], self.vivas())
self.c.restaurar_movimientos(r["ids"])
self.assertEqual(self.vivas(), sorted(self.ids))
def test_varias_de_golpe_y_lo_que_no_esta_borrado_no_se_toca(self):
r = self.c.borrar_movimientos(self.ids[:2])
self.assertEqual(sorted(r["ids"]), sorted(self.ids[:2]))
with self.assertRaises(ErrorOperacion):
self.c.restaurar_movimientos([self.ids[2]]) # esta no está borrada
with self.assertRaises(ErrorOperacion):
self.c.restaurar_movimientos([])
self.c.restaurar_movimientos(r["ids"])
self.assertEqual(self.vivas(), sorted(self.ids))
if __name__ == "__main__":
unittest.main()