5 import unittest_utils as uu
6 from type.centcount import CentCount
9 class TestCentCount(unittest.TestCase):
10 def test_basic_utility(self):
11 amount = CentCount(1.45)
12 another = CentCount.parse("USD 1.45")
13 self.assertEqual(amount, another)
15 def test_negation(self):
16 amount = CentCount(1.45)
18 self.assertEqual(CentCount(-1.45), amount)
20 def test_addition_and_subtraction(self):
21 amount = CentCount(1.00)
22 another = CentCount(2.00)
23 total = amount + another
24 self.assertEqual(CentCount(3.00), total)
25 delta = another - amount
26 self.assertEqual(CentCount(1.00), delta)
27 neg = amount - another
28 self.assertEqual(CentCount(-1.00), neg)
30 self.assertEqual(CentCount(1.00), neg)
32 self.assertEqual(CentCount(2.00), neg)
34 self.assertEqual(CentCount(1.00), neg)
36 self.assertEqual(CentCount(9.0), x)
38 def test_multiplication(self):
39 amount = CentCount(3.00)
41 self.assertEqual(CentCount(9.00), amount)
42 with self.assertRaises(TypeError):
43 another = CentCount(0.33)
46 def test_division(self):
47 amount = CentCount(10.00)
49 self.assertEqual(CentCount(2.00), x)
50 with self.assertRaises(TypeError):
51 another = CentCount(1.33)
54 def test_equality(self):
55 usa = CentCount(1.0, 'USD')
56 can = CentCount(1.0, 'CAD')
57 self.assertNotEqual(usa, can)
58 eh = CentCount(1.0, 'CAD')
59 self.assertEqual(can, eh)
61 def test_comparison(self):
64 three = CentCount(3.0)
65 neg_one = CentCount(-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 = CentCount(1.0, 'CAD')
71 with self.assertRaises(TypeError):
74 def test_strict_mode(self):
75 one = CentCount(1.0, strict_mode=True)
76 two = CentCount(2.0, strict_mode=True)
77 with self.assertRaises(TypeError):
79 self.assertEqual(CentCount(3.0), one + two)
80 with self.assertRaises(TypeError):
82 self.assertEqual(CentCount(1.0), two - one)
83 with self.assertRaises(TypeError):
85 self.assertTrue(CentCount(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 x.truncate_fractional_cents()
97 self.assertEqual(CentCount(6.66), x)
100 if __name__ == '__main__':