-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathtest_tools.py
More file actions
345 lines (321 loc) · 10.9 KB
/
test_tools.py
File metadata and controls
345 lines (321 loc) · 10.9 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import importlib.metadata
import json
import os
from pathlib import Path
import numpy as np
import pytest
from diffpy.utils.tools import (
_extend_z_and_convolve,
check_and_build_global_config,
compute_density_from_cif,
compute_mu_using_xraydb,
compute_mud,
get_package_info,
get_user_info,
)
@pytest.mark.parametrize(
"runtime_inputs, expected",
[ # config file in home is present, no config in cwd. various runtime
# values passed
# C1: nothing passed in, expect uname, email, orcid from home_config
(
{},
{
"owner_name": "home_ownername",
"owner_email": "home@email.com",
"owner_orcid": "home_orcid",
},
),
# C2: empty strings passed in, expect uname, email, orcid from
# home_config
(
{"owner_name": "", "owner_email": "", "owner_orcid": ""},
{
"owner_name": "home_ownername",
"owner_email": "home@email.com",
"owner_orcid": "home_orcid",
},
),
# C3: just owner name passed in at runtime. expect runtime_oname but
# others from config
(
{"owner_name": "runtime_ownername"},
{
"owner_name": "runtime_ownername",
"owner_email": "home@email.com",
"owner_orcid": "home_orcid",
},
),
# C4: just owner email passed in at runtime. expect runtime_email
# but others from config
(
{"owner_email": "runtime@email.com"},
{
"owner_name": "home_ownername",
"owner_email": "runtime@email.com",
"owner_orcid": "home_orcid",
},
),
# C5: just owner ci passed in at runtime. expect runtime_orcid but
# others from config
(
{"owner_orcid": "runtime_orcid"},
{
"owner_name": "home_ownername",
"owner_email": "home@email.com",
"owner_orcid": "runtime_orcid",
},
),
],
)
def test_get_user_info_with_home_conf_file(
runtime_inputs, expected, user_filesystem, mocker
):
# user_filesystem[0] is tmp_dir/home_dir with the global config file in it,
# user_filesystem[1] is tmp_dir/cwd_dir
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
actual = get_user_info(**runtime_inputs)
assert actual == expected
@pytest.mark.parametrize(
"runtime_inputs, expected",
[ # tests as before but now config file present in cwd and home but orcid
# missing in the cwd config
# C1: nothing passed in, expect uname, email from local config, orcid
# from home_config
(
{},
{
"owner_name": "cwd_ownername",
"owner_email": "cwd@email.com",
"owner_orcid": "home_orcid",
},
),
# C2: empty strings passed in, expect uname, email, orcid from
# home_config
(
{"owner_name": "", "owner_email": "", "owner_orcid": ""},
{
"owner_name": "cwd_ownername",
"owner_email": "cwd@email.com",
"owner_orcid": "home_orcid",
},
),
# C3: just owner name passed in at runtime. expect runtime_oname but
# others from config
(
{"owner_name": "runtime_ownername"},
{
"owner_name": "runtime_ownername",
"owner_email": "cwd@email.com",
"owner_orcid": "home_orcid",
},
),
# C4: just owner email passed in at runtime. expect runtime_email
# but others from config
(
{"owner_email": "runtime@email.com"},
{
"owner_name": "cwd_ownername",
"owner_email": "runtime@email.com",
"owner_orcid": "home_orcid",
},
),
# C5: just owner ci passed in at runtime. expect runtime_orcid but
# others from config
(
{"owner_orcid": "runtime_orcid"},
{
"owner_name": "cwd_ownername",
"owner_email": "cwd@email.com",
"owner_orcid": "runtime_orcid",
},
),
],
)
def test_get_user_info_with_local_conf_file(
runtime_inputs, expected, user_filesystem, mocker
):
# user_filesystem[0] is tmp_dir/home_dir with the global config file in it,
# user_filesystem[1] is tmp_dir/cwd_dir
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
local_config_data = {
"owner_name": "cwd_ownername",
"owner_email": "cwd@email.com",
}
with open(user_filesystem[1] / "diffpyconfig.json", "w") as f:
json.dump(local_config_data, f)
actual = get_user_info(**runtime_inputs)
assert actual == expected
@pytest.mark.parametrize(
"test_inputs,expected",
[ # Check check_and_build_global_config() builds correct config when
# config is found missing
( # C1: user inputs valid name, email and orcid
{"user_inputs": ["input_name", "input@email.com", "input_orcid"]},
{
"owner_email": "input@email.com",
"owner_orcid": "input_orcid",
"owner_name": "input_name",
},
),
(
{"user_inputs": ["", "", ""]},
None,
), # C2: empty strings passed in, expect no config file created
( # C3: just username input, expect config file but with some empty
# values
{"user_inputs": ["input_name", "", ""]},
{"owner_email": "", "owner_orcid": "", "owner_name": "input_name"},
),
],
)
def test_check_and_build_global_config(
test_inputs, expected, user_filesystem, mocker
):
# user_filesystem[0] is tmp_dir/home_dir with the global config file in it,
# user_filesystem[1] is tmp_dir/cwd_dir
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
confile = user_filesystem[0] / "diffpyconfig.json"
# remove the config file from home that came with user_filesystem
os.remove(confile)
mocker.patch("builtins.input", side_effect=test_inputs["user_inputs"])
actual_bool = check_and_build_global_config()
try:
with open(confile, "r") as f:
actual = json.load(f)
expected_bool = True
except FileNotFoundError:
expected_bool = False
actual = None
assert actual == expected
assert actual_bool == expected_bool
def test_check_and_build_global_config_file_exists(user_filesystem, mocker):
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
confile = user_filesystem[0] / "diffpyconfig.json"
expected = {
"owner_name": "home_ownername",
"owner_email": "home@email.com",
"owner_orcid": "home_orcid",
}
actual_bool = check_and_build_global_config()
assert actual_bool is True
with open(confile, "r") as f:
actual = json.load(f)
assert actual == expected
def test_check_and_build_global_config_skipped(user_filesystem, mocker):
mocker.patch.object(Path, "home", return_value=user_filesystem[0])
os.chdir(user_filesystem[1])
confile = user_filesystem[0] / "diffpyconfig.json"
# remove the config file from home that came with user_filesystem
os.remove(confile)
actual_bool = check_and_build_global_config(skip_config_creation=True)
assert actual_bool is False
assert not confile.exists()
params_package_info = [
(["diffpy.utils", None], {"package_info": {"diffpy.utils": "3.3.0"}}),
(
["package1", None],
{"package_info": {"package1": "1.2.3", "diffpy.utils": "3.3.0"}},
),
(
["package1", {"thing1": 1}],
{
"thing1": 1,
"package_info": {"package1": "1.2.3", "diffpy.utils": "3.3.0"},
},
),
(
[
"package1",
{"package_info": {"package1": "1.1.0", "package2": "3.4.5"}},
],
{
"package_info": {
"package1": "1.2.3",
"package2": "3.4.5",
"diffpy.utils": "3.3.0",
}
},
),
]
@pytest.mark.parametrize("inputs, expected", params_package_info)
def test_get_package_info(monkeypatch, inputs, expected):
monkeypatch.setattr(
importlib.metadata,
"version",
lambda package_name: (
"3.3.0" if package_name == "diffpy.utils" else "1.2.3"
),
)
actual_metadata = get_package_info(inputs[0], metadata=inputs[1])
assert actual_metadata == expected
@pytest.mark.parametrize(
"inputs, expected_density",
[
(
{
"sample_composition": "NaCl",
"cif_data_filename": "cif_data.json",
},
2.187,
),
],
)
def test_compute_density_from_cif(inputs, expected_density):
path = Path("testdata") / inputs["cif_data_filename"]
with open(path) as f:
cif_data = json.load(f)
actual_density = compute_density_from_cif(
inputs["sample_composition"], cif_data
)
assert actual_density == pytest.approx(expected_density, rel=0.01, abs=0.1)
@pytest.mark.parametrize(
"inputs",
[
# Test when the function has invalid inputs
( # C1: Both mass density and packing fraction are provided,
# expect ValueError exception
{
"sample_composition": "SiO2",
"energy": 10,
"sample_mass_density": 2.65,
"packing_fraction": 1,
}
),
( # C2: None of mass density or packing fraction are provided,
# expect ValueError exception
{
"sample_composition": "SiO2",
"energy": 10,
}
),
],
)
def test_compute_mu_using_xraydb_bad(inputs):
with pytest.raises(
ValueError,
match=(
"You must specify either sample_mass_density or "
"packing_fraction, but not both. "
"Please rerun specifying only one."
),
):
compute_mu_using_xraydb(**inputs)
def test_compute_mud(tmp_path):
diameter, slit_width, z0, I0, mud, slope = 1, 0.1, 0, 1e5, 3, 0
z_data = np.linspace(-1, 1, 50)
convolved_I_data = _extend_z_and_convolve(
z_data, diameter, slit_width, z0, I0, mud, slope
)
directory = Path(tmp_path)
file = directory / "testfile"
with open(file, "w") as f:
for x, I in zip(z_data, convolved_I_data):
f.write(f"{x}\t{I}\n")
expected_mud = 3
actual_mud = compute_mud(file)
assert actual_mud == pytest.approx(expected_mud, rel=1e-4, abs=1e-3)