Easier and more self documenting patterns for loading/saving Persistent
[python_utils.git] / tests / money_test.py
1 #!/usr/bin/env python3
2
3 # © Copyright 2021-2022, Scott Gasch
4
5 """money unittest."""
6
7 import unittest
8
9 import unittest_utils as uu
10 from type.money import Money
11
12
13 class TestMoney(unittest.TestCase):
14     def test_basic_utility(self):
15         amount = Money(1.45)
16         another = Money.parse("USD 1.45")
17         self.assertAlmostEqual(amount.amount, another.amount)
18
19     def test_negation(self):
20         amount = Money(1.45)
21         amount = -amount
22         self.assertAlmostEqual(Money(-1.45).amount, amount.amount)
23
24     def test_addition_and_subtraction(self):
25         amount = Money(1.00)
26         another = Money(2.00)
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)
33         neg += another
34         self.assertEqual(Money(1.00), neg)
35         neg += 1.00
36         self.assertEqual(Money(2.00), neg)
37         neg -= 1
38         self.assertEqual(Money(1.00), neg)
39         x = 10 - amount
40         self.assertEqual(Money(9.0), x)
41
42     def test_multiplication(self):
43         amount = Money(3.00)
44         amount *= 3
45         self.assertEqual(Money(9.00), amount)
46         with self.assertRaises(TypeError):
47             another = Money(0.33)
48             amount *= another
49
50     def test_division(self):
51         amount = Money(10.00)
52         x = amount / 5.0
53         self.assertEqual(Money(2.00), x)
54         with self.assertRaises(TypeError):
55             another = Money(1.33)
56             amount /= another
57
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)
64
65     def test_comparison(self):
66         one = Money(1.0)
67         two = Money(2.0)
68         three = Money(3.0)
69         neg_one = Money(-1)
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):
76             print(looney < one)
77
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):
82             x = one + 2.4
83         self.assertEqual(Money(3.0), one + two)
84         with self.assertRaises(TypeError):
85             x = two - 1.9
86         self.assertEqual(Money(1.0), two - one)
87         with self.assertRaises(TypeError):
88             print(one == 1.0)
89         self.assertTrue(Money(1.0) == one)
90         with self.assertRaises(TypeError):
91             print(one < 2.0)
92         self.assertTrue(one < two)
93         with self.assertRaises(TypeError):
94             print(two > 1.0)
95         self.assertTrue(two > one)
96
97     def test_truncate_and_round(self):
98         ten = Money(10.0)
99         x = ten * 2 / 3
100         self.assertEqual(6.66, x.truncate_fractional_cents())
101         x = ten * 2 / 3
102         self.assertEqual(6.67, x.round_fractional_cents())
103
104
105 if __name__ == '__main__':
106     unittest.main()