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.
59 lines
2.4 KiB
Python
59 lines
2.4 KiB
Python
"""
|
|
Pruebas de lo que la ficha de una inversión pide al servidor: su evolución y
|
|
«si la vendieras hoy» (servidor.App.si_vendo), con la cartera de ejemplo.
|
|
|
|
python3 -m unittest pruebas.test_ficha -v
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import datetime as dt
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from cartera import servidor # noqa: E402
|
|
from cartera.datos import Datos, escribir # noqa: E402
|
|
from cartera.operaciones import Cartera # noqa: E402
|
|
|
|
|
|
class TestFicha(unittest.TestCase):
|
|
def setUp(self):
|
|
t = tempfile.TemporaryDirectory()
|
|
self.addCleanup(t.cleanup)
|
|
carpeta = Path(t.name) / "datos"
|
|
escribir(carpeta, Datos([], [], [], []))
|
|
self.app = servidor.App(Cartera(carpeta))
|
|
self.app.ejemplo(True)
|
|
self.addCleanup(self.app.ejemplo, False)
|
|
self.ids = {a["nombre"]: a["id"] for a in self.app.cartera.datos.vivos("activos")}
|
|
|
|
def test_la_evolucion_de_una_inversion_acaba_hoy_en_su_valor(self):
|
|
ev = servidor.evolucion_activo(self.app.cartera.datos, self.ids["Bitcoin"], dt.date.today())
|
|
fila = next(f for f in self.app.estado()["resumen"]["abiertos"] if f["nombre"] == "Bitcoin")
|
|
self.assertGreater(len(ev), 12)
|
|
self.assertEqual(ev[-1]["fecha"], dt.date.today().isoformat())
|
|
self.assertAlmostEqual(ev[-1]["valor"], fila["valor"], places=2)
|
|
self.assertEqual(servidor.evolucion_activo(self.app.cartera.datos, "NO", dt.date.today()), [])
|
|
|
|
def test_si_vendo_hoy_la_ganancia_y_el_impuesto_al_tipo_del_ahorro(self):
|
|
v = self.app.si_vendo(self.ids["Bitcoin"])
|
|
self.assertTrue(v["posible"])
|
|
self.assertAlmostEqual(v["ganancia"], 3423.96, places=1)
|
|
self.assertAlmostEqual(v["impuesto"], round(v["ganancia"] * 0.19, 2), delta=0.05) # primer tramo del común
|
|
|
|
def test_en_un_piso_alquilado_hacienda_suma_lo_amortizado(self):
|
|
v = self.app.si_vendo(self.ids["Piso alquilado"])
|
|
fila = next(f for f in self.app.estado()["resumen"]["abiertos"] if f["nombre"] == "Piso alquilado")
|
|
self.assertGreater(v["ganancia"], fila["valor"] - fila["compra"])
|
|
|
|
def test_lo_que_no_se_vende_no_tiene_si_vendo(self):
|
|
for nombre in ("Cuenta bancaria", "Hipoteca del piso"):
|
|
self.assertEqual(self.app.si_vendo(self.ids[nombre]), {"posible": False})
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|