forked from casact/chainladder-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
110 lines (82 loc) · 3 KB
/
Copy pathconftest.py
File metadata and controls
110 lines (82 loc) · 3 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
from __future__ import annotations
import pytest
import chainladder as cl
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 "prism_dense" in metafunc.fixturenames:
metafunc.parametrize(
"prism_dense", ["normal_run", "sparse_only_run"], indirect=True
)
if "prism" in metafunc.fixturenames:
metafunc.parametrize("prism", ["normal_run"], indirect=True)
if "xyz" in metafunc.fixturenames:
metafunc.parametrize("xyz", ["normal_run", "sparse_only_run"], indirect=True)
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.
"""
# Set the backend to sparse for a sparse-only-run.
cl.options.set_option("ARRAY_BACKEND", "sparse" if request.param == "sparse_only_run" else "numpy")
# Load the sample data.
tri = cl.load_sample(sample)
# Apply a transformation if supplied, then yield the triangle to the test.
yield transform(tri) if transform else tri
# After the test, reset the backend to default numpy.
cl.options.set_option("ARRAY_BACKEND", "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 prism_dense(request):
yield from _sample_fixture(request, "prism", transform=lambda t: t.sum())
@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()