forked from diffpy/diffpy.utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tools.py
215 lines (195 loc) · 8.94 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
import importlib.metadata
import json
import os
from pathlib import Path
import pytest
from diffpy.utils.tools import compute_mu_using_xraydb, get_package_info, get_user_info
# def _setup_dirs(monkeypatch, user_filesystem):
# home_dir, cwd_dir = user_filesystem.home_dir, user_filesystem.cwd_dir
# os.chdir(cwd_dir)
# return home_dir
#
def _run_tests(inputs, expected):
args = {"username": inputs[0], "email": inputs[1]}
expected_username, expected_email = expected
config = get_user_info(args)
assert config.get("username") == expected_username
assert config.get("email") == expected_email
params_user_info_with_local_conf_file = [
(["", ""], ["cwd_username", "[email protected]"]),
(["cli_username", ""], ["cli_username", "[email protected]"]),
(["", "[email protected]"], ["cwd_username", "[email protected]"]),
([None, None], ["cwd_username", "[email protected]"]),
(["cli_username", None], ["cli_username", "[email protected]"]),
([None, "[email protected]"], ["cwd_username", "[email protected]"]),
(["cli_username", "[email protected]"], ["cli_username", "[email protected]"]),
]
params_user_info_with_no_home_conf_file = [
(
[None, None],
["input_username", "[email protected]"],
["input_username", "[email protected]"],
),
(
["cli_username", None],
["", "[email protected]"],
["cli_username", "[email protected]"],
),
(
[None, "[email protected]"],
["input_username", ""],
["input_username", "[email protected]"],
),
(
["", ""],
["input_username", "[email protected]"],
["input_username", "[email protected]"],
),
(
["cli_username", ""],
["", "[email protected]"],
["cli_username", "[email protected]"],
),
(
["", "[email protected]"],
["input_username", ""],
["input_username", "[email protected]"],
),
(
["cli_username", "[email protected]"],
["input_username", "[email protected]"],
["cli_username", "[email protected]"],
),
]
params_user_info_no_conf_file_no_inputs = [
([None, None], ["", ""], ["", ""]),
]
@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("inputsa, inputsb, expected", params_user_info_with_no_home_conf_file)
# def test_get_user_info_with_no_home_conf_file(monkeypatch, inputsa, inputsb, expected, user_filesystem):
# _setup_dirs(monkeypatch, user_filesystem)
# os.remove(Path().home() / "diffpyconfig.json")
# inp_iter = iter(inputsb)
# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
# _run_tests(inputsa, expected)
# confile = Path().home() / "diffpyconfig.json"
# assert confile.is_file()
#
#
# @pytest.mark.parametrize("inputsa, inputsb, expected", params_user_info_no_conf_file_no_inputs)
# def test_get_user_info_no_conf_file_no_inputs(monkeypatch, inputsa, inputsb, expected, user_filesystem):
# _setup_dirs(monkeypatch, user_filesystem)
# os.remove(Path().home() / "diffpyconfig.json")
# inp_iter = iter(inputsb)
# monkeypatch.setattr("builtins.input", lambda _: next(inp_iter))
# _run_tests(inputsa, expected)
# confile = Path().home() / "diffpyconfig.json"
# assert confile.exists() is False
#
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
params_mu = [
# C1: user didn't specify density or packing fraction
({"sample_composition": "H2O", "energy": 10000, "density": None, "packing_fraction": 1}, 0.5330),
# C2: user specified packing fraction only
({"sample_composition": "H2O", "energy": 10000, "density": None, "packing_fraction": 0.5}, 0.2665),
# C3: user specified density only
({"sample_composition": "H2O", "energy": 10000, "density": 0.997, "packing_fraction": 1}, 0.5330),
({"sample_composition": "H2O", "energy": 10000, "density": 0.4985, "packing_fraction": 1}, 0.2665),
# C4: user specified a standard density and a packing fraction
({"sample_composition": "H2O", "energy": 10000, "density": 0.997, "packing_fraction": 0.5}, 0.2665),
]
@pytest.mark.parametrize("inputs, expected", params_mu)
def test_compute_mu_using_xraydb(inputs, expected):
actual_mu = compute_mu_using_xraydb(
inputs["sample_composition"],
inputs["energy"],
density=inputs["density"],
packing_fraction=inputs["packing_fraction"],
)
assert actual_mu == pytest.approx(expected, rel=0.01, abs=0.1)