Easier and more self documenting patterns for loading/saving Persistent
[python_utils.git] / tests / rate_test.py
1 #!/usr/bin/env python3
2
3 # © Copyright 2021-2022, Scott Gasch
4
5 """rate unittest."""
6
7 import unittest
8
9 import unittest_utils as uu
10 from type.money import Money
11 from type.rate import Rate
12
13
14 class TestRate(unittest.TestCase):
15     def test_basic_utility(self):
16         my_stock_returns = Rate(percent_change=-20.0)
17         my_portfolio = 1000.0
18         self.assertAlmostEqual(800.0, my_stock_returns.apply_to(my_portfolio))
19
20         my_bond_returns = Rate(percentage=104.5)
21         my_money = Money(500.0)
22         self.assertAlmostEqual(Money(522.5), my_bond_returns.apply_to(my_money))
23
24         my_multiplier = Rate(multiplier=1.72)
25         my_nose_length = 3.2
26         self.assertAlmostEqual(5.504, my_multiplier.apply_to(my_nose_length))
27
28     def test_conversions(self):
29         x = Rate(104.55)
30         s = x.__repr__()
31         y = Rate(s)
32         self.assertAlmostEqual(x, y)
33         f = float(x)
34         z = Rate(f)
35         self.assertAlmostEqual(x, z)
36
37     def test_divide(self):
38         x = Rate(20.0)
39         x /= 2
40         self.assertAlmostEqual(10.0, x)
41         x = Rate(-20.0)
42         x /= 2
43         self.assertAlmostEqual(-10.0, x)
44
45     def test_add(self):
46         x = Rate(5.0)
47         y = Rate(10.0)
48         z = x + y
49         self.assertAlmostEqual(15.0, z)
50         x = Rate(-5.0)
51         x += y
52         self.assertAlmostEqual(5.0, x)
53
54     def test_sub(self):
55         x = Rate(5.0)
56         y = Rate(10.0)
57         z = x - y
58         self.assertAlmostEqual(-5.0, z)
59         z = y - x
60         self.assertAlmostEqual(5.0, z)
61
62     def test_repr(self):
63         x = Rate(percent_change=-50.0)
64         s = x.__repr__(relative=True)
65         self.assertEqual("-50.000%", s)
66         s = x.__repr__()
67         self.assertEqual("+50.000%", s)
68
69
70 if __name__ == '__main__':
71     unittest.main()