pytest-regtest 2.3.0__py2.py3-none-any.whl → 2.3.2__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pytest_regtest-2.3.0.dist-info → pytest_regtest-2.3.2.dist-info}/METADATA +8 -28
- pytest_regtest-2.3.2.dist-info/RECORD +5 -0
- pytest_regtest/__init__.py +0 -108
- pytest_regtest/numpy_handler.py +0 -216
- pytest_regtest/pandas_handler.py +0 -143
- pytest_regtest/polars_handler.py +0 -114
- pytest_regtest/pytest_regtest.py +0 -636
- pytest_regtest/register_third_party_handlers.py +0 -43
- pytest_regtest/snapshot_handler.py +0 -188
- pytest_regtest/utils.py +0 -28
- pytest_regtest-2.3.0.dist-info/RECORD +0 -13
- {pytest_regtest-2.3.0.dist-info → pytest_regtest-2.3.2.dist-info}/WHEEL +0 -0
- {pytest_regtest-2.3.0.dist-info → pytest_regtest-2.3.2.dist-info}/entry_points.txt +0 -0
- {pytest_regtest-2.3.0.dist-info → pytest_regtest-2.3.2.dist-info}/licenses/LICENSE.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: pytest-regtest
|
|
3
|
-
Version: 2.3.
|
|
3
|
+
Version: 2.3.2
|
|
4
4
|
Summary: pytest plugin for snapshot regression testing
|
|
5
5
|
Project-URL: Source, https://gitlab.com/uweschmitt/pytest-regtest
|
|
6
6
|
Project-URL: Documentation, https://pytest-regtest.readthedocs.org
|
|
@@ -14,33 +14,13 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.11
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Requires-Dist: pytest>7.2
|
|
17
|
-
Provides-Extra: dev
|
|
18
|
-
Requires-Dist: black; extra == 'dev'
|
|
19
|
-
Requires-Dist: build; extra == 'dev'
|
|
20
|
-
Requires-Dist: hatchling; extra == 'dev'
|
|
21
|
-
Requires-Dist: jinja2-cli; extra == 'dev'
|
|
22
|
-
Requires-Dist: mistletoe; extra == 'dev'
|
|
23
|
-
Requires-Dist: mkdocs; extra == 'dev'
|
|
24
|
-
Requires-Dist: mkdocs-awesome-pages-plugin; extra == 'dev'
|
|
25
|
-
Requires-Dist: mkdocs-material; extra == 'dev'
|
|
26
|
-
Requires-Dist: mkdocstrings[python]; extra == 'dev'
|
|
27
|
-
Requires-Dist: numpy; extra == 'dev'
|
|
28
|
-
Requires-Dist: numpy>=2.1.1; extra == 'dev'
|
|
29
|
-
Requires-Dist: pandas; extra == 'dev'
|
|
30
|
-
Requires-Dist: pandas>=2.2.3; extra == 'dev'
|
|
31
|
-
Requires-Dist: polars>=1.9.0; extra == 'dev'
|
|
32
|
-
Requires-Dist: pre-commit; extra == 'dev'
|
|
33
|
-
Requires-Dist: pytest-cov; extra == 'dev'
|
|
34
|
-
Requires-Dist: ruff; extra == 'dev'
|
|
35
|
-
Requires-Dist: twine; extra == 'dev'
|
|
36
|
-
Requires-Dist: wheel; extra == 'dev'
|
|
37
17
|
Description-Content-Type: text/markdown
|
|
38
18
|
|
|
39
19
|

|
|
40
20
|

|
|
41
21
|
|
|
42
22
|
|
|
43
|
-
The full
|
|
23
|
+
The full documentation for this package are available at
|
|
44
24
|
https://pytest-regtest.readthedocs.org
|
|
45
25
|
|
|
46
26
|
# About
|
|
@@ -70,12 +50,12 @@ To install and activate this plugin execute:
|
|
|
70
50
|
|
|
71
51
|
!!! note
|
|
72
52
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
53
|
+
`pytest-regtest` provides some functionality specific to `NumPy`,
|
|
54
|
+
`pandas`, and `polars`. These dependencies are not installed when
|
|
55
|
+
you install `pytest-regtest`. For example, if you are using NumPy
|
|
56
|
+
snapshots, we assume that your production code (the code under
|
|
57
|
+
test) uses NumPy and therefore should be part of your project's
|
|
58
|
+
setup.
|
|
79
59
|
|
|
80
60
|
|
|
81
61
|
## Use case 1: Changing code with no or little testing setup yet
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
pytest_regtest-2.3.2.dist-info/METADATA,sha256=g-6og_8Il3fBjTHYBRA1eGHHQ-FMIWU1Nd37oJgUBys,3275
|
|
2
|
+
pytest_regtest-2.3.2.dist-info/WHEEL,sha256=fl6v0VwpzfGBVsGtkAkhILUlJxROXbA3HvRL6Fe3140,105
|
|
3
|
+
pytest_regtest-2.3.2.dist-info/entry_points.txt,sha256=4VuIhXeMGhDo0ATbaUfyjND0atofmZjV_P-o6_uEk2s,36
|
|
4
|
+
pytest_regtest-2.3.2.dist-info/licenses/LICENSE.txt,sha256=Tue36uAzpW79-9WAqzkwPhsDDVd1X-VWUmdZ0MfGYvk,1068
|
|
5
|
+
pytest_regtest-2.3.2.dist-info/RECORD,,
|
pytest_regtest/__init__.py
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
from importlib.metadata import version as _version
|
|
2
|
-
|
|
3
|
-
import pytest
|
|
4
|
-
|
|
5
|
-
from .pytest_regtest import clear_converters # noqa: F401
|
|
6
|
-
from .pytest_regtest import patch_terminal_size # noqa: F401
|
|
7
|
-
from .pytest_regtest import register_converter_post # noqa: F401
|
|
8
|
-
from .pytest_regtest import register_converter_pre # noqa: F401
|
|
9
|
-
from .pytest_regtest import (
|
|
10
|
-
PytestRegtestCommonHooks,
|
|
11
|
-
PytestRegtestPlugin,
|
|
12
|
-
RegtestStream,
|
|
13
|
-
Snapshot,
|
|
14
|
-
SnapshotPlugin,
|
|
15
|
-
)
|
|
16
|
-
from .register_third_party_handlers import (
|
|
17
|
-
register_numpy_handler,
|
|
18
|
-
register_pandas_handler,
|
|
19
|
-
register_polars_handler,
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
from .snapshot_handler import register_python_object_handler
|
|
23
|
-
|
|
24
|
-
__version__ = _version(__package__)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def pytest_addoption(parser):
|
|
28
|
-
"""Add options to control the timeout plugin"""
|
|
29
|
-
group = parser.getgroup("regtest", "regression test plugin")
|
|
30
|
-
group.addoption(
|
|
31
|
-
"--regtest-reset",
|
|
32
|
-
action="store_true",
|
|
33
|
-
help="do not run regtest but record current output",
|
|
34
|
-
)
|
|
35
|
-
group.addoption(
|
|
36
|
-
"--regtest-tee",
|
|
37
|
-
action="store_true",
|
|
38
|
-
default=False,
|
|
39
|
-
help="print recorded results to console too",
|
|
40
|
-
)
|
|
41
|
-
group.addoption(
|
|
42
|
-
"--regtest-consider-line-endings",
|
|
43
|
-
action="store_true",
|
|
44
|
-
default=False,
|
|
45
|
-
help="do not strip whitespaces at end of recorded lines",
|
|
46
|
-
)
|
|
47
|
-
group.addoption(
|
|
48
|
-
"--regtest-nodiff",
|
|
49
|
-
action="store_true",
|
|
50
|
-
default=False,
|
|
51
|
-
help="do not show diff output for failed regresson tests",
|
|
52
|
-
)
|
|
53
|
-
group.addoption(
|
|
54
|
-
"--regtest-disable-stdconv",
|
|
55
|
-
action="store_true",
|
|
56
|
-
default=False,
|
|
57
|
-
help=(
|
|
58
|
-
"do not apply standard output converters to clean up indeterministic output"
|
|
59
|
-
),
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def pytest_configure(config):
|
|
64
|
-
common = PytestRegtestCommonHooks()
|
|
65
|
-
config.pluginmanager.register(common)
|
|
66
|
-
config.pluginmanager.register(PytestRegtestPlugin(common))
|
|
67
|
-
config.pluginmanager.register(SnapshotPlugin(common))
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
@pytest.fixture
|
|
71
|
-
def regtest(request):
|
|
72
|
-
yield RegtestStream(request)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
@pytest.fixture
|
|
76
|
-
def snapshot(request):
|
|
77
|
-
yield Snapshot(request)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
@pytest.fixture
|
|
81
|
-
def regtest_all(regtest):
|
|
82
|
-
yield regtest
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
snapshot_all_output = regtest_all
|
|
86
|
-
|
|
87
|
-
register_python_object_handler()
|
|
88
|
-
|
|
89
|
-
try:
|
|
90
|
-
import pandas # noqa: F401
|
|
91
|
-
|
|
92
|
-
register_pandas_handler()
|
|
93
|
-
except ImportError:
|
|
94
|
-
pass
|
|
95
|
-
|
|
96
|
-
try:
|
|
97
|
-
import numpy # noqa: F401
|
|
98
|
-
|
|
99
|
-
register_numpy_handler()
|
|
100
|
-
except ImportError:
|
|
101
|
-
pass
|
|
102
|
-
|
|
103
|
-
try:
|
|
104
|
-
import polars # noqa: F401
|
|
105
|
-
|
|
106
|
-
register_polars_handler()
|
|
107
|
-
except ImportError:
|
|
108
|
-
pass
|
pytest_regtest/numpy_handler.py
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import difflib
|
|
2
|
-
import io
|
|
3
|
-
import os.path
|
|
4
|
-
import warnings
|
|
5
|
-
|
|
6
|
-
import numpy as np
|
|
7
|
-
|
|
8
|
-
from .snapshot_handler import BaseSnapshotHandler
|
|
9
|
-
from .utils import highlight_mismatches
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class NumpyHandler(BaseSnapshotHandler):
|
|
13
|
-
def __init__(self, handler_options, pytest_config, tw):
|
|
14
|
-
self.atol = handler_options.get("atol", 0.0)
|
|
15
|
-
self.rtol = handler_options.get("rtol", 0.0)
|
|
16
|
-
self.equal_nan = handler_options.get("equal_nan", True)
|
|
17
|
-
if handler_options.get("print_options"):
|
|
18
|
-
warnings.warn(
|
|
19
|
-
"please use the numpy.printoptions context manager instead of"
|
|
20
|
-
" the print_options argument.",
|
|
21
|
-
DeprecationWarning,
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
self.print_options = np.get_printoptions() | handler_options.get(
|
|
25
|
-
"print_options", {}
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
def _filename(self, folder):
|
|
29
|
-
return os.path.join(folder, "arrays.npy")
|
|
30
|
-
|
|
31
|
-
def save(self, folder, obj):
|
|
32
|
-
np.save(self._filename(folder), obj)
|
|
33
|
-
|
|
34
|
-
def load(self, folder):
|
|
35
|
-
return np.load(self._filename(folder))
|
|
36
|
-
|
|
37
|
-
def show(self, obj):
|
|
38
|
-
stream = io.StringIO()
|
|
39
|
-
with np.printoptions(**self.print_options):
|
|
40
|
-
print(obj, file=stream)
|
|
41
|
-
return stream.getvalue().splitlines()
|
|
42
|
-
|
|
43
|
-
def compare(self, current_obj, recorded_obj):
|
|
44
|
-
return (
|
|
45
|
-
isinstance(current_obj, np.ndarray)
|
|
46
|
-
and current_obj.shape == recorded_obj.shape
|
|
47
|
-
and current_obj.dtype == recorded_obj.dtype
|
|
48
|
-
and np.allclose(
|
|
49
|
-
recorded_obj,
|
|
50
|
-
current_obj,
|
|
51
|
-
atol=self.atol,
|
|
52
|
-
rtol=self.rtol,
|
|
53
|
-
equal_nan=self.equal_nan,
|
|
54
|
-
)
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
def show_differences(self, current_obj, recorded_obj, has_markup):
|
|
58
|
-
lines = []
|
|
59
|
-
|
|
60
|
-
if recorded_obj.dtype != current_obj.dtype:
|
|
61
|
-
lines.extend(
|
|
62
|
-
[
|
|
63
|
-
f"dtype mismatch: current dtype: {current_obj.dtype}",
|
|
64
|
-
f" recorded dtype: {recorded_obj.dtype}",
|
|
65
|
-
]
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
recorded_as_text = self.show(recorded_obj)
|
|
69
|
-
current_as_text = self.show(current_obj)
|
|
70
|
-
|
|
71
|
-
if recorded_obj.shape == current_obj.shape:
|
|
72
|
-
if np.allclose(current_obj, recorded_obj, rtol=self.rtol, atol=self.atol):
|
|
73
|
-
return lines or None
|
|
74
|
-
|
|
75
|
-
lines.extend(self.error_diagnostics(recorded_obj, current_obj))
|
|
76
|
-
|
|
77
|
-
else:
|
|
78
|
-
lines.extend(
|
|
79
|
-
[
|
|
80
|
-
f"shape mismatch: current shape: {current_obj.shape}",
|
|
81
|
-
f" recorded shape: {recorded_obj.shape}",
|
|
82
|
-
]
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
if recorded_obj.ndim > 2:
|
|
86
|
-
return lines
|
|
87
|
-
|
|
88
|
-
if recorded_obj.ndim == 1:
|
|
89
|
-
diff_lines = list(
|
|
90
|
-
difflib.unified_diff(
|
|
91
|
-
current_as_text,
|
|
92
|
-
recorded_as_text,
|
|
93
|
-
"current",
|
|
94
|
-
"expected",
|
|
95
|
-
lineterm="",
|
|
96
|
-
)
|
|
97
|
-
)
|
|
98
|
-
lines.append("")
|
|
99
|
-
lines.extend(diff_lines)
|
|
100
|
-
|
|
101
|
-
else:
|
|
102
|
-
diff_lines = self.error_diagnostics_2d_linewise(
|
|
103
|
-
current_obj,
|
|
104
|
-
current_as_text,
|
|
105
|
-
recorded_obj,
|
|
106
|
-
recorded_as_text,
|
|
107
|
-
has_markup,
|
|
108
|
-
)
|
|
109
|
-
lines.extend(diff_lines)
|
|
110
|
-
|
|
111
|
-
if not diff_lines:
|
|
112
|
-
lines.append("diff is empty, you may want to change the print options")
|
|
113
|
-
|
|
114
|
-
return lines
|
|
115
|
-
|
|
116
|
-
def error_diagnostics(self, recorded_obj, current_obj):
|
|
117
|
-
with warnings.catch_warnings():
|
|
118
|
-
warnings.simplefilter("ignore", RuntimeWarning)
|
|
119
|
-
rel_err = np.abs(current_obj - recorded_obj) / recorded_obj
|
|
120
|
-
rel_err[(recorded_obj == 0) * (current_obj == recorded_obj)] = 0.0
|
|
121
|
-
rel_err_max_1 = np.max(rel_err)
|
|
122
|
-
rel_err_max_2 = np.max(rel_err[recorded_obj != 0])
|
|
123
|
-
|
|
124
|
-
abs_err = np.abs(current_obj - recorded_obj)
|
|
125
|
-
abs_err_max = np.max(abs_err)
|
|
126
|
-
|
|
127
|
-
lines = []
|
|
128
|
-
|
|
129
|
-
if rel_err_max_1 == rel_err_max_2:
|
|
130
|
-
lines.append(f"max relative deviation: {rel_err_max_1:e}")
|
|
131
|
-
else:
|
|
132
|
-
lines.append(f"max relative deviation: {rel_err_max_1:e}")
|
|
133
|
-
lines.append(f"max relative deviation except inf: {rel_err_max_2:e}")
|
|
134
|
-
|
|
135
|
-
lines.append(f"max absolute deviation: {abs_err_max:e}")
|
|
136
|
-
|
|
137
|
-
n_diff = np.sum(
|
|
138
|
-
np.logical_not(
|
|
139
|
-
np.isclose(current_obj, recorded_obj, rtol=self.rtol, atol=self.atol)
|
|
140
|
-
)
|
|
141
|
-
)
|
|
142
|
-
|
|
143
|
-
lines.append(
|
|
144
|
-
f"both arrays differ in {n_diff} out of {np.prod(recorded_obj.shape)}"
|
|
145
|
-
" entries"
|
|
146
|
-
)
|
|
147
|
-
lines.append(
|
|
148
|
-
f"up to given precision settings rtol={self.rtol:e} and"
|
|
149
|
-
f" atol={self.atol:e}"
|
|
150
|
-
)
|
|
151
|
-
|
|
152
|
-
return lines
|
|
153
|
-
|
|
154
|
-
def error_diagnostics_2d_linewise(
|
|
155
|
-
self, current_obj, current_as_text, recorded_obj, recorded_as_text, has_markup
|
|
156
|
-
):
|
|
157
|
-
sub_diff = []
|
|
158
|
-
|
|
159
|
-
for i, (l1, l2, r1, r2) in enumerate(
|
|
160
|
-
zip(current_as_text, recorded_as_text, current_obj, recorded_obj)
|
|
161
|
-
):
|
|
162
|
-
if r1.shape == r2.shape and np.allclose(
|
|
163
|
-
r1, r2, rtol=self.rtol, atol=self.atol
|
|
164
|
-
):
|
|
165
|
-
continue
|
|
166
|
-
|
|
167
|
-
if r1.shape == r2.shape:
|
|
168
|
-
# enforces more uniform formatting of both lines:
|
|
169
|
-
rows_together = np.vstack((r1, r2))
|
|
170
|
-
lines_together = self.show(rows_together)
|
|
171
|
-
line_diff = list(
|
|
172
|
-
difflib.unified_diff(
|
|
173
|
-
[lines_together[0][1:].strip()],
|
|
174
|
-
[lines_together[1][:-1].strip()],
|
|
175
|
-
"current",
|
|
176
|
-
"expected",
|
|
177
|
-
lineterm="",
|
|
178
|
-
)
|
|
179
|
-
)
|
|
180
|
-
else:
|
|
181
|
-
row_1 = self.show(r1)
|
|
182
|
-
row_2 = self.show(r2)
|
|
183
|
-
line_diff = list(
|
|
184
|
-
difflib.unified_diff(
|
|
185
|
-
row_1,
|
|
186
|
-
row_2,
|
|
187
|
-
"current",
|
|
188
|
-
"expected",
|
|
189
|
-
lineterm="",
|
|
190
|
-
)
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
if line_diff:
|
|
194
|
-
if not sub_diff:
|
|
195
|
-
sub_diff = line_diff[:2]
|
|
196
|
-
|
|
197
|
-
l1, l2 = line_diff[-2], line_diff[-1]
|
|
198
|
-
if has_markup:
|
|
199
|
-
l1, l2 = highlight_mismatches(l1, l2)
|
|
200
|
-
|
|
201
|
-
sub_diff.append(f"row {i:3d}: {l1}")
|
|
202
|
-
sub_diff.append(f" {l2}")
|
|
203
|
-
|
|
204
|
-
missing = len(current_as_text) - len(recorded_as_text)
|
|
205
|
-
if missing > 0:
|
|
206
|
-
for i, row in enumerate(current_as_text[-missing:], len(recorded_as_text)):
|
|
207
|
-
# remove duplicate brackets
|
|
208
|
-
row = row.rstrip("]") + "]"
|
|
209
|
-
sub_diff.append(f"row {i:3d}: -{row.lstrip()}")
|
|
210
|
-
if missing < 0:
|
|
211
|
-
for i, row in enumerate(recorded_as_text[missing:], len(current_as_text)):
|
|
212
|
-
# remove duplicate brackets
|
|
213
|
-
row = row.rstrip("]") + "]"
|
|
214
|
-
sub_diff.append(f"row {i:3d}: +{row.lstrip()}")
|
|
215
|
-
|
|
216
|
-
return sub_diff
|
pytest_regtest/pandas_handler.py
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import difflib
|
|
2
|
-
import io
|
|
3
|
-
import os.path
|
|
4
|
-
import warnings
|
|
5
|
-
|
|
6
|
-
import numpy as np
|
|
7
|
-
import pandas as pd
|
|
8
|
-
|
|
9
|
-
from .snapshot_handler import BaseSnapshotHandler
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class DataFrameHandler(BaseSnapshotHandler):
|
|
13
|
-
def __init__(self, handler_options, pytest_config, tw):
|
|
14
|
-
if handler_options.get("display_options"):
|
|
15
|
-
warnings.warn(
|
|
16
|
-
"please use the 'pandas.option_context' context manager instead of"
|
|
17
|
-
" the display_options argument.",
|
|
18
|
-
DeprecationWarning,
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
# default contains a few nested dicts and we flatten those, e.g.
|
|
22
|
-
# { "html": {"border": 1} } -> { "html.border": 1 }
|
|
23
|
-
default = list(pd.options.display.d.items())
|
|
24
|
-
default_flattened = {}
|
|
25
|
-
for k, v in default:
|
|
26
|
-
if isinstance(v, dict):
|
|
27
|
-
for k0, v0 in v.items():
|
|
28
|
-
default_flattened[f"{k}.{k0}"] = v0
|
|
29
|
-
else:
|
|
30
|
-
default_flattened[k] = v
|
|
31
|
-
|
|
32
|
-
# overwrite with user settings:
|
|
33
|
-
items = (default_flattened | handler_options.get("display_options", {})).items()
|
|
34
|
-
|
|
35
|
-
# flatten items as required by pandas.option_context:
|
|
36
|
-
self.display_options_flat = [
|
|
37
|
-
entry for item in items for entry in (f"display.{item[0]}", item[1])
|
|
38
|
-
]
|
|
39
|
-
self.atol = handler_options.get("atol", 0.0)
|
|
40
|
-
self.rtol = handler_options.get("rtol", 0.0)
|
|
41
|
-
|
|
42
|
-
def _filename(self, folder):
|
|
43
|
-
return os.path.join(folder, "dataframe.pkl")
|
|
44
|
-
|
|
45
|
-
def save(self, folder, obj):
|
|
46
|
-
obj.to_pickle(self._filename(folder), compression="gzip")
|
|
47
|
-
|
|
48
|
-
def load(self, folder):
|
|
49
|
-
return pd.read_pickle(self._filename(folder), compression="gzip")
|
|
50
|
-
|
|
51
|
-
def show(self, obj):
|
|
52
|
-
stream = io.StringIO()
|
|
53
|
-
with pd.option_context(*self.display_options_flat):
|
|
54
|
-
print(obj, file=stream)
|
|
55
|
-
return stream.getvalue().splitlines()
|
|
56
|
-
|
|
57
|
-
def compare(self, current, recorded):
|
|
58
|
-
missing = set(
|
|
59
|
-
n
|
|
60
|
-
for (n, t) in set(zip(recorded.columns, recorded.dtypes))
|
|
61
|
-
^ set(zip(current.columns, current.dtypes))
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
if missing:
|
|
65
|
-
return False
|
|
66
|
-
|
|
67
|
-
common = set(
|
|
68
|
-
n
|
|
69
|
-
for (n, t) in set(zip(recorded.columns, recorded.dtypes))
|
|
70
|
-
& set(zip(current.columns, current.dtypes))
|
|
71
|
-
)
|
|
72
|
-
current_reduced = current[[n for n in current.columns if n in common]]
|
|
73
|
-
recorded_reduced = recorded[[n for n in recorded.columns if n in common]]
|
|
74
|
-
|
|
75
|
-
def extract(df, selector):
|
|
76
|
-
return df[[n for (n, t) in zip(df.columns, df.dtypes) if selector(t)]]
|
|
77
|
-
|
|
78
|
-
current_reduced_floats = extract(
|
|
79
|
-
current_reduced, lambda t: t.type is np.float64
|
|
80
|
-
).to_numpy()
|
|
81
|
-
|
|
82
|
-
current_reduced_other = extract(
|
|
83
|
-
current_reduced, lambda t: t.type is not np.float64
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
recorded_reduced_floats = extract(
|
|
87
|
-
recorded_reduced, lambda t: t.type is np.float64
|
|
88
|
-
).to_numpy()
|
|
89
|
-
|
|
90
|
-
recorded_reduced_other = extract(
|
|
91
|
-
recorded_reduced, lambda t: t.type is not np.float64
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
return np.allclose(
|
|
95
|
-
current_reduced_floats,
|
|
96
|
-
recorded_reduced_floats,
|
|
97
|
-
atol=self.atol,
|
|
98
|
-
rtol=self.rtol,
|
|
99
|
-
equal_nan=True,
|
|
100
|
-
) and (current_reduced_other == recorded_reduced_other).all(axis=None)
|
|
101
|
-
|
|
102
|
-
def show_differences(self, current, recorded, has_markup):
|
|
103
|
-
lines = []
|
|
104
|
-
|
|
105
|
-
stream = io.StringIO()
|
|
106
|
-
current.info(buf=stream, verbose=True, memory_usage=False)
|
|
107
|
-
current_info = stream.getvalue().splitlines()[2:][:-1]
|
|
108
|
-
|
|
109
|
-
stream = io.StringIO()
|
|
110
|
-
recorded.info(buf=stream, verbose=True, memory_usage=False)
|
|
111
|
-
recorded_info = stream.getvalue().splitlines()[2:][:-1]
|
|
112
|
-
|
|
113
|
-
info_diff = list(
|
|
114
|
-
difflib.unified_diff(
|
|
115
|
-
current_info,
|
|
116
|
-
recorded_info,
|
|
117
|
-
"current",
|
|
118
|
-
"expected",
|
|
119
|
-
lineterm="",
|
|
120
|
-
)
|
|
121
|
-
)
|
|
122
|
-
lines.extend(info_diff)
|
|
123
|
-
|
|
124
|
-
recorded_as_text = self.show(recorded)
|
|
125
|
-
current_as_text = self.show(current)
|
|
126
|
-
|
|
127
|
-
diffs = list(
|
|
128
|
-
difflib.unified_diff(
|
|
129
|
-
current_as_text,
|
|
130
|
-
recorded_as_text,
|
|
131
|
-
"current",
|
|
132
|
-
"expected",
|
|
133
|
-
lineterm="",
|
|
134
|
-
)
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
lines.append("")
|
|
138
|
-
if diffs:
|
|
139
|
-
lines.extend(diffs)
|
|
140
|
-
else:
|
|
141
|
-
lines.append("diff is empty, you may want to change the print options")
|
|
142
|
-
|
|
143
|
-
return lines
|
pytest_regtest/polars_handler.py
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import difflib
|
|
2
|
-
import io
|
|
3
|
-
import os
|
|
4
|
-
from typing import Any
|
|
5
|
-
|
|
6
|
-
import polars as pl
|
|
7
|
-
from polars.testing import assert_frame_equal
|
|
8
|
-
|
|
9
|
-
from .snapshot_handler import BaseSnapshotHandler
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class PolarsHandler(BaseSnapshotHandler):
|
|
13
|
-
"""
|
|
14
|
-
PolarsHandler is a class for handling Polars DataFrame snapshots in pytest-regtest.
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
def __init__(self, handler_options: dict[str, Any], pytest_config, tw):
|
|
18
|
-
self.atol = handler_options.get("atol", 0.0)
|
|
19
|
-
self.rtol = handler_options.get("rtol", 0.0)
|
|
20
|
-
self.display_options = handler_options.get("display_options", None)
|
|
21
|
-
|
|
22
|
-
def _filename(self, folder: str | os.PathLike[Any]) -> str:
|
|
23
|
-
return os.path.join(folder, "polars.parquet")
|
|
24
|
-
|
|
25
|
-
def save(self, folder: str | os.PathLike[Any], obj: pl.DataFrame):
|
|
26
|
-
obj.write_parquet(self._filename(folder))
|
|
27
|
-
|
|
28
|
-
def load(self, folder: str | os.PathLike[Any]) -> pl.DataFrame:
|
|
29
|
-
return pl.read_parquet(self._filename(folder))
|
|
30
|
-
|
|
31
|
-
def show(self, obj: pl.DataFrame) -> list[str]:
|
|
32
|
-
stream = io.StringIO()
|
|
33
|
-
if self.display_options:
|
|
34
|
-
with pl.Config(**self.display_options):
|
|
35
|
-
stream.write(str(obj))
|
|
36
|
-
else:
|
|
37
|
-
stream.write(str(obj))
|
|
38
|
-
return stream.getvalue().splitlines()
|
|
39
|
-
|
|
40
|
-
def compare(self, current_obj: pl.DataFrame, recorded_obj: pl.DataFrame) -> bool:
|
|
41
|
-
try:
|
|
42
|
-
assert_frame_equal(
|
|
43
|
-
current_obj, recorded_obj, atol=self.atol, rtol=self.rtol
|
|
44
|
-
)
|
|
45
|
-
return True
|
|
46
|
-
except AssertionError:
|
|
47
|
-
return False
|
|
48
|
-
|
|
49
|
-
@staticmethod
|
|
50
|
-
def create_schema_info(df: pl.DataFrame) -> list[str]:
|
|
51
|
-
"""
|
|
52
|
-
Generate a summary of the schema information for a given Polars DataFrame.
|
|
53
|
-
|
|
54
|
-
Parameters:
|
|
55
|
-
df (pl.DataFrame): The Polars DataFrame for which to generate schema information.
|
|
56
|
-
|
|
57
|
-
Returns:
|
|
58
|
-
list[str]: A list of strings representing the schema information, including
|
|
59
|
-
the total number of columns, column names, non-null counts, and data types.
|
|
60
|
-
"""
|
|
61
|
-
schema = df.schema
|
|
62
|
-
schema_string_repr = [
|
|
63
|
-
"Data columns (total {} columns):".format(len(schema)),
|
|
64
|
-
" # Column Non-Null Count Dtype ",
|
|
65
|
-
"--- ------ -------------- ----- ",
|
|
66
|
-
]
|
|
67
|
-
for i, (column, dtype) in enumerate(schema.items()):
|
|
68
|
-
total_count = df.height
|
|
69
|
-
null_count = df[column].null_count()
|
|
70
|
-
non_null_count = total_count - null_count
|
|
71
|
-
dtype_str = str(dtype)
|
|
72
|
-
schema_string_repr.append(
|
|
73
|
-
f" {i} {column} {non_null_count} non-null {dtype_str}"
|
|
74
|
-
)
|
|
75
|
-
return schema_string_repr
|
|
76
|
-
|
|
77
|
-
def show_differences(
|
|
78
|
-
self, current_obj: pl.DataFrame, recorded_obj: pl.DataFrame, has_markup: bool
|
|
79
|
-
) -> list[str]:
|
|
80
|
-
lines = []
|
|
81
|
-
|
|
82
|
-
current_schema = self.create_schema_info(current_obj)
|
|
83
|
-
recorded_schema = self.create_schema_info(recorded_obj)
|
|
84
|
-
|
|
85
|
-
info_diff = list(
|
|
86
|
-
difflib.unified_diff(
|
|
87
|
-
current_schema,
|
|
88
|
-
recorded_schema,
|
|
89
|
-
"current",
|
|
90
|
-
"expected",
|
|
91
|
-
lineterm="",
|
|
92
|
-
)
|
|
93
|
-
)
|
|
94
|
-
lines.extend(info_diff)
|
|
95
|
-
recorded_as_text = self.show(recorded_obj)
|
|
96
|
-
current_as_text = self.show(current_obj)
|
|
97
|
-
|
|
98
|
-
diffs = list(
|
|
99
|
-
difflib.unified_diff(
|
|
100
|
-
current_as_text,
|
|
101
|
-
recorded_as_text,
|
|
102
|
-
"current",
|
|
103
|
-
"expected",
|
|
104
|
-
lineterm="",
|
|
105
|
-
)
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
lines.append("")
|
|
109
|
-
if diffs:
|
|
110
|
-
lines.extend(diffs)
|
|
111
|
-
else:
|
|
112
|
-
lines.append("diff is empty, you may want to change the print options")
|
|
113
|
-
|
|
114
|
-
return lines
|