forked from diffpy/diffpy.utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tools.py
231 lines (211 loc) · 9.42 KB
/
test_tools.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
import importlib.metadata
import json
import os
from pathlib import Path
import pytest
from diffpy.utils.tools import (
check_and_build_global_config,
compute_mu_using_xraydb,
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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "owner_orcid": "home_orcid"},
),
# C4: just owner email passed in at runtime. expect runtime_email but others from config
(
{"owner_email": "[email protected]"},
{"owner_name": "home_ownername", "owner_email": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "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": "[email protected]", "owner_orcid": "home_orcid"},
),
# C4: just owner email passed in at runtime. expect runtime_email but others from config
(
{"owner_email": "[email protected]"},
{"owner_name": "cwd_ownername", "owner_email": "[email protected]", "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": "[email protected]", "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": "[email protected]"}
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", "[email protected]", "input_orcid"]},
{"owner_email": "[email protected]", "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": "[email protected]", "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_mu",
[
# Test whether the function returns the correct mu
( # C1: Composition, energy, and mass density provided, expect to get mu based on mass density
# 1. Fully dense mass density
{"sample_composition": "quartz", "energy": 10, "sample_mass_density": 2.65},
5.0368,
),
( # 2. Measured mass density
{
"sample_composition": "ZrO2",
"energy": 17.445,
"sample_mass_density": 1.009,
},
1.2522,
),
( # C2: Composition, energy, and packing fraction provided, expect to get mu based on packing fraction
# Reuse pattern from C1.1 here
{
"sample_composition": "quartz",
"energy": 10,
"packing_fraction": 0.5,
},
2.5184,
),
],
)
def test_compute_mu_using_xraydb(inputs, expected_mu):
actual_mu = compute_mu_using_xraydb(**inputs)
assert actual_mu == pytest.approx(expected_mu, rel=1e-6, abs=1e-4)
@pytest.mark.parametrize(
"inputs",
[
# Test when the function raises ValueError
# C1: Both mass density and packing fraction are provided
(
{
"sample_composition": "quartz",
"energy": 10,
"sample_mass_density": 2.65,
"packing_fraction": 1,
}
),
# C2: None of mass density or packing fraction are provided
(
{
"sample_composition": "quartz",
"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)