Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
227 lines (171 loc) · 4.75 KB
/
Copy pathconftest.py
File metadata and controls
227 lines (171 loc) · 4.75 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""
pytest settings shared by every test and every docstring example.
.. versionadded:: 0.1.0
"""
from pathlib import Path
from types import SimpleNamespace
from typing import Any
import matplotlib
import numpy
import pytest
from numpy.typing import NDArray
# The examples end with ``plot.show()``: this matplotlib backend opens no window, so the tests never wait.
matplotlib.use("Agg")
PROJECT_FOLDER = Path(__file__).parent
SOURCE_FOLDER = PROJECT_FOLDER / "src"
EXAMPLE_DATA_FOLDER = PROJECT_FOLDER / "tests" / "data"
TERMINAL_WIDTH_VARIABLE = "COLUMNS"
TERMINAL_WIDTH = "100"
FAR_MICROSECONDS = numpy.array([2**62], dtype="timedelta64[us]")
WRAPPED_NANOSECONDS = numpy.array([-(2**62)], dtype="timedelta64[ns]")
class FarDurations:
"""
Fake numpy array of a duration beyond nanoseconds, turned into nanoseconds as one version of numpy does.
Depending on their version, numpy raises ``OverflowError`` or wraps the value around. The fake gives both
behaviors in every version, and acts as the real durations for anything else.
.. versionadded:: 0.1.0
"""
def __init__(self, raises_overflow: bool) -> None:
"""
Hold a duration of 2**62 microseconds.
.. versionadded:: 0.1.0
Parameters
----------
raises_overflow
True to raise ``OverflowError`` when turned into nanoseconds, False to give a wrapped value.
"""
self.values = FAR_MICROSECONDS
self.dtype = FAR_MICROSECONDS.dtype
self.raises_overflow = raises_overflow
def __array__(self, dtype: Any = None, copy: Any = None) -> NDArray[Any]:
"""
Give the real durations to numpy functions.
.. versionadded:: 0.1.0
Parameters
----------
dtype
Ignored.
copy
Ignored.
Returns
-------
NDArray
The real durations.
"""
return self.values
def astype(self, dtype: numpy.dtype[Any]) -> NDArray[Any]:
"""
Convert the durations into nanoseconds, imitating one version of numpy.
.. versionadded:: 0.1.0
Parameters
----------
dtype
The new type, ignored.
Returns
-------
NDArray
A wrapped value, as the code under test only converts these durations into nanoseconds.
Raises
------
OverflowError
For a fake raising.
"""
if self.raises_overflow:
raise OverflowError
return WRAPPED_NANOSECONDS
@pytest.fixture(autouse=True)
def set_terminal_width(monkeypatch: pytest.MonkeyPatch) -> None:
"""
Give every test the same terminal width, since the text of a wide Table depends on it.
.. versionadded:: 0.1.0
Parameters
----------
monkeypatch
Restores the variable after the test.
"""
monkeypatch.setenv(TERMINAL_WIDTH_VARIABLE, TERMINAL_WIDTH)
@pytest.fixture(autouse=True)
def run_examples_in_data_folder(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> None:
"""
Run the docstring examples of numdaplot from the folder of the example files.
The examples read the example files by their name alone, as a user reads a file of the current folder.
.. versionadded:: 0.1.0
Parameters
----------
request
The running test.
monkeypatch
Restores the current folder after the test.
"""
test_path = request.path
is_source_example = test_path.is_relative_to(SOURCE_FOLDER)
if is_source_example:
monkeypatch.chdir(EXAMPLE_DATA_FOLDER)
@pytest.fixture
def overflowing_durations() -> Any:
"""
Return durations beyond nanoseconds whose conversion raises ``OverflowError``, as some versions of numpy do.
.. versionadded:: 0.1.0
Returns
-------
Any
A fake numpy array, typed Any to be given where a numpy array is expected.
"""
durations = FarDurations(True)
return durations
@pytest.fixture
def wrapping_durations() -> Any:
"""
Return durations beyond nanoseconds whose conversion wraps around, as other versions of numpy do.
.. versionadded:: 0.1.0
Returns
-------
Any
A fake numpy array, typed Any to be given where a numpy array is expected.
"""
durations = FarDurations(False)
return durations
class ModuleGiver:
"""
Fake module import giving the same module whatever its name.
.. versionadded:: 0.1.0
Parameters
----------
module
The module to give.
"""
def __init__(self, module: SimpleNamespace) -> None:
"""
Keep the module to give.
.. versionadded:: 0.1.0
Parameters
----------
module
The module to give.
"""
self.module = module
def __call__(self, module_name: str) -> SimpleNamespace:
"""
Give the module.
.. versionadded:: 0.1.0
Parameters
----------
module_name
The imported name, ignored.
Returns
-------
SimpleNamespace
The kept module.
"""
return self.module
@pytest.fixture
def module_giver() -> Any:
"""
Return the fake module import giving the same module whatever its name, to replace ``import_module``.
.. versionadded:: 0.2.0
Returns
-------
Any
The ``ModuleGiver`` class, called with the module to give.
"""
return ModuleGiver