3 # © Copyright 2021-2022, Scott Gasch
9 import unittest_utils as uu
10 from type.money import Money
13 class TestMoney(unittest.TestCase):
14 def test_basic_utility(self):
16 another = Money.parse("USD 1.45")
17 self.assertAlmostEqual(amount.amount, another.amount)
19 def test_negation(self):
22 self.assertAlmostEqual(Money(-1.45).amount, amount.amount)
24 def test_addition_and_subtraction(self):
27 total = amount + another
28 self.assertEqual(Money(3.00), total)
29 delta = another - amount
30 self.assertEqual(Money(1.00), delta)
31 neg = amount - another
32 self.assertEqual(Money(-1.00), neg)
34 self.assertEqual(Money(1.00), neg)
36 self.assertEqual(Money(2.00), neg)
38 self.assertEqual(Money(1.00), neg)
40 self.assertEqual(Money(9.0), x)
42 def test_multiplication(self):
45 self.assertEqual(Money(9.00), amount)
46 with self.assertRaises(TypeError):
50 def test_division(self):
53 self.assertEqual(Money(2.00), x)
54 with self.assertRaises(TypeError):
58 def test_equality(self):
59 usa = Money(1.0, 'USD')
60 can = Money(1.0, 'CAD')
61 self.assertNotEqual(usa, can)
62 eh = Money(1.0, 'CAD')
63 self.assertEqual(can, eh)
65 def test_comparison(self):
70 self.assertLess(one, two)
71 self.assertLess(neg_one, one)
72 self.assertGreater(one, neg_one)
73 self.assertGreater(three, one)
74 looney = Money(1.0, 'CAD')
75 with self.assertRaises(TypeError):
78 def test_strict_mode(self):
79 one = Money(1.0, strict_mode=True)
80 two = Money(2.0, strict_mode=True)
81 with self.assertRaises(TypeError):
83 self.assertEqual(Money(3.0), one + two)
84 with self.assertRaises(TypeError):
86 self.assertEqual(Money(1.0), two - one)
87 with self.assertRaises(TypeError):
89 self.assertTrue(Money(1.0) == one)
90 with self.assertRaises(TypeError):
92 self.assertTrue(one < two)
93 with self.assertRaises(TypeError):
95 self.assertTrue(two > one)
97 def test_truncate_and_round(self):
100 self.assertEqual(6.66, x.truncate_fractional_cents())
102 self.assertEqual(6.67, x.round_fractional_cents())
105 if __name__ == '__main__':