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