Skip to content

Commit e624d26

Browse files
authored
Merge pull request #41 from Kai-Striega/move_tests_into_financial
Move tests into own classes
2 parents e3c2a4e + 6521614 commit e624d26

File tree

1 file changed

+109
-92
lines changed

1 file changed

+109
-92
lines changed

numpy_financial/tests/test_financial.py

Lines changed: 109 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,89 @@
1313

1414

1515
class TestFinancial(object):
16+
def test_when(self):
17+
# begin
18+
assert_equal(npf.rate(10, 20, -3500, 10000, 1),
19+
npf.rate(10, 20, -3500, 10000, 'begin'))
20+
# end
21+
assert_equal(npf.rate(10, 20, -3500, 10000),
22+
npf.rate(10, 20, -3500, 10000, 'end'))
23+
assert_equal(npf.rate(10, 20, -3500, 10000, 0),
24+
npf.rate(10, 20, -3500, 10000, 'end'))
25+
26+
# begin
27+
assert_equal(npf.pv(0.07, 20, 12000, 0, 1),
28+
npf.pv(0.07, 20, 12000, 0, 'begin'))
29+
# end
30+
assert_equal(npf.pv(0.07, 20, 12000, 0),
31+
npf.pv(0.07, 20, 12000, 0, 'end'))
32+
assert_equal(npf.pv(0.07, 20, 12000, 0, 0),
33+
npf.pv(0.07, 20, 12000, 0, 'end'))
34+
35+
# begin
36+
assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 1),
37+
npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'begin'))
38+
# end
39+
assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0),
40+
npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end'))
41+
assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 0),
42+
npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end'))
43+
44+
# begin
45+
assert_equal(npf.nper(0.075, -2000, 0, 100000., 1),
46+
npf.nper(0.075, -2000, 0, 100000., 'begin'))
47+
# end
48+
assert_equal(npf.nper(0.075, -2000, 0, 100000.),
49+
npf.nper(0.075, -2000, 0, 100000., 'end'))
50+
assert_equal(npf.nper(0.075, -2000, 0, 100000., 0),
51+
npf.nper(0.075, -2000, 0, 100000., 'end'))
52+
53+
def test_decimal_with_when(self):
54+
"""
55+
Test that decimals are still supported if the when argument is passed
56+
"""
57+
# begin
58+
assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
59+
Decimal('10000'), Decimal('1')),
60+
npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
61+
Decimal('10000'), 'begin'))
62+
# end
63+
assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
64+
Decimal('10000')),
65+
npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
66+
Decimal('10000'), 'end'))
67+
assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
68+
Decimal('10000'), Decimal('0')),
69+
npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
70+
Decimal('10000'), 'end'))
71+
72+
# begin
73+
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
74+
Decimal('0'), Decimal('1')),
75+
npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
76+
Decimal('0'), 'begin'))
77+
# end
78+
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
79+
Decimal('0')),
80+
npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
81+
Decimal('0'), 'end'))
82+
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
83+
Decimal('0'), Decimal('0')),
84+
npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
85+
Decimal('0'), 'end'))
86+
87+
88+
class TestPV:
89+
def test_pv(self):
90+
assert_almost_equal(npf.pv(0.07, 20, 12000, 0), -127128.17, 2)
91+
92+
def test_pv_decimal(self):
93+
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
94+
Decimal('0')),
95+
Decimal('-127128.1709461939327295222005'))
96+
97+
98+
class TestRate:
1699
def test_rate(self):
17100
assert_almost_equal(npf.rate(10, 0, -3500, 10000), 0.1107, 4)
18101

@@ -40,37 +123,50 @@ def test_rate_decimal(self):
40123
Decimal('10000'))
41124
assert_equal(Decimal('0.1106908537142689284704528100'), rate)
42125

43-
def test_pv(self):
44-
assert_almost_equal(npf.pv(0.07, 20, 12000, 0), -127128.17, 2)
45126

46-
def test_pv_decimal(self):
47-
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
48-
Decimal('0')),
49-
Decimal('-127128.1709461939327295222005'))
127+
class TestNpv:
128+
def test_npv(self):
129+
assert_almost_equal(
130+
npf.npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000]),
131+
122.89, 2)
132+
133+
def test_npv_decimal(self):
134+
assert_equal(
135+
npf.npv(Decimal('0.05'), [-15000, 1500, 2500, 3500, 4500, 6000]),
136+
Decimal('122.894854950942692161628715'))
137+
50138

51-
def test_pmt(self):
139+
class TestPmt:
140+
def test_pmt_simple(self):
52141
res = npf.pmt(0.08 / 12, 5 * 12, 15000)
53142
tgt = -304.145914
54143
assert_allclose(res, tgt)
144+
145+
def test_pmt_zero_rate(self):
55146
# Test the edge case where rate == 0.0
56147
res = npf.pmt(0.0, 5 * 12, 15000)
57148
tgt = -250.0
58149
assert_allclose(res, tgt)
150+
151+
def test_pmt_broadcast(self):
59152
# Test the case where we use broadcast and
60153
# the arguments passed in are arrays.
61154
res = npf.pmt([[0.0, 0.8], [0.3, 0.8]], [12, 3], [2000, 20000])
62155
tgt = numpy.array([[-166.66667, -19311.258], [-626.90814, -19311.258]])
63156
assert_allclose(res, tgt)
64157

65-
def test_pmt_decimal(self):
158+
def test_pmt_decimal_simple(self):
66159
res = npf.pmt(Decimal('0.08') / Decimal('12'), 5 * 12, 15000)
67160
tgt = Decimal('-304.1459143262052370338701494')
68161
assert_equal(res, tgt)
162+
163+
def test_pmt_decimal_zero_rate(self):
69164
# Test the edge case where rate == 0.0
70165
res = npf.pmt(Decimal('0'), Decimal('60'), Decimal('15000'))
71166
tgt = -250
72167
assert_equal(res, tgt)
73168

169+
def test_pmt_decimal_broadcast(self):
74170
# Test the case where we use broadcast and
75171
# the arguments passed in are arrays.
76172
res = npf.pmt([[Decimal('0'), Decimal('0.8')],
@@ -90,16 +186,8 @@ def test_pmt_decimal(self):
90186
assert_equal(res[1][0], tgt[1][0])
91187
assert_equal(res[1][1], tgt[1][1])
92188

93-
def test_npv(self):
94-
assert_almost_equal(
95-
npf.npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000]),
96-
122.89, 2)
97-
98-
def test_npv_decimal(self):
99-
assert_equal(
100-
npf.npv(Decimal('0.05'), [-15000, 1500, 2500, 3500, 4500, 6000]),
101-
Decimal('122.894854950942692161628715'))
102189

190+
class TestMirr:
103191
def test_mirr(self):
104192
val = [-4500, -800, 800, 800, 600, 600, 800, 800, 700, 3000]
105193
assert_almost_equal(npf.mirr(val, 0.08, 0.055), 0.0666, 4)
@@ -134,81 +222,6 @@ def test_mirr_decimal(self):
134222
Decimal('37000'), Decimal('46000')]
135223
assert_(numpy.isnan(npf.mirr(val, Decimal('0.10'), Decimal('0.12'))))
136224

137-
def test_when(self):
138-
# begin
139-
assert_equal(npf.rate(10, 20, -3500, 10000, 1),
140-
npf.rate(10, 20, -3500, 10000, 'begin'))
141-
# end
142-
assert_equal(npf.rate(10, 20, -3500, 10000),
143-
npf.rate(10, 20, -3500, 10000, 'end'))
144-
assert_equal(npf.rate(10, 20, -3500, 10000, 0),
145-
npf.rate(10, 20, -3500, 10000, 'end'))
146-
147-
# begin
148-
assert_equal(npf.pv(0.07, 20, 12000, 0, 1),
149-
npf.pv(0.07, 20, 12000, 0, 'begin'))
150-
# end
151-
assert_equal(npf.pv(0.07, 20, 12000, 0),
152-
npf.pv(0.07, 20, 12000, 0, 'end'))
153-
assert_equal(npf.pv(0.07, 20, 12000, 0, 0),
154-
npf.pv(0.07, 20, 12000, 0, 'end'))
155-
156-
# begin
157-
assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 1),
158-
npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'begin'))
159-
# end
160-
assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0),
161-
npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end'))
162-
assert_equal(npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 0),
163-
npf.pmt(0.08 / 12, 5 * 12, 15000., 0, 'end'))
164-
165-
# begin
166-
assert_equal(npf.nper(0.075, -2000, 0, 100000., 1),
167-
npf.nper(0.075, -2000, 0, 100000., 'begin'))
168-
# end
169-
assert_equal(npf.nper(0.075, -2000, 0, 100000.),
170-
npf.nper(0.075, -2000, 0, 100000., 'end'))
171-
assert_equal(npf.nper(0.075, -2000, 0, 100000., 0),
172-
npf.nper(0.075, -2000, 0, 100000., 'end'))
173-
174-
def test_decimal_with_when(self):
175-
"""
176-
Test that decimals are still supported if the when argument is passed
177-
"""
178-
# begin
179-
assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
180-
Decimal('10000'), Decimal('1')),
181-
npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
182-
Decimal('10000'), 'begin'))
183-
# end
184-
assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
185-
Decimal('10000')),
186-
npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
187-
Decimal('10000'), 'end'))
188-
assert_equal(npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
189-
Decimal('10000'), Decimal('0')),
190-
npf.rate(Decimal('10'), Decimal('20'), Decimal('-3500'),
191-
Decimal('10000'), 'end'))
192-
193-
# begin
194-
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
195-
Decimal('0'), Decimal('1')),
196-
npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
197-
Decimal('0'), 'begin'))
198-
# end
199-
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
200-
Decimal('0')),
201-
npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
202-
Decimal('0'), 'end'))
203-
assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
204-
Decimal('0'), Decimal('0')),
205-
npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'),
206-
Decimal('0'), 'end'))
207-
208-
def test_broadcast(self):
209-
assert_almost_equal(npf.nper(0.075, -2000, 0, 100000., [0, 1]),
210-
[21.5449442, 20.76156441], 4)
211-
212225

213226
class TestNper:
214227
def test_basic_values(self):
@@ -233,6 +246,10 @@ def test_infinite_payments(self):
233246
def test_no_interest(self):
234247
assert_(npf.nper(0, -100, 1000) == 10)
235248

249+
def test_broadcast(self):
250+
assert_almost_equal(npf.nper(0.075, -2000, 0, 100000., [0, 1]),
251+
[21.5449442, 20.76156441], 4)
252+
236253

237254
class TestPpmt:
238255
def test_float(self):

0 commit comments

Comments
 (0)