Money, Rate, CentCount and a bunch of bugfixes.
[python_utils.git] / tests / rate_test.py
diff --git a/tests/rate_test.py b/tests/rate_test.py
new file mode 100755 (executable)
index 0000000..621539b
--- /dev/null
@@ -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()