Skip to content

Commit 8fcd034

Browse files
authored
Merge pull request #49 from yucongalicechen/get_args_for_tests
changed output directory and wavelength tests to use get_args
2 parents 1393a45 + 24e8288 commit 8fcd034

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def get_args(override_cli_inputs=None):
7676

7777
def main():
7878
args = get_args()
79-
args = load_user_metadata(args)
8079
args.output_directory = set_output_directory(args)
8180
args.wavelength = set_wavelength(args)
81+
args = load_user_metadata(args)
8282

8383
filepath = Path(args.input_file)
8484
outfilestem = filepath.stem + "_corrected"

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import argparse
2-
import os
31
import re
42
from pathlib import Path
53

@@ -9,72 +7,70 @@
97
from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength
108

119
params1 = [
12-
([None], ["."]),
13-
(["."], ["."]),
14-
(["new_dir"], ["new_dir"]),
15-
(["existing_dir"], ["existing_dir"]),
10+
([], ["."]),
11+
(["--output-directory", "."], ["."]),
12+
(["--output-directory", "new_dir"], ["new_dir"]),
13+
(["--output-directory", "input_dir"], ["input_dir"]),
1614
]
1715

1816

1917
@pytest.mark.parametrize("inputs, expected", params1)
20-
def test_set_output_directory(inputs, expected, tmp_path):
21-
directory = Path(tmp_path)
22-
os.chdir(directory)
23-
24-
existing_dir = Path(tmp_path).resolve() / "existing_dir"
25-
existing_dir.mkdir(parents=True, exist_ok=True)
26-
27-
expected_output_directory = Path(tmp_path).resolve() / expected[0]
28-
actual_args = argparse.Namespace(output_directory=inputs[0])
18+
def test_set_output_directory(inputs, expected, user_filesystem):
19+
expected_output_directory = Path(user_filesystem) / expected[0]
20+
cli_inputs = ["2.5"] + inputs
21+
actual_args = get_args(cli_inputs)
2922
actual_args.output_directory = set_output_directory(actual_args)
3023
assert actual_args.output_directory == expected_output_directory
3124
assert Path(actual_args.output_directory).exists()
3225
assert Path(actual_args.output_directory).is_dir()
3326

3427

35-
def test_set_output_directory_bad(tmp_path):
36-
directory = Path(tmp_path)
37-
os.chdir(directory)
38-
39-
existing_file = Path(tmp_path).resolve() / "existing_file.py"
40-
existing_file.touch()
41-
42-
actual_args = argparse.Namespace(output_directory="existing_file.py")
28+
def test_set_output_directory_bad(user_filesystem):
29+
cli_inputs = ["2.5", "--output-directory", "good_data.chi"]
30+
actual_args = get_args(cli_inputs)
4331
with pytest.raises(FileExistsError):
4432
actual_args.output_directory = set_output_directory(actual_args)
4533
assert Path(actual_args.output_directory).exists()
4634
assert not Path(actual_args.output_directory).is_dir()
4735

4836

4937
params2 = [
50-
([None, None], [0.71]),
51-
([None, "Ag"], [0.59]),
52-
([0.25, "Ag"], [0.25]),
53-
([0.25, None], [0.25]),
38+
([], [0.71]),
39+
(["--anode-type", "Ag"], [0.59]),
40+
(["--wavelength", "0.25"], [0.25]),
41+
(["--wavelength", "0.25", "--anode-type", "Ag"], [0.25]),
5442
]
5543

5644

5745
@pytest.mark.parametrize("inputs, expected", params2)
5846
def test_set_wavelength(inputs, expected):
5947
expected_wavelength = expected[0]
60-
actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1])
61-
actual_wavelength = set_wavelength(actual_args)
62-
assert actual_wavelength == expected_wavelength
48+
cli_inputs = ["2.5"] + inputs
49+
actual_args = get_args(cli_inputs)
50+
actual_args.wavelength = set_wavelength(actual_args)
51+
assert actual_args.wavelength == expected_wavelength
6352

6453

6554
params3 = [
6655
(
67-
[None, "invalid"],
56+
["--anode-type", "invalid"],
6857
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."],
6958
),
70-
([0, None], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."]),
71-
([-1, "Mo"], ["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."]),
59+
(
60+
["--wavelength", "0"],
61+
["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."],
62+
),
63+
(
64+
["--wavelength", "-1", "--anode-type", "Mo"],
65+
["No valid wavelength. Please rerun specifying a known anode_type or a positive wavelength."],
66+
),
7267
]
7368

7469

7570
@pytest.mark.parametrize("inputs, msg", params3)
7671
def test_set_wavelength_bad(inputs, msg):
77-
actual_args = argparse.Namespace(wavelength=inputs[0], anode_type=inputs[1])
72+
cli_inputs = ["2.5"] + inputs
73+
actual_args = get_args(cli_inputs)
7874
with pytest.raises(ValueError, match=re.escape(msg[0])):
7975
actual_args.wavelength = set_wavelength(actual_args)
8076

0 commit comments

Comments
 (0)