Skip to content

Commit bc118ea

Browse files
feat: add tests
1 parent 411ba59 commit bc118ea

File tree

2 files changed

+109
-15
lines changed

2 files changed

+109
-15
lines changed

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def user_filesystem(tmp_path):
6060
f.write(f"{str(input_dir.resolve() / 'good_data.txt')}\n")
6161

6262
home_config_data = {
63-
"wavelength": 0.25,
63+
"wavelength": 0.3,
6464
"owner_name": "home_username",
6565
"owner_email": "[email protected]",
6666
"owner_orcid": "home_orcid",

tests/test_tools.py

+108-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import re
34
from pathlib import Path
@@ -196,26 +197,119 @@ def test_set_output_directory_bad(user_filesystem):
196197
assert not Path(actual_args.output_directory).is_dir()
197198

198199

199-
def test_load_wavelength_from_config_file_with_home_conf_file():
200-
# C1: args provided, return args
201-
# C2: no args, local config exists, return local config
202-
# C3: no args or local config file, return global config
203-
return True
200+
@pytest.mark.parametrize(
201+
"inputs, expected",
202+
[
203+
# Test when only a home config file exists (no local config file),
204+
# expect to return args if wavelength or anode type is specified,
205+
# otherwise update args with values from the home config file.
206+
# C1: no args, expect to update arg values from home config
207+
([""], {"wavelength": 0.3, "anode_type": None}),
208+
# C2: wavelength provided, expect to return args unchanged
209+
(["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}),
210+
# C3: anode type provided, expect to return args unchanged
211+
(["--anode-type", "Mo"], {"wavelength": None, "anode_type": "Mo"}),
212+
# C4: both wavelength and anode type provided,
213+
# expect to return args unchanged
214+
(
215+
["--wavelength", "0.7", "--anode-type", "Mo"],
216+
{"wavelength": 0.7, "anode_type": "Mo"},
217+
),
218+
],
219+
)
220+
def test_load_wavelength_from_config_file_with_home_conf_file(
221+
mocker, user_filesystem, inputs, expected
222+
):
223+
cwd = Path(user_filesystem)
224+
home_dir = cwd / "home_dir"
225+
mocker.patch("pathlib.Path.home", lambda _: home_dir)
226+
os.chdir(cwd)
227+
228+
cli_inputs = ["2.5", "data.xy"] + inputs
229+
actual_args = get_args(cli_inputs)
230+
actual_args = _load_wavelength_from_config_file(actual_args)
231+
assert actual_args.wavelength == expected["wavelength"]
232+
assert actual_args.anode_type == expected["anode_type"]
233+
234+
235+
@pytest.mark.parametrize(
236+
"inputs, expected",
237+
[
238+
# Test when a local config file exists,
239+
# expect to return args if wavelength or anode type is specified,
240+
# otherwise update args with values from the home config file.
241+
# Results should be the same whether if the home config exists.
242+
# C1: no args, expect to update arg values from local config
243+
([""], {"wavelength": 0.6, "anode_type": None}),
244+
# C2: wavelength provided, expect to return args unchanged
245+
(["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}),
246+
# C3: anode type provided, expect to return args unchanged
247+
(["--anode-type", "Mo"], {"wavelength": None, "anode_type": "Mo"}),
248+
# C4: both wavelength and anode type provided,
249+
# expect to return args unchanged
250+
(
251+
["--wavelength", "0.7", "--anode-type", "Mo"],
252+
{"wavelength": 0.7, "anode_type": "Mo"},
253+
),
254+
],
255+
)
256+
def test_load_wavelength_from_config_file_with_local_conf_file(
257+
mocker, user_filesystem, inputs, expected
258+
):
259+
cwd = Path(user_filesystem)
260+
home_dir = cwd / "home_dir"
261+
mocker.patch("pathlib.Path.home", lambda _: home_dir)
262+
os.chdir(cwd)
263+
local_config_data = {"wavelength": 0.6}
264+
with open(cwd / "diffpyconfig.json", "w") as f:
265+
json.dump(local_config_data, f)
204266

267+
cli_inputs = ["2.5", "data.xy"] + inputs
268+
actual_args = get_args(cli_inputs)
269+
actual_args = _load_wavelength_from_config_file(actual_args)
270+
assert actual_args.wavelength == expected["wavelength"]
271+
assert actual_args.anode_type == expected["anode_type"]
205272

206-
def test_load_wavelength_from_config_file_with_local_conf_file():
207-
# C1: args provided, return args
208-
# C2: no args, return local config file
209-
# remove global config file to test again
210-
return True
273+
# remove home config file, expect the same results
274+
confile = home_dir / "diffpyconfig.json"
275+
os.remove(confile)
276+
assert actual_args.wavelength == expected["wavelength"]
277+
assert actual_args.anode_type == expected["anode_type"]
211278

212279

213-
def test_load_wavelength_from_config_file_without_conf_files(user_filesystem):
214-
# C1: args provided, return args
215-
cli_inputs = ["2.5", "data.xy", "-w", "0.25"]
280+
@pytest.mark.parametrize(
281+
"inputs, expected",
282+
[
283+
# Test when no config files exist,
284+
# expect to return args without modification.
285+
# C1: no args
286+
([""], {"wavelength": None, "anode_type": None}),
287+
# C1: wavelength provided
288+
(["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}),
289+
# C2: anode type provided
290+
(["--anode-type", "Mo"], {"wavelength": None, "anode_type": "Mo"}),
291+
# C4: both wavelength and anode type provided
292+
(
293+
["--wavelength", "0.7", "--anode-type", "Mo"],
294+
{"wavelength": 0.7, "anode_type": "Mo"},
295+
),
296+
],
297+
)
298+
def test_load_wavelength_from_config_file_without_conf_files(
299+
mocker, user_filesystem, inputs, expected
300+
):
301+
cwd = Path(user_filesystem)
302+
home_dir = cwd / "home_dir"
303+
mocker.patch("pathlib.Path.home", lambda _: home_dir)
304+
os.chdir(cwd)
305+
confile = home_dir / "diffpyconfig.json"
306+
os.remove(confile)
307+
308+
cli_inputs = ["2.5", "data.xy"] + inputs
216309
actual_args = get_args(cli_inputs)
217310
actual_args = _load_wavelength_from_config_file(actual_args)
218-
assert actual_args.wavelength == 0.25
311+
assert actual_args.wavelength == expected["wavelength"]
312+
assert actual_args.anode_type == expected["anode_type"]
219313

220314

221315
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)