summaryrefslogtreecommitdiff
path: root/tests/rate_test.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
committerScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
commit4faa994d32223c8d560d9dad0ca90a3f7eb10d6a (patch)
tree1200e5615d94247a120b98a4ddceb2cd6674e0af /tests/rate_test.py
parentc79ecbf708a63a54a9c3e8d189b65d4794930082 (diff)
Money, Rate, CentCount and a bunch of bugfixes.
Diffstat (limited to 'tests/rate_test.py')
-rwxr-xr-xtests/rate_test.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/rate_test.py b/tests/rate_test.py
new file mode 100755
index 0000000..621539b
--- /dev/null
+++ b/tests/rate_test.py
@@ -0,0 +1,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()