5 from type.money import Money
6 import unittest_utils as uu
9 class TestMoney(unittest.TestCase):
11 def test_basic_utility(self):
13 another = Money.parse("USD 1.45")
14 self.assertAlmostEqual(amount.amount, another.amount)
16 def test_negation(self):
19 self.assertAlmostEqual(Money(-1.45).amount, amount.amount)
21 def test_addition_and_subtraction(self):
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)
31 self.assertEqual(Money(1.00), neg)
33 self.assertEqual(Money(2.00), neg)
35 self.assertEqual(Money(1.00), neg)
37 self.assertEqual(Money(9.0), x)
39 def test_multiplication(self):
42 self.assertEqual(Money(9.00), amount)
43 with self.assertRaises(TypeError):
47 def test_division(self):
50 self.assertEqual(Money(2.00), x)
51 with self.assertRaises(TypeError):
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)
62 def test_comparison(self):
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):
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):
80 self.assertEqual(Money(3.0), one + two)
81 with self.assertRaises(TypeError):
83 self.assertEqual(Money(1.0), two - one)
84 with self.assertRaises(TypeError):
86 self.assertTrue(Money(1.0) == one)
87 with self.assertRaises(TypeError):
89 self.assertTrue(one < two)
90 with self.assertRaises(TypeError):
92 self.assertTrue(two > one)
94 def test_truncate_and_round(self):
97 self.assertEqual(6.66, x.truncate_fractional_cents())
99 self.assertEqual(6.67, x.round_fractional_cents())
102 if __name__ == '__main__':