3 # © Copyright 2021-2022, Scott Gasch
5 """centcount unittest."""
9 from pyutils import unittest_utils
10 from pyutils.types.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 y = amount / 1.9999999999
55 self.assertEqual(CentCount(5.00), y)
56 with self.assertRaises(TypeError):
57 another = CentCount(1.33)
60 def test_equality(self):
61 usa = CentCount(1.0, "USD")
62 can = CentCount(1.0, "CAD")
63 self.assertNotEqual(usa, can)
64 eh = CentCount(1.0, "CAD")
65 self.assertEqual(can, eh)
67 def test_comparison(self):
70 three = CentCount(3.0)
71 neg_one = CentCount(-1)
72 self.assertLess(one, two)
73 self.assertLess(neg_one, one)
74 self.assertGreater(one, neg_one)
75 self.assertGreater(three, one)
76 looney = CentCount(1.0, "CAD")
77 with self.assertRaises(TypeError):
80 def test_strict_mode(self):
81 one = CentCount(1.0, strict_mode=True)
82 two = CentCount(2.0, strict_mode=True)
83 with self.assertRaises(TypeError):
85 self.assertEqual(CentCount(3.0), one + two)
86 with self.assertRaises(TypeError):
88 self.assertEqual(CentCount(1.0), two - one)
89 with self.assertRaises(TypeError):
91 self.assertTrue(CentCount(1.0) == one)
92 with self.assertRaises(TypeError):
94 self.assertTrue(one < two)
95 with self.assertRaises(TypeError):
97 self.assertTrue(two > one)
100 if __name__ == "__main__":