|
1 | 1 | import math
|
2 | 2 | from decimal import Decimal
|
| 3 | +from typing import Literal, Union |
| 4 | + |
| 5 | +import hypothesis.strategies as st |
3 | 6 |
|
4 | 7 | # Don't use 'import numpy as np', to avoid accidentally testing
|
5 | 8 | # the versions in numpy instead of numpy_financial.
|
6 | 9 | import numpy
|
7 | 10 | import pytest
|
| 11 | +from hypothesis import Verbosity, given, settings |
8 | 12 | from numpy.testing import (
|
9 | 13 | assert_,
|
10 | 14 | assert_allclose,
|
|
15 | 19 |
|
16 | 20 | import numpy_financial as npf
|
17 | 21 |
|
18 |
| - |
19 | 22 | class TestFinancial(object):
|
20 | 23 | def test_when(self):
|
21 | 24 | # begin
|
@@ -90,13 +93,168 @@ def test_decimal_with_when(self):
|
90 | 93 |
|
91 | 94 |
|
92 | 95 | class TestPV:
|
| 96 | + # Test cases for pytest parametrized example-based tests |
| 97 | + test_cases = { |
| 98 | + "default_fv_and_when": { |
| 99 | + "inputs": { |
| 100 | + "rate": 0.05, |
| 101 | + "nper": 10, |
| 102 | + "pmt": 1000, |
| 103 | + }, |
| 104 | + "expected_result": -7721.73, |
| 105 | + }, |
| 106 | + "specify_fv_and_when": { |
| 107 | + "inputs": { |
| 108 | + "rate": 0.05, |
| 109 | + "nper": 10, |
| 110 | + "pmt": 1000, |
| 111 | + "fv": 0, |
| 112 | + "when": 0, |
| 113 | + }, |
| 114 | + "expected_result": -7721.73, |
| 115 | + }, |
| 116 | + "when_1": { |
| 117 | + "inputs": { |
| 118 | + "rate": 0.05, |
| 119 | + "nper": 10, |
| 120 | + "pmt": 1000, |
| 121 | + "fv": 0, |
| 122 | + "when": 1, |
| 123 | + }, |
| 124 | + "expected_result": -8107.82, |
| 125 | + }, |
| 126 | + "when_1_and_fv_1000": { |
| 127 | + "inputs": { |
| 128 | + "rate": 0.05, |
| 129 | + "nper": 10, |
| 130 | + "pmt": 1000, |
| 131 | + "fv": 1000, |
| 132 | + "when": 1, |
| 133 | + }, |
| 134 | + "expected_result": -8721.73, |
| 135 | + }, |
| 136 | + "fv>0": { |
| 137 | + "inputs": { |
| 138 | + "rate": 0.05, |
| 139 | + "nper": 10, |
| 140 | + "pmt": 1000, |
| 141 | + "fv": 1000, |
| 142 | + }, |
| 143 | + "expected_result": -8335.65, |
| 144 | + }, |
| 145 | + "negative_rate": { |
| 146 | + "inputs": { |
| 147 | + "rate": -0.05, |
| 148 | + "nper": 10, |
| 149 | + "pmt": 1000, |
| 150 | + "fv": 0, |
| 151 | + }, |
| 152 | + "expected_result": -13403.65, |
| 153 | + }, |
| 154 | + "rates_as_array": { |
| 155 | + "inputs": { |
| 156 | + "rate": numpy.array([0.010, 0.015, 0.020, 0.025, 0.030, 0.035]), |
| 157 | + "nper": 10, |
| 158 | + "pmt": 1000, |
| 159 | + "fv": 0, |
| 160 | + }, |
| 161 | + "expected_result": numpy.array( |
| 162 | + [-9471.30, -9222.18, -8982.59, -8752.06, -8530.20, -8316.61] |
| 163 | + ), |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + # Randomized input strategies for fuzz tests & property-based tests |
| 168 | + numeric_strategy = st.one_of( |
| 169 | + st.decimals(), |
| 170 | + st.floats(), |
| 171 | + st.integers(), |
| 172 | + # arrays(dtype=scalar_dtypes(), shape=array_shapes(max_dims=1)), |
| 173 | + ) |
| 174 | + |
| 175 | + when_period_strategy = st.sampled_from(["end", "begin", 1, 0]) |
| 176 | + |
93 | 177 | def test_pv(self):
|
94 | 178 | assert_almost_equal(npf.pv(0.07, 20, 12000, 0), -127128.17, 2)
|
95 | 179 |
|
96 | 180 | def test_pv_decimal(self):
|
97 |
| - assert_equal(npf.pv(Decimal('0.07'), Decimal('20'), Decimal('12000'), |
98 |
| - Decimal('0')), |
99 |
| - Decimal('-127128.1709461939327295222005')) |
| 181 | + assert_equal( |
| 182 | + npf.pv(Decimal("0.07"), Decimal("20"), Decimal("12000"), Decimal("0")), |
| 183 | + Decimal("-127128.1709461939327295222005"), |
| 184 | + ) |
| 185 | + |
| 186 | + @pytest.mark.parametrize("test_case", test_cases.values(), ids=test_cases.keys()) |
| 187 | + def test_pv_examples(self, test_case): |
| 188 | + inputs, expected_result = test_case["inputs"], test_case["expected_result"] |
| 189 | + result = npf.pv(**inputs) |
| 190 | + assert result == pytest.approx(expected_result) |
| 191 | + |
| 192 | + @pytest.mark.slow |
| 193 | + @given( |
| 194 | + rate=numeric_strategy, |
| 195 | + nper=numeric_strategy, |
| 196 | + pmt=numeric_strategy, |
| 197 | + fv=numeric_strategy, |
| 198 | + when=when_period_strategy, |
| 199 | + ) |
| 200 | + @settings(verbosity=Verbosity.verbose) |
| 201 | + def test_pv_fuzz( |
| 202 | + self, |
| 203 | + rate: Union[int, float, Decimal, numpy.ndarray], |
| 204 | + nper: Union[int, float, Decimal, numpy.ndarray], |
| 205 | + pmt: Union[int, float, Decimal, numpy.ndarray], |
| 206 | + fv: Union[int, float, Decimal, numpy.ndarray], |
| 207 | + when: Literal[0, 1, "begin", "end"], |
| 208 | + ) -> None: |
| 209 | + npf.pv(rate, nper, pmt, fv, when) |
| 210 | + |
| 211 | + @pytest.mark.slow |
| 212 | + @given( |
| 213 | + rate=st.floats(), |
| 214 | + nper=st.floats(), |
| 215 | + pmt=st.floats(), |
| 216 | + fv=st.floats(), |
| 217 | + when=when_period_strategy, |
| 218 | + ) |
| 219 | + @settings(verbosity=Verbosity.verbose) |
| 220 | + def test_pv_time_value_of_money( |
| 221 | + self, |
| 222 | + rate: float, |
| 223 | + nper: float, |
| 224 | + pmt: float, |
| 225 | + fv: float, |
| 226 | + when: Literal[0, 1, "begin", "end"], |
| 227 | + ) -> None: |
| 228 | + """ |
| 229 | + Test that the present value is inversely proportional to number of periods, |
| 230 | + all other things being equal. |
| 231 | + """ |
| 232 | + npf.pv(rate, nper, pmt, fv, when) > npf.pv( |
| 233 | + rate, float(nper) + float(1), pmt, fv, when |
| 234 | + ) |
| 235 | + |
| 236 | + @pytest.mark.slow |
| 237 | + @given( |
| 238 | + rate=st.floats(), |
| 239 | + nper=st.floats(), |
| 240 | + pmt=st.floats(), |
| 241 | + fv=st.floats(), |
| 242 | + when=when_period_strategy, |
| 243 | + ) |
| 244 | + @settings(verbosity=Verbosity.verbose) |
| 245 | + def test_pv_interest_rate_sensitivity( |
| 246 | + self, |
| 247 | + rate: float, |
| 248 | + nper: float, |
| 249 | + pmt: float, |
| 250 | + fv: float, |
| 251 | + when: Literal[0, 1, "begin", "end"], |
| 252 | + ) -> None: |
| 253 | + """ |
| 254 | + Test that the present value is inversely proportional to the interest rate, |
| 255 | + all other things being equal. |
| 256 | + """ |
| 257 | + npf.pv(rate, nper, pmt, fv, when) > npf.pv(rate + 0.1, nper, pmt, fv, when) |
100 | 258 |
|
101 | 259 |
|
102 | 260 | class TestRate:
|
|
0 commit comments