5 import unittest_utils as uu
6 from type.money import Money
9 class TestMoney(unittest.TestCase):
10 def test_basic_utility(self):
12 another = Money.parse("USD 1.45")
13 self.assertAlmostEqual(amount.amount, another.amount)
15 def test_negation(self):
18 self.assertAlmostEqual(Money(-1.45).amount, amount.amount)
20 def test_addition_and_subtraction(self):
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)
30 self.assertEqual(Money(1.00), neg)
32 self.assertEqual(Money(2.00), neg)
34 self.assertEqual(Money(1.00), neg)
36 self.assertEqual(Money(9.0), x)
38 def test_multiplication(self):
41 self.assertEqual(Money(9.00), amount)
42 with self.assertRaises(TypeError):
46 def test_division(self):
49 self.assertEqual(Money(2.00), x)
50 with self.assertRaises(TypeError):
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)
61 def test_comparison(self):
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):
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):
79 self.assertEqual(Money(3.0), one + two)
80 with self.assertRaises(TypeError):
82 self.assertEqual(Money(1.0), two - one)
83 with self.assertRaises(TypeError):
85 self.assertTrue(Money(1.0) == one)
86 with self.assertRaises(TypeError):
88 self.assertTrue(one < two)
89 with self.assertRaises(TypeError):
91 self.assertTrue(two > one)
93 def test_truncate_and_round(self):
96 self.assertEqual(6.66, x.truncate_fractional_cents())
98 self.assertEqual(6.67, x.round_fractional_cents())
101 if __name__ == '__main__':