import unittest from datetime import date from helpers import eur from ledger.money import Money from ledger.transactions import Posting, Transaction, UnbalancedTransaction class TransferTests(unittest.TestCase): def test_transfer_postings(self): tx = Transaction.transfer("t1", date(2024, 1, 5), "Pay rent", "5000", "1000", eur("800.00")) self.assertEqual([p.account for p in tx.postings], ["5000", "1000"]) self.assertEqual(tx.postings[0].amount, eur("800.00")) self.assertEqual(tx.postings[1].amount, eur("-800.00")) self.assertTrue(tx.is_balanced()) def test_date_may_be_an_iso_string(self): tx = Transaction("t1", "2024-02-29", "x", [Posting("1", eur("1")), Posting("2", eur("-1"))]) self.assertEqual(tx.date, date(2024, 2, 29)) def test_accounts_are_distinct_in_order(self): tx = Transaction("t1", date(2024, 1, 1), "x", [ Posting("5000", eur("3")), Posting("1000", eur("-1")), Posting("5000", eur("-2")), ]) self.assertEqual(tx.accounts(), ["5000", "1000"]) self.assertEqual(tx.amount_for("5000"), eur("1")) class ValidationTests(unittest.TestCase): def test_unbalanced_is_rejected(self): tx = Transaction("t1", date(2024, 1, 1), "x", [Posting("1", eur("10.00")), Posting("2", eur("-9.99"))]) self.assertFalse(tx.is_balanced()) with self.assertRaises(UnbalancedTransaction) as ctx: tx.validate() self.assertIn("0.01 EUR", str(ctx.exception)) def test_needs_two_postings(self): tx = Transaction("t1", date(2024, 1, 1), "x", [Posting("1", eur("0"))]) with self.assertRaises(ValueError): tx.validate() def test_balance_is_checked_per_currency(self): usd = lambda text: Money(text, "USD") balanced = Transaction("t1", date(2024, 1, 1), "x", [ Posting("1", eur("10")), Posting("2", eur("-10")), Posting("3", usd("5")), Posting("4", usd("-5")), ]) crossed = Transaction("t2", date(2024, 1, 1), "x", [Posting("1", eur("10")), Posting("2", usd("-10"))]) self.assertTrue(balanced.is_balanced()) self.assertEqual(sorted(crossed.imbalance()), ["EUR", "USD"]) class ReversalTests(unittest.TestCase): def test_reversed_negates_postings(self): tx = Transaction.transfer("t1", date(2024, 1, 5), "Pay rent", "5000", "1000", eur("800.00"), tags=["rent"]) undo = tx.reversed("t1-r", date(2024, 1, 9)) self.assertEqual(undo.id, "t1-r") self.assertEqual(undo.date, date(2024, 1, 9)) self.assertEqual(undo.description, "Reversal of Pay rent") self.assertEqual(undo.postings[0].amount, eur("-800.00")) self.assertEqual(undo.tags, ["rent"]) self.assertIsNot(undo.tags, tx.tags) def test_reversal_defaults_to_same_date(self): tx = Transaction.transfer("t1", date(2024, 1, 5), "x", "5000", "1000", eur("1.00")) self.assertEqual(tx.reversed("t1-r").date, date(2024, 1, 5)) class SplitTests(unittest.TestCase): def test_split_evenly(self): tx = Transaction.from_split( "s1", date(2024, 3, 1), "Shared bill", "1000", {"5000": 1, "5100": 1, "5200": 1}, eur("100.00"), ) self.assertEqual([str(p.amount.amount) for p in tx.postings], ["33.34", "33.33", "33.33", "-100.00"]) self.assertTrue(tx.is_balanced()) def test_split_by_ratio_gives_extra_cents_to_first_targets(self): tx = Transaction.from_split( "s2", date(2024, 3, 1), "Tip", "1000", {"5000": 3, "5100": 1, "5200": 6}, eur("0.07"), ) self.assertEqual([str(p.amount.amount) for p in tx.postings], ["0.03", "0.00", "0.04", "-0.07"]) def test_refund_split_stays_balanced(self): tx = Transaction.from_split( "s3", date(2024, 3, 1), "Refund", "1000", {"5000": 1, "5100": 1, "5200": 1}, eur("-100.00"), ) self.assertTrue(tx.is_balanced()) self.assertEqual([str(p.amount.amount) for p in tx.postings[:3]], ["-33.34", "-33.33", "-33.33"]) if __name__ == "__main__": unittest.main()