import unittest

from ledger import Ledger


class LedgerTests(unittest.TestCase):
    def test_success_is_idempotent(self):
        ledger = Ledger()
        self.assertEqual(ledger.apply("deposit-1", "a", 10), 10)
        self.assertEqual(ledger.apply("deposit-1", "a", 10), 10)

    def test_overdraft_does_not_change_balance(self):
        ledger = Ledger()
        with self.assertRaises(ValueError):
            ledger.apply("withdraw-1", "a", -4)
        self.assertEqual(ledger.balance("a"), 0)

    def test_rejected_operation_can_be_retried(self):
        ledger = Ledger()
        with self.assertRaises(ValueError):
            ledger.apply("withdraw-1", "a", -4)
        ledger.apply("deposit-1", "a", 10)
        self.assertEqual(ledger.apply("withdraw-1", "a", -4), 6)


if __name__ == "__main__":
    unittest.main()
