summaryrefslogtreecommitdiff
path: root/tests/rate_test.py
blob: 621539b112b12775f30749dae98886b2246f3d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3

import unittest

from type.rate import Rate
from type.money import Money

import unittest_utils as uu


class TestRate(unittest.TestCase):
    def test_basic_utility(self):
        my_stock_returns = Rate(percent_change=-20.0)
        my_portfolio = 1000.0
        self.assertAlmostEqual(
            800.0,
            my_stock_returns.apply_to(my_portfolio)
        )

        my_bond_returns = Rate(percentage=104.5)
        my_money = Money(500.0)
        self.assertAlmostEqual(
            Money(522.5),
            my_bond_returns.apply_to(my_money)
        )

        my_multiplier = Rate(multiplier=1.72)
        my_nose_length = 3.2
        self.assertAlmostEqual(
            5.504,
            my_multiplier.apply_to(my_nose_length)
        )

    def test_conversions(self):
        x = Rate(104.55)
        s = x.__repr__()
        y = Rate(s)
        self.assertAlmostEqual(x, y)
        f = float(x)
        z = Rate(f)
        self.assertAlmostEqual(x, z)

    def test_divide(self):
        x = Rate(20.0)
        x /= 2
        self.assertAlmostEqual(10.0, x)
        x = Rate(-20.0)
        x /= 2
        self.assertAlmostEqual(-10.0, x)

    def test_add(self):
        x = Rate(5.0)
        y = Rate(10.0)
        z = x + y
        self.assertAlmostEqual(15.0, z)
        x = Rate(-5.0)
        x += y
        self.assertAlmostEqual(5.0, x)

    def test_sub(self):
        x = Rate(5.0)
        y = Rate(10.0)
        z = x - y
        self.assertAlmostEqual(-5.0, z)
        z = y - x
        self.assertAlmostEqual(5.0, z)

    def test_repr(self):
        x = Rate(percent_change=-50.0)
        s = x.__repr__(relative=True)
        self.assertEqual("-50.000%", s)
        s = x.__repr__()
        self.assertEqual("+50.000%", s)


if __name__ == '__main__':
    unittest.main()