3 # © Copyright 2021-2022, Scott Gasch
8 from decimal import Decimal
10 from pyutils import unittest_utils
11 from pyutils.types.money import Money
14 class TestMoney(unittest.TestCase):
15 def test_basic_utility(self):
17 another = Money.parse("USD 1.45")
18 self.assertAlmostEqual(amount.amount, another.amount)
20 def test_negation(self):
23 self.assertAlmostEqual(Money(-1.45).amount, amount.amount)
25 def test_addition_and_subtraction(self):
28 total = amount + another
29 self.assertEqual(Money(3.00), total)
30 delta = another - amount
31 self.assertEqual(Money(1.00), delta)
32 neg = amount - another
33 self.assertEqual(Money(-1.00), neg)
35 self.assertEqual(Money(1.00), neg)
37 self.assertEqual(Money(2.00), neg)
39 self.assertEqual(Money(1.00), neg)
41 self.assertEqual(Money(9.0), x)
43 def test_multiplication(self):
46 self.assertEqual(Money(9.00), amount)
47 with self.assertRaises(TypeError):
51 def test_division(self):
54 self.assertEqual(Money(2.00), x)
55 with self.assertRaises(TypeError):
59 def test_equality(self):
60 usa = Money(1.0, "USD")
61 can = Money(1.0, "CAD")
62 self.assertNotEqual(usa, can)
63 eh = Money(1.0, "CAD")
64 self.assertEqual(can, eh)
66 def test_comparison(self):
71 self.assertLess(one, two)
72 self.assertLess(neg_one, one)
73 self.assertGreater(one, neg_one)
74 self.assertGreater(three, one)
75 looney = Money(1.0, "CAD")
76 with self.assertRaises(TypeError):
79 def test_strict_mode(self):
80 one = Money(1.0, strict_mode=True)
81 two = Money(2.0, strict_mode=True)
82 with self.assertRaises(TypeError):
84 self.assertEqual(Money(3.0), one + two)
85 with self.assertRaises(TypeError):
87 self.assertEqual(Money(1.0), two - one)
88 with self.assertRaises(TypeError):
90 self.assertTrue(Money(1.0) == one)
91 with self.assertRaises(TypeError):
93 self.assertTrue(one < two)
94 with self.assertRaises(TypeError):
96 self.assertTrue(two > one)
98 def test_truncate_and_round(self):
101 expected = Decimal(6.66)
102 expected = expected.quantize(Decimal(".01"))
103 self.assertEqual(expected, x.truncate_fractional_cents())
105 expected = Decimal(6.67)
106 expected = expected.quantize(Decimal(".01"))
107 self.assertEqual(expected, x.round_fractional_cents())
110 if __name__ == "__main__":