-
Notifications
You must be signed in to change notification settings - Fork 2
/
conftest.py
269 lines (194 loc) · 6.99 KB
/
conftest.py
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Module-wide fixtures for testing pyppms."""
# pylint: disable-msg=fixme
import pytest
from loguru import logger
from _pytest.logging import LogCaptureFixture
from ppms_values import values
from pyppms.user import PpmsUser
__PPMS_VALUES__ = values()
### pytest setup ###
@pytest.fixture
def caplog(caplog: LogCaptureFixture):
"""Override the built-in caplog fixture to propagate Loguru messages to it.
Parameters
----------
caplog : LogCaptureFixture
Yields
------
LogCaptureFixture
"""
handler_id = logger.add(
caplog.handler,
format="{message}",
level=0,
filter=lambda record: record["level"].no >= caplog.handler.level,
enqueue=False, # Set to 'True' if your test is spawning child processes.
)
yield caplog
logger.remove(handler_id)
def pytest_addoption(parser):
"""Add a command line option '--online' to pytest."""
parser.addoption(
"--online",
action="store_true",
default=False,
help="enable online tests talking to a PUMAPI instance",
)
def pytest_collection_modifyitems(config, items):
"""Add the 'skip' marker to tests decorated with 'pytest.mark.online'."""
if config.getoption("--online"):
# --online given in cli: do not skip online tests
return
skip_online = pytest.mark.skip(reason="need --online option to run")
for item in items:
if "online" in item.keywords:
item.add_marker(skip_online)
### common helper functions to be used in fixtures below ###
def extend_raw_details(raw_details):
"""Helper function to extend a details dict with some additional elements.
Creates a copy of the given dict with user details (created by the
user_details_raw() and user_admin_details_raw() fixtures) and extends it
with some details that are useful for the related tests.
Parameters
----------
raw_details : dict
A dict with user details as created by the `user_details_raw()` and
`user_admin_details_raw()` fixtures.
Returns
-------
dict
A copy of the provided dict extended by the keys 'fullname', 'expected'
and 'api_response'.
"""
details = raw_details.copy()
details["fullname"] = f'{details["lname"]} {details["fname"]}'
details["expected"] = (
f'username: {details["login"]}, '
f'email: {details["email"]}, '
f'fullname: {details["fullname"]}, '
f'ppms_group: {details["unitlogin"]}, '
"active: True"
)
details["api_response"] = (
"login,lname,fname,"
"email,phone,bcode,affiliation,"
"unitlogin,mustchpwd,mustchbcode,active\r\n"
f'"{details["login"]}","{details["lname"]}","{details["fname"]}",'
f'"{details["email"]}","{details["phone"]}","","",'
f'"{details["unitlogin"]}",false,false,true\r\n'
)
return details
### raw user dicts ###
@pytest.fixture(scope="module")
def user_details_raw():
"""A dict with default user details matching a parsed API response.
Provides a dict with user details that corresponds to the same format that
is generated by the PpmsUser.from_response() constructor.
Returns
-------
dict
"""
return __PPMS_VALUES__["user_standard"]
@pytest.fixture(scope="module")
def user_admin_details_raw():
"""A dict with default admin-user details matching a parsed API response.
Provides a dict with user details of an admin user that corresponds to the
same format that is generated by the PpmsUser.from_response() constructor.
Returns
-------
dict
"""
return __PPMS_VALUES__["user_admin"]
### extended user dicts (with keys 'fullname', 'api_response', 'expected') ###
@pytest.fixture(scope="module")
def user_details(user_details_raw):
"""A dict with extended user details."""
return extend_raw_details(user_details_raw)
@pytest.fixture(scope="module")
def user_admin_details(user_admin_details_raw):
"""A dict with extended administrator user details."""
return extend_raw_details(user_admin_details_raw)
### PpmsUser objects ###
@pytest.fixture(scope="module")
def ppms_user(user_details):
"""Helper function to create a PpmsUser object with default values.
Parameters
----------
user_details : dict
A dictionary with user details.
Returns
-------
pyppms.user.PpmsUser
"""
return PpmsUser(user_details["api_response"])
@pytest.fixture(scope="module")
def ppms_user_admin(user_admin_details):
"""Helper function to create a PpmsUser object of an administrator user.
Parameters
----------
user_details : dict
A dictionary with user details.
Returns
-------
pyppms.user.PpmsUser
"""
return PpmsUser(user_admin_details["api_response"])
### group details ###
@pytest.fixture(scope="module")
def group_details():
"""Helper function providing a dict with default group details.
Returns
-------
dict
"""
return __PPMS_VALUES__["group"]
### system detail dicts ###
@pytest.fixture(scope="module")
def system_details_raw():
"""A dict with default system details matching a parsed API response.
Provides a dict with system details that corresponds to the same format that
is consumed by the `PpmsSystem()` constructor.
Returns
-------
dict
"""
# TODO: this *URGENTLY* needs a better solution than hard-coding the system ID!!
return __PPMS_VALUES__["system"]
### mapping dicts for user fullname, system name, ... ###
@pytest.fixture(scope="module")
def fullname_mapping(ppms_user, ppms_user_admin):
"""A dict to map user "fullnames" to login / account names."""
mapping = {
ppms_user.fullname: ppms_user.username,
ppms_user_admin.fullname: ppms_user_admin.username,
}
return mapping
@pytest.fixture(scope="module")
def systemname_mapping(system_details_raw):
"""A dict to map the system name to its ID."""
mapping = {system_details_raw["Name"]: int(system_details_raw["System id"])}
return mapping
### booking / runningsheet details ###
@pytest.fixture(scope="module")
def runningsheet_response():
"""Example response text of a 'getrunningsheet' request.
The runningsheet returned by this function has entries of four bookings
(13:00-14:00, 18:00-19:00, 20:00-21:00, 22:00-23:00), all of the same
user (pyppms) for the same system.
Returns
-------
str
The full (multi-line) text as produced by a getrunningsheet request.
"""
txt = (
"Location, Start time, End time, Object, User, Training, Assisted\n"
'"VDI (Development)","13:00","14:00","Python Development System",'
'"Python PumAPI","",""\n'
'"VDI (Development)","18:00","19:00","Python Development System",'
'"Python PumAPI","",""\n'
'"VDI (Development)","20:00","21:00","Python Development System",'
'"Python PumAPI","",""\n'
'"VDI (Development)","22:00","23:00","Python Development System",'
'"Python PumAPI","",""\n'
)
return txt