Files
leleka/scripts/e2e/codex/tasks/fixbug-ledger/seed/tests/test_money.py
T

112 lines
4.2 KiB
Python

import unittest
from decimal import Decimal
from ledger.money import CurrencyMismatch, Money, minor_units
def amounts(parts):
return [str(p.amount) for p in parts]
class ConstructionTests(unittest.TestCase):
def test_from_str_and_int(self):
self.assertEqual(Money("12.50").amount, Decimal("12.50"))
self.assertEqual(Money(7).amount, Decimal("7"))
def test_floats_are_rejected(self):
with self.assertRaises(TypeError):
Money(0.1)
def test_currency_is_upper_cased(self):
self.assertEqual(Money("1", "usd").currency, "USD")
def test_minor_units(self):
self.assertEqual(minor_units("EUR"), 2)
self.assertEqual(minor_units("jpy"), 0)
self.assertEqual(minor_units("KWD"), 3)
def test_minor_unit_round_trip(self):
self.assertEqual(Money("12.34").to_minor(), 1234)
self.assertEqual(Money.from_minor(-5), Money("-0.05"))
self.assertEqual(Money("500", "JPY").to_minor(), 500)
self.assertEqual(Money.from_minor(1234, "KWD").amount, Decimal("1.234"))
def test_sub_unit_amount_has_no_minor_value(self):
with self.assertRaises(ValueError):
Money("0.001").to_minor()
class ArithmeticTests(unittest.TestCase):
def test_add_sub_neg_abs(self):
self.assertEqual(Money("1.10") + Money("2.20"), Money("3.30"))
self.assertEqual(Money("1.10") - Money("2.20"), Money("-1.10"))
self.assertEqual(-Money("1.10"), Money("-1.10"))
self.assertEqual(abs(Money("-4.00")), Money("4.00"))
def test_currency_mismatch(self):
with self.assertRaises(CurrencyMismatch):
Money("1", "EUR") + Money("1", "USD")
with self.assertRaises(CurrencyMismatch):
Money("1", "EUR") < Money("2", "USD")
def test_comparisons(self):
self.assertTrue(Money("1.00") < Money("1.01"))
self.assertTrue(Money("2.00") >= Money("2.0"))
self.assertFalse(Money("2.00") > Money("2.00"))
def test_multiply_rounds_to_minor_unit(self):
self.assertEqual(Money("19.99") * Decimal("0.2"), Money("4.00"))
self.assertEqual(3 * Money("0.10"), Money("0.30"))
def test_multiply_tie_rounds_half_even(self):
self.assertEqual(Money("10.00") * Decimal("0.2675"), Money("2.68"))
self.assertEqual(Money("10.00") * Decimal("0.2665"), Money("2.66"))
def test_divide(self):
self.assertEqual(Money("10.00") / 3, Money("3.33"))
def test_str(self):
self.assertEqual(str(Money("1234.5")), "1234.50 EUR")
self.assertEqual(str(Money("-3", "JPY")), "-3 JPY")
class AllocateTests(unittest.TestCase):
def test_split_in_three(self):
self.assertEqual(amounts(Money("100.00").split(3)), ["33.34", "33.33", "33.33"])
def test_allocate_by_ratio(self):
self.assertEqual(amounts(Money("100.00").allocate([70, 30])), ["70.00", "30.00"])
self.assertEqual(amounts(Money("0.05").allocate([3, 7])), ["0.02", "0.03"])
def test_leftover_goes_to_first_parts_not_largest_remainder(self):
self.assertEqual(amounts(Money("0.07").allocate([3, 1, 6])), ["0.03", "0.00", "0.04"])
def test_zero_ratio_gets_nothing(self):
self.assertEqual(amounts(Money("0.01").allocate([0, 1, 1])), ["0.00", "0.01", "0.00"])
def test_parts_always_add_up(self):
for cents in (1, 2, 99, 100, 101, 12345):
for ratios in ([1, 1], [1, 1, 1], [5, 3, 2], [1, 2, 3, 4, 5]):
parts = Money.from_minor(cents).allocate(ratios)
self.assertEqual(sum((p.amount for p in parts), Decimal(0)), Decimal(cents) / 100)
def test_split_without_minor_units(self):
self.assertEqual(amounts(Money("100", "JPY").split(3)), ["34", "33", "33"])
def test_negative_amount_split(self):
self.assertEqual(amounts(Money("-100.00").split(3)), ["-33.34", "-33.33", "-33.33"])
def test_invalid_arguments(self):
with self.assertRaises(ValueError):
Money("1.00").allocate([])
with self.assertRaises(ValueError):
Money("1.00").allocate([0, 0])
with self.assertRaises(ValueError):
Money("1.00").allocate([1, -1])
with self.assertRaises(ValueError):
Money("1.00").split(0)
if __name__ == "__main__":
unittest.main()