Money, Rate, CentCount and a bunch of bugfixes.
[python_utils.git] / tests / money_test.py
1 #!/usr/bin/env python3
2
3 import unittest
4
5 from type.money import Money
6 import unittest_utils as uu
7
8
9 class TestMoney(unittest.TestCase):
10
11     def test_basic_utility(self):
12         amount = Money(1.45)
13         another = Money.parse("USD 1.45")
14         self.assertAlmostEqual(amount.amount, another.amount)
15
16     def test_negation(self):
17         amount = Money(1.45)
18         amount = -amount
19         self.assertAlmostEqual(Money(-1.45).amount, amount.amount)
20
21     def test_addition_and_subtraction(self):
22         amount = Money(1.00)
23         another = Money(2.00)
24         total = amount + another
25         self.assertEqual(Money(3.00), total)
26         delta = another - amount
27         self.assertEqual(Money(1.00), delta)
28         neg = amount - another
29         self.assertEqual(Money(-1.00), neg)
30         neg += another
31         self.assertEqual(Money(1.00), neg)
32         neg += 1.00
33         self.assertEqual(Money(2.00), neg)
34         neg -= 1
35         self.assertEqual(Money(1.00), neg)
36         x = 10 - amount
37         self.assertEqual(Money(9.0), x)
38
39     def test_multiplication(self):
40         amount = Money(3.00)
41         amount *= 3
42         self.assertEqual(Money(9.00), amount)
43         with self.assertRaises(TypeError):
44             another = Money(0.33)
45             amount *= another
46
47     def test_division(self):
48         amount = Money(10.00)
49         x = amount / 5.0
50         self.assertEqual(Money(2.00), x)
51         with self.assertRaises(TypeError):
52             another = Money(1.33)
53             amount /= another
54
55     def test_equality(self):
56         usa = Money(1.0, 'USD')
57         can = Money(1.0, 'CAD')
58         self.assertNotEqual(usa, can)
59         eh = Money(1.0, 'CAD')
60         self.assertEqual(can, eh)
61
62     def test_comparison(self):
63         one = Money(1.0)
64         two = Money(2.0)
65         three = Money(3.0)
66         neg_one = Money(-1)
67         self.assertLess(one, two)
68         self.assertLess(neg_one, one)
69         self.assertGreater(one, neg_one)
70         self.assertGreater(three, one)
71         looney = Money(1.0, 'CAD')
72         with self.assertRaises(TypeError):
73             print(looney < one)
74
75     def test_strict_mode(self):
76         one = Money(1.0, strict_mode=True)
77         two = Money(2.0, strict_mode=True)
78         with self.assertRaises(TypeError):
79             x = one + 2.4
80         self.assertEqual(Money(3.0), one + two)
81         with self.assertRaises(TypeError):
82             x = two - 1.9
83         self.assertEqual(Money(1.0), two - one)
84         with self.assertRaises(TypeError):
85             print(one == 1.0)
86         self.assertTrue(Money(1.0) == one)
87         with self.assertRaises(TypeError):
88             print(one < 2.0)
89         self.assertTrue(one < two)
90         with self.assertRaises(TypeError):
91             print(two > 1.0)
92         self.assertTrue(two > one)
93
94     def test_truncate_and_round(self):
95         ten = Money(10.0)
96         x = ten * 2 / 3
97         self.assertEqual(6.66, x.truncate_fractional_cents())
98         x = ten * 2 / 3
99         self.assertEqual(6.67, x.round_fractional_cents())
100
101
102 if __name__ == '__main__':
103     unittest.main()