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.
@@ -1,188 +0,0 @@
1
- import abc
2
- import difflib
3
- import os
4
- import pickle
5
- import pprint
6
- from collections.abc import Callable
7
- from typing import Any, Union
8
-
9
- import pytest
10
-
11
-
12
- class BaseSnapshotHandler(abc.ABC):
13
- """
14
- Abstract base class to implement snapshot handlers.
15
- """
16
-
17
- @abc.abstractmethod
18
- def __init__(
19
- handler_options: dict[str, Any],
20
- pytest_config: type[pytest.Config],
21
- tw: int,
22
- ) -> None:
23
- """
24
- This method is called within `snapshot.check` to configure the handler.
25
-
26
- Parameters:
27
- handler_options: Keyword arguments from `shapshot.check` call
28
- pytest_config: `pytest` config object.
29
- tw: Terminal width.
30
- """
31
-
32
- @abc.abstractmethod
33
- def save(self, folder: Union[str, os.PathLike], obj: Any) -> None:
34
- """
35
- This method is called when a snapshot is reset, and a subclass must store the
36
- given object in the given folder. How this folder is managed internally is
37
- entirely up to the snapshot handler. It can be a single file, or a complex
38
- structure of multiple files and sub-folders.
39
-
40
-
41
- Parameters:
42
- folder: Path to the folder. The folder exists already when this
43
- method is called.
44
- obj: The object from the `snapshot.check` call.
45
- """
46
-
47
- @abc.abstractmethod
48
- def load(self, folder: Union[str, os.PathLike]) -> Any:
49
- """
50
- This method loads an existing snapshot from the given folder and must
51
- be consistent with the `save` method.
52
-
53
- Parameters:
54
- folder: Path to the folder. The folder exists already when this
55
- method is called.
56
-
57
- Returns:
58
- The loaded object.
59
- """
60
-
61
- @abc.abstractmethod
62
- def show(self, obj: Any) -> list[str]:
63
- """
64
- This method returns a line wise textual representation of the given object.
65
-
66
- Parameters:
67
- obj: The object the snapshot handler is managing.
68
-
69
- Returns:
70
- List of strings.
71
- """
72
-
73
- @abc.abstractmethod
74
- def compare(self, current_obj: Any, recorded_obj: Any) -> bool:
75
- """
76
- The method compares if the current object from the
77
- `snapshot.check` call and the object loaded with the `load`
78
- method are considered to be the same. If this returns `True`
79
- the `snapshot.check` call will pass.
80
-
81
- Parameters:
82
- current_obj: The object the snapshot handler receiving from
83
- `snapshot.check`.
84
- recorded_obj: The object the snapshot handler is loading with
85
- the `load` method.
86
-
87
- Returns:
88
- Boolean value which decides is the `snapshot.check` call will pass.
89
- """
90
-
91
- @abc.abstractmethod
92
- def show_differences(
93
- self, current_obj: Any, recorded_obj: Any, has_markup: bool
94
- ) -> list[str]:
95
- """
96
- The method shows differences between the object from the
97
- `snapshot.check` call and the object loaded with the `load`
98
- method.
99
-
100
- Parameters:
101
- current_obj: The object the snapshot handler receiving from
102
- `snapshot.check`.
103
- recorded_obj: The object the snapshot handler is loading with
104
- the `load` method.
105
- has_markup: Indicates if the tests run within a terminal and
106
- the method can use color output in case `has_markup` is
107
- `True`.
108
-
109
- Returns:
110
- List of strings to describe the differences.
111
- """
112
-
113
-
114
- class SnapshotHandlerRegistry:
115
- """
116
- This class serves as a registry for the builtin snapshot handlers
117
- and can be used to add more handlers for particular data types.
118
- """
119
-
120
- _snapshot_handlers: list[tuple[Callable[[Any], bool]]] = []
121
-
122
- @classmethod
123
- def add_handler(
124
- clz,
125
- check_function: Callable[[Any], bool],
126
- handler_class: type[BaseSnapshotHandler],
127
- ):
128
- """Add a handler.
129
-
130
- Parameters:
131
- check_function: A function which takes an object and returns `True`
132
- if the `handler_class` argument should be
133
- used for the given object.
134
- """
135
-
136
- clz._snapshot_handlers.append((check_function, handler_class))
137
-
138
- @classmethod
139
- def get_handler(clz, obj: Any) -> BaseSnapshotHandler:
140
- """Find and initialize handler for the given object.
141
-
142
- Parameters:
143
- obj: The object for which we try to find a handler.
144
-
145
- Returns:
146
- An instance of the handler or `None` if no appropriate handler was found.
147
- """
148
- for check_function, handler in clz._snapshot_handlers:
149
- if check_function(obj):
150
- return handler
151
- return None
152
-
153
-
154
- class PythonObjectHandler(BaseSnapshotHandler):
155
- def __init__(self, handler_options, pytest_config, tw):
156
- self.compact = handler_options.get("compact", False)
157
-
158
- def save(self, folder, obj):
159
- with open(os.path.join(folder, "object.pkl"), "wb") as fh:
160
- pickle.dump(obj, fh)
161
-
162
- def load(self, folder):
163
- with open(os.path.join(folder, "object.pkl"), "rb") as fh:
164
- return pickle.load(fh)
165
-
166
- def show(self, obj):
167
- return pprint.pformat(obj, compact=self.compact).splitlines()
168
-
169
- def compare(self, current_obj, recorded_obj):
170
- return recorded_obj == current_obj
171
-
172
- def show_differences(self, current_obj, recorded_obj, has_markup):
173
- return list(
174
- difflib.unified_diff(
175
- self.show(current_obj),
176
- self.show(recorded_obj),
177
- "current",
178
- "expected",
179
- lineterm="",
180
- )
181
- )
182
-
183
-
184
- def register_python_object_handler():
185
- SnapshotHandlerRegistry.add_handler(
186
- lambda obj: isinstance(obj, (int, float, str, list, tuple, dict, set)),
187
- PythonObjectHandler,
188
- )
pytest_regtest/utils.py DELETED
@@ -1,28 +0,0 @@
1
- def highlight_mismatches(l1, l2):
2
- if not l1 and not l2:
3
- return l1, l2
4
-
5
- l1 = l1.ljust(max(len(l1), len(l2)), " ")
6
- l2 = l2.ljust(max(len(l1), len(l2)), " ")
7
-
8
- chars_1 = [l1[0]]
9
- chars_2 = [l2[0]]
10
- UNDERLINE_ON = "\x1b[21m"
11
- UNDERLINE_OFF = "\x1b[24m"
12
-
13
- is_invert = False
14
-
15
- for c1, c2 in zip(l1[1:], l2[1:]):
16
- if not is_invert and c1 != c2:
17
- chars_1.append(UNDERLINE_ON)
18
- chars_2.append(UNDERLINE_ON)
19
- is_invert = True
20
- if is_invert and c1 == c2:
21
- chars_1.append(UNDERLINE_OFF)
22
- chars_2.append(UNDERLINE_OFF)
23
- is_invert = False
24
- chars_1.append(c1)
25
- chars_2.append(c2)
26
- chars_1.append(UNDERLINE_OFF)
27
- chars_2.append(UNDERLINE_OFF)
28
- return "".join(chars_1), "".join(chars_2)
@@ -1,13 +0,0 @@
1
- pytest_regtest/__init__.py,sha256=q3wEEi3ZZXaN5CXc0jKCeupbAl73t64Fz1pFNcYYPYo,2536
2
- pytest_regtest/numpy_handler.py,sha256=hKrVY9dId-45rUR_83w9jbUWgUhd0ABk8uLbOhgm0IM,7173
3
- pytest_regtest/pandas_handler.py,sha256=7vKaHyODcKMltgn9MbbbL1FsOBQK994dEY5fVlDoQ1g,4528
4
- pytest_regtest/polars_handler.py,sha256=B1CFNz0L96W8ktFMAT_j13q7nJZVN8ug402ILJjuBZA,3771
5
- pytest_regtest/pytest_regtest.py,sha256=umtd97AIpY4FpaYJCd741mTlJbrToWkUfrkoKCvHDAE,22135
6
- pytest_regtest/register_third_party_handlers.py,sha256=mfmcyeMKuxFykkKLO7T1Tz5RPitn1pKLYRM7B9lA1Kg,1132
7
- pytest_regtest/snapshot_handler.py,sha256=5q1oMisifbQ_i8LCSPsD1OxPVEHVUIWwZeEDcZcBNDI,6036
8
- pytest_regtest/utils.py,sha256=2jYTlV_qL5hH6FCeg7T1HJJvKual-Kux2scJ9_aB1lY,811
9
- pytest_regtest-2.3.0.dist-info/METADATA,sha256=AJQuXnr5Dqc2mcGI5zdXdOoxRs227YIa7owdSFY2SQU,4097
10
- pytest_regtest-2.3.0.dist-info/WHEEL,sha256=fl6v0VwpzfGBVsGtkAkhILUlJxROXbA3HvRL6Fe3140,105
11
- pytest_regtest-2.3.0.dist-info/entry_points.txt,sha256=4VuIhXeMGhDo0ATbaUfyjND0atofmZjV_P-o6_uEk2s,36
12
- pytest_regtest-2.3.0.dist-info/licenses/LICENSE.txt,sha256=Tue36uAzpW79-9WAqzkwPhsDDVd1X-VWUmdZ0MfGYvk,1068
13
- pytest_regtest-2.3.0.dist-info/RECORD,,