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