From 96ea38693bd662b8436e29bc39af85a3acd2ae29 Mon Sep 17 00:00:00 2001 From: Rachel Blake Date: Wed, 22 Nov 2023 15:22:23 +0800 Subject: [PATCH 1/2] TST: add PV tests for basic examples --- tests/test_financial.py | 42 +++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/tests/test_financial.py b/tests/test_financial.py index af7aded..6cea500 100644 --- a/tests/test_financial.py +++ b/tests/test_financial.py @@ -3,7 +3,7 @@ # Don't use 'import numpy as np', to avoid accidentally testing # the versions in numpy instead of numpy_financial. -import numpy +import numpy as numpy from numpy.testing import ( assert_, assert_almost_equal, assert_allclose, assert_equal, assert_raises ) @@ -86,13 +86,27 @@ def test_decimal_with_when(self): class TestPV: - def test_pv(self): - assert_almost_equal(npf.pv(0.07, 20, 12000, 0), -127128.17, 2) - - def test_pv_decimal(self): - assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), - Decimal('0')), - Decimal('-127128.1709461939327295222005')) + @pytest.mark.parametrize("rate, periods, payments, future_value, exp_result", [ + (0.07, 20, 0, 12000, -3101.0280337664), + (0.025, 4, 0, 1000, -905.9506447998), + (-0.07, 56, 0, 1200000, -69845129.5182595000), + (0.05, 20, 0, -12000, 4522.6737944760), + (0.05, 356.5288568, 0, -25000.00, 0.0006971776), + (0.05, 50, 0, 0, 0.0000000000), + (0.05, 26, 500, 0, -7187.5926504975), + ]) + @pytest.mark.parametrize('number_type', [float, Decimal]) + def test_pv(self, rate, periods, payments, future_value, exp_result, number_type): + result = npf.pv(rate, periods, payments, future_value) + + if exp_result is not numpy.nan: + if number_type is Decimal: + assert result == pytest.approx(exp_result, rel=0.2) + else: + assert_allclose(float(result), float( + exp_result), atol=1e-10) + else: + assert numpy.isnan(result) class TestRate: @@ -273,7 +287,8 @@ def test_mirr(self, values, finance_rate, reinvest_rate, expected): ) def test_mirr_decimal(self, number_type, args, expected): values = [number_type(v) for v in args['values']] - result = npf.mirr(values, number_type(args['finance_rate']), number_type(args['reinvest_rate'])) + result = npf.mirr(values, number_type( + args['finance_rate']), number_type(args['reinvest_rate'])) if expected is not numpy.nan: assert_almost_equal(result, number_type(expected), 15) @@ -285,7 +300,8 @@ def test_mirr_no_real_solution_exception(self): # have the same sign, then npf.mirr returns NoRealSolutionException # when raise_exceptions is set to True. val = [39000, 30000, 21000, 37000, 46000] - assert_raises(npf.NoRealSolutionException, npf.mirr, val, 0.10, 0.12, raise_exceptions=True) + assert_raises(npf.NoRealSolutionException, npf.mirr, + val, 0.10, 0.12, raise_exceptions=True) class TestNper: @@ -709,7 +725,8 @@ def test_gh_39(self): def test_gh_44(self): # "true" value as calculated by Google sheets - cf = [-1678.87, 771.96, 1814.05, 3520.30, 3552.95, 3584.99, 4789.91, -1] + cf = [-1678.87, 771.96, 1814.05, 3520.30, + 3552.95, 3584.99, 4789.91, -1] assert_almost_equal(npf.irr(cf), 1.00426, 4) def test_irr_no_real_solution_exception(self): @@ -717,7 +734,8 @@ def test_irr_no_real_solution_exception(self): # have the same sign, then npf.irr returns NoRealSolutionException # when raise_exceptions is set to True. cashflows = numpy.array([40000, 5000, 8000, 12000, 30000]) - assert_raises(npf.NoRealSolutionException, npf.irr, cashflows, raise_exceptions=True) + assert_raises(npf.NoRealSolutionException, npf.irr, + cashflows, raise_exceptions=True) def test_irr_maximum_iterations_exception(self): # Test that if the maximum number of iterations is reached, From aaf64273e99bd3adae4c74e7e4acd92253c57493 Mon Sep 17 00:00:00 2001 From: Rachel Blake Date: Wed, 22 Nov 2023 15:48:20 +0800 Subject: [PATCH 2/2] STY: Fix import styling --- tests/test_financial.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_financial.py b/tests/test_financial.py index 22de7b1..920a060 100644 --- a/tests/test_financial.py +++ b/tests/test_financial.py @@ -3,10 +3,8 @@ # Don't use 'import numpy as np', to avoid accidentally testing # the versions in numpy instead of numpy_financial. - import numpy import pytest - from numpy.testing import ( assert_, assert_allclose,