-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathconftest.py
More file actions
140 lines (104 loc) · 3.62 KB
/
Copy pathconftest.py
File metadata and controls
140 lines (104 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from __future__ import annotations
import chainladder as cl
import functools
import pytest
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterator
from chainladder import Triangle
from typing import (
Any,
Callable,
)
def pytest_generate_tests(metafunc):
if "raa" in metafunc.fixturenames:
metafunc.parametrize("raa", ["normal_run", "sparse_only_run"], indirect=True)
if "qtr" in metafunc.fixturenames:
metafunc.parametrize("qtr", ["normal_run", "sparse_only_run"], indirect=True)
if "clrd" in metafunc.fixturenames:
metafunc.parametrize("clrd", ["normal_run", "sparse_only_run"], indirect=True)
if "genins" in metafunc.fixturenames:
metafunc.parametrize("genins", ["normal_run", "sparse_only_run"], indirect=True)
if "monthly" in metafunc.fixturenames:
metafunc.parametrize(
"monthly", ["normal_run", "sparse_only_run"], indirect=True
)
if "prism" in metafunc.fixturenames:
metafunc.parametrize("prism", ["sparse_only_run"], indirect=True)
if "tail_sample" in metafunc.fixturenames:
metafunc.parametrize(
"tail_sample", ["normal_run", "sparse_only_run"], indirect=True
)
if "xyz" in metafunc.fixturenames:
metafunc.parametrize("xyz", ["normal_run", "sparse_only_run"], indirect=True)
@functools.lru_cache(maxsize=None)
def _cached_load_sample(sample: str) -> Triangle:
"""
Create a cache of the requested sample Triangle when called initially,
then load the cache when called again.
The cache is preserved throughout the test session.
Parameters
----------
sample: str
The requested triangle, e.g., "clrd", "raa", etc.
Returns
-------
Triangle
The cached triangle.
"""
return cl.load_sample(sample)
def _sample_fixture(
request: Any,
sample: str,
transform: Callable[[Triangle], Triangle] | None = None,
) -> Iterator[Triangle]:
"""
Common template fixture for using sample data in unit tests.
Parameters
----------
request:Any
The pytest request built-in.
sample: str
The name of the sample data set to be loaded, e.g., raa, clrd, etc.
transform: Callable[[Triangle], Triangle] | None
An optional transformation to be applied to the triangle supplied as a lambda function.
Yields
-------
A Triangle, with backend set according to request.param.
"""
# Load a copy of cached sample data.
tri = _cached_load_sample(sample).copy()
# Apply a transformation if supplied
tri = transform(tri) if transform else tri
# Set the backend to sparse for a sparse-only-run, then yield the triangle to the test.
yield tri.set_backend("sparse" if request.param == "sparse_only_run" else "numpy")
@pytest.fixture
def raa(request):
yield from _sample_fixture(request, "raa")
@pytest.fixture
def qtr(request):
yield from _sample_fixture(request, "quarterly")
@pytest.fixture
def clrd(request):
yield from _sample_fixture(request, "clrd")
@pytest.fixture
def genins(request):
yield from _sample_fixture(request, "genins")
@pytest.fixture
def prism(request):
yield from _sample_fixture(request, "prism")
@pytest.fixture
def monthly(request):
yield from _sample_fixture(request, "prism", transform=lambda t: t.sum())
@pytest.fixture
def tail_sample(request):
yield from _sample_fixture(request, "tail_sample")
@pytest.fixture
def xyz(request):
yield from _sample_fixture(request, "xyz")
@pytest.fixture
def atol():
return 1e-4
@pytest.fixture
def empty_triangle():
return cl.Triangle()