Add and use valid_byte_count.
[pyutils.git] / tests / types / centcount_test.py
1 #!/usr/bin/env python3
2
3 # © Copyright 2021-2022, Scott Gasch
4
5 """centcount unittest."""
6
7 import unittest
8
9 from pyutils import unittest_utils
10 from pyutils.types.centcount import CentCount
11
12
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)
18
19     def test_negation(self):
20         amount = CentCount(1.45)
21         amount = -amount
22         self.assertEqual(CentCount(-1.45), amount)
23
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)
33         neg += another
34         self.assertEqual(CentCount(1.00), neg)
35         neg += 1.00
36         self.assertEqual(CentCount(2.00), neg)
37         neg -= 1.00
38         self.assertEqual(CentCount(1.00), neg)
39         x = 1000 - amount
40         self.assertEqual(CentCount(9.0), x)
41
42     def test_multiplication(self):
43         amount = CentCount(3.00)
44         amount *= 3
45         self.assertEqual(CentCount(9.00), amount)
46         with self.assertRaises(TypeError):
47             another = CentCount(0.33)
48             amount *= another
49
50     def test_division(self):
51         amount = CentCount(10.00)
52         x = amount / 5.0
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)
58             amount /= another
59
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)
66
67     def test_comparison(self):
68         one = CentCount(1.0)
69         two = CentCount(2.0)
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):
78             print(looney < one)
79
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):
84             x = one + 2.4
85         self.assertEqual(CentCount(3.0), one + two)
86         with self.assertRaises(TypeError):
87             x = two - 1.9
88         self.assertEqual(CentCount(1.0), two - one)
89         with self.assertRaises(TypeError):
90             print(one == 1.0)
91         self.assertTrue(CentCount(1.0) == one)
92         with self.assertRaises(TypeError):
93             print(one < 2.0)
94         self.assertTrue(one < two)
95         with self.assertRaises(TypeError):
96             print(two > 1.0)
97         self.assertTrue(two > one)
98
99
100 if __name__ == "__main__":
101     unittest.main()