From 4faa994d32223c8d560d9dad0ca90a3f7eb10d6a Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Wed, 28 Jul 2021 22:13:43 -0700 Subject: Money, Rate, CentCount and a bunch of bugfixes. --- tests/rate_test.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 tests/rate_test.py (limited to 'tests/rate_test.py') 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() -- cgit v1.3