Skip to content

Commit f57ae85

Browse files
committed
TST: addition of exception tests
1 parent c7ea640 commit f57ae85

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

numpy_financial/_financial.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import numpy as np
1818

1919
__all__ = ['fv', 'pmt', 'nper', 'ipmt', 'ppmt', 'pv', 'rate',
20-
'irr', 'npv', 'mirr']
20+
'irr', 'npv', 'mirr',
21+
'NoRealSolutionException', 'IterationsExceededException']
2122

2223
_when_to_num = {'end': 0, 'begin': 1,
2324
'e': 0, 'b': 1,

numpy_financial/tests/test_financial.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# the versions in numpy instead of numpy_financial.
66
import numpy
77
from numpy.testing import (
8-
assert_, assert_almost_equal, assert_allclose, assert_equal
8+
assert_, assert_almost_equal, assert_allclose, assert_equal, assert_raises
99
)
1010
import pytest
1111

@@ -136,6 +136,26 @@ def test_gh48(self):
136136
actual = npf.rate(nper, pmt, pv, fv)
137137
assert_allclose(actual, des)
138138

139+
def test_rate_maximum_iterations_exception_scalar(self):
140+
# Test that if the maximum number of iterations is reached,
141+
# then npf.rate returns IterationsExceededException
142+
# when raise_exceptions is set to True.
143+
assert_raises(npf.IterationsExceededException, npf.rate, Decimal(12.0),
144+
Decimal(400.0), Decimal(10000.0), Decimal(5000.0),
145+
raise_exceptions=True)
146+
147+
def test_rate_maximum_iterations_exception_array(self):
148+
# Test that if the maximum number of iterations is reached in at least
149+
# one rate, then npf.rate returns IterationsExceededException
150+
# when raise_exceptions is set to True.
151+
nper = 2
152+
pmt = 0
153+
pv = [-593.06, -4725.38, -662.05, -428.78, -13.65]
154+
fv = [214.07, 4509.97, 224.11, 686.29, -329.67]
155+
assert_raises(npf.IterationsExceededException, npf.rate, nper,
156+
pmt, pv, fv,
157+
raise_exceptions=True)
158+
139159

140160
class TestNpv:
141161
def test_npv(self):
@@ -248,6 +268,13 @@ def test_mirr_decimal(self):
248268
Decimal('37000'), Decimal('46000')]
249269
assert_(numpy.isnan(npf.mirr(val, Decimal('0.10'), Decimal('0.12'))))
250270

271+
def test_mirr_no_real_solution_exception(self):
272+
# Test that if there is no solution because all the cashflows
273+
# have the same sign, then npf.mirr returns NoRealSolutionException
274+
# when raise_exceptions is set to True.
275+
val = [39000, 30000, 21000, 37000, 46000]
276+
assert_raises(npf.NoRealSolutionException, npf.mirr, val, 0.10, 0.12, raise_exceptions=True)
277+
251278

252279
class TestNper:
253280
def test_basic_values(self):
@@ -575,6 +602,7 @@ def test_some_rates_zero(self):
575602

576603

577604
class TestIrr:
605+
578606
def test_npv_irr_congruence(self):
579607
# IRR is defined as the rate required for the present value of
580608
# a series of cashflows to be zero, so we should have
@@ -671,3 +699,18 @@ def test_gh_44(self):
671699
# "true" value as calculated by Google sheets
672700
cf = [-1678.87, 771.96, 1814.05, 3520.30, 3552.95, 3584.99, 4789.91, -1]
673701
assert_almost_equal(npf.irr(cf), 1.00426, 4)
702+
703+
def test_irr_no_real_solution_exception(self):
704+
# Test that if there is no solution because all the cashflows
705+
# have the same sign, then npf.irr returns NoRealSolutionException
706+
# when raise_exceptions is set to True.
707+
cashflows = numpy.array([40000, 5000, 8000, 12000, 30000])
708+
assert_raises(npf.NoRealSolutionException, npf.irr, cashflows, raise_exceptions=True)
709+
710+
def test_irr_maximum_iterations_exception(self):
711+
# Test that if the maximum number of iterations is reached,
712+
# then npf.irr returns IterationsExceededException
713+
# when raise_exceptions is set to True.
714+
cashflows = numpy.array([-40000, 5000, 8000, 12000, 30000])
715+
assert_raises(npf.IterationsExceededException, npf.irr, cashflows,
716+
maxiter=1, raise_exceptions=True)

0 commit comments

Comments
 (0)