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