3 # © Copyright 2021-2022, Scott Gasch
5 """centcount unittest."""
9 import unittest_utils as uu
10 from type.centcount import CentCount
13 class TestCentCount(unittest.TestCase):
14 def test_basic_utility(self):
15 amount = CentCount(1.45)
16 another = CentCount.parse("USD 1.45")
17 self.assertEqual(amount, another)
19 def test_negation(self):
20 amount = CentCount(1.45)
22 self.assertEqual(CentCount(-1.45), amount)
24 def test_addition_and_subtraction(self):
25 amount = CentCount(1.00)
26 another = CentCount(2.00)
27 total = amount + another
28 self.assertEqual(CentCount(3.00), total)
29 delta = another - amount
30 self.assertEqual(CentCount(1.00), delta)
31 neg = amount - another
32 self.assertEqual(CentCount(-1.00), neg)
34 self.assertEqual(CentCount(1.00), neg)
36 self.assertEqual(CentCount(2.00), neg)
38 self.assertEqual(CentCount(1.00), neg)
40 self.assertEqual(CentCount(9.0), x)
42 def test_multiplication(self):
43 amount = CentCount(3.00)
45 self.assertEqual(CentCount(9.00), amount)
46 with self.assertRaises(TypeError):
47 another = CentCount(0.33)
50 def test_division(self):
51 amount = CentCount(10.00)
53 self.assertEqual(CentCount(2.00), x)
54 with self.assertRaises(TypeError):
55 another = CentCount(1.33)
58 def test_equality(self):
59 usa = CentCount(1.0, 'USD')
60 can = CentCount(1.0, 'CAD')
61 self.assertNotEqual(usa, can)
62 eh = CentCount(1.0, 'CAD')
63 self.assertEqual(can, eh)
65 def test_comparison(self):
68 three = CentCount(3.0)
69 neg_one = CentCount(-1)
70 self.assertLess(one, two)
71 self.assertLess(neg_one, one)
72 self.assertGreater(one, neg_one)
73 self.assertGreater(three, one)
74 looney = CentCount(1.0, 'CAD')
75 with self.assertRaises(TypeError):
78 def test_strict_mode(self):
79 one = CentCount(1.0, strict_mode=True)
80 two = CentCount(2.0, strict_mode=True)
81 with self.assertRaises(TypeError):
83 self.assertEqual(CentCount(3.0), one + two)
84 with self.assertRaises(TypeError):
86 self.assertEqual(CentCount(1.0), two - one)
87 with self.assertRaises(TypeError):
89 self.assertTrue(CentCount(1.0) == one)
90 with self.assertRaises(TypeError):
92 self.assertTrue(one < two)
93 with self.assertRaises(TypeError):
95 self.assertTrue(two > one)
97 def test_truncate_and_round(self):
100 x.truncate_fractional_cents()
101 self.assertEqual(CentCount(6.66), x)
104 if __name__ == '__main__':