# Pruebas de unidad - Cuenta bancaria (c) 2024 Baltasar MIT License <baltasarq@gmail.com>
import unittest
class CuentaBancaria:
def __init__(self):
self.__saldo = 0
@property
def saldo(self):
return self.__saldo
def ingresa(self, x):
self.__saldo += x
def retira(self, x):
if ( x > self.saldo):
raise ValueError("retirada superior al saldo")
self.__saldo -= x
def __str__(self):
return str(self.__saldo)
class TestCuentaBancaria(unittest.TestCase):
def setUp(self):
self._cuenta = CuentaBancaria()
def test_ingresos(self):
ingresos = [20, 30, 400, 1200, 45]
total_ingresos = sum(ingresos)
for ingreso in ingresos:
saldo_actual = self._cuenta.saldo
self._cuenta.ingresa( ingreso )
self.assertEqual( saldo_actual + ingreso, self._cuenta.saldo)
self.assertEqual(total_ingresos, self._cuenta.saldo)
def test_retiradas(self):
ingreso_inicial = 10000
retiradas = [100, 10, 15, 25, 800, 300, 250]
total_retiradas = sum(retiradas)
self._cuenta.ingresa(ingreso_inicial)
for retirada in retiradas:
saldo_actual = self._cuenta.saldo
self._cuenta.retira(retirada)
self.assertEqual(saldo_actual - retirada, self._cuenta.saldo)
self.assertEqual(ingreso_inicial - total_retiradas, self._cuenta.saldo)
def test_saldo_negativo(self):
self._cuenta.ingresa(1000)
with self.assertRaises(ValueError):
self._cuenta.retira(1500)
if __name__ == "__main__":
unittest.main()
IyBQcnVlYmFzIGRlIHVuaWRhZCAtIEN1ZW50YSBiYW5jYXJpYSAoYykgMjAyNCBCYWx0YXNhciBNSVQgTGljZW5zZSA8YmFsdGFzYXJxQGdtYWlsLmNvbT4KCgppbXBvcnQgdW5pdHRlc3QKCgpjbGFzcyBDdWVudGFCYW5jYXJpYToKICAgIGRlZiBfX2luaXRfXyhzZWxmKToKICAgICAgICBzZWxmLl9fc2FsZG8gPSAwCiAgICAgICAgCiAgICBAcHJvcGVydHkKICAgIGRlZiBzYWxkbyhzZWxmKToKICAgICAgICByZXR1cm4gc2VsZi5fX3NhbGRvCiAgICAgICAgCiAgICBkZWYgaW5ncmVzYShzZWxmLC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