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