Skip to content

Commit 3c49a18

Browse files
added test cases for UC1-4 and made input a required argument
1 parent d164dba commit 3c49a18

File tree

3 files changed

+51
-22
lines changed

3 files changed

+51
-22
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
def get_args(override_cli_inputs=None):
1818
p = ArgumentParser()
1919
p.add_argument("mud", help="Value of mu*D for your " "sample. Required.", type=float)
20-
p.add_argument("-i", "--input-file", help="The filename of the " "datafile to load.")
20+
p.add_argument("input", help="The filename or directory of the " "datafile to load.")
2121
p.add_argument(
2222
"-a",
2323
"--anode-type",

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,36 @@
1313
)
1414
from diffpy.utils.parsers.loaddata import loadData
1515

16-
params1 = [
16+
# Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
17+
params_input = [
18+
(["good_data.chi"], [".", "good_data.chi"]),
19+
(["input_dir/good_data.chi"], ["input_dir", "good_data.chi"]),
20+
(["./input_dir/good_data.chi"], ["input_dir", "good_data.chi"]),
1721
(
18-
["--input-file", "."],
22+
["."],
1923
[
2024
".",
2125
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
2226
],
2327
),
24-
(["--input-file", "good_data.chi"], [".", "good_data.chi"]),
25-
(["--input-file", "input_dir/unreadable_file.txt"], ["input_dir", "unreadable_file.txt"]),
26-
# ([Path.cwd()], [Path.cwd()]),
28+
(
29+
["./input_dir"],
30+
[
31+
"input_dir",
32+
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
33+
],
34+
),
35+
(
36+
["input_dir"],
37+
[
38+
"input_dir",
39+
["good_data.chi", "good_data.xy", "good_data.txt", "unreadable_file.txt", "binary.pkl"],
40+
],
41+
),
2742
]
2843

2944

30-
@pytest.mark.parametrize("inputs, expected", params1)
45+
@pytest.mark.parametrize("inputs, expected", params_input)
3146
def test_set_input_files(inputs, expected, user_filesystem):
3247
expected_input_directory = Path(user_filesystem) / expected[0]
3348
expected_input_files = expected[1]
@@ -39,6 +54,21 @@ def test_set_input_files(inputs, expected, user_filesystem):
3954
assert set(actual_args.input_file) == set(expected_input_files)
4055

4156

57+
params_input_bad = [
58+
(["new_file.xy"]),
59+
(["./input_dir/new_file.xy"]),
60+
(["./new_dir"]),
61+
]
62+
63+
64+
@pytest.mark.parametrize("inputs", params_input_bad)
65+
def test_set_input_files_bad(inputs, user_filesystem):
66+
cli_inputs = ["2.5"] + inputs
67+
actual_args = get_args(cli_inputs)
68+
with pytest.raises(ValueError):
69+
actual_args = set_input_files(actual_args)
70+
71+
4272
def test_loadData_with_input_files(user_filesystem):
4373
xarray_chi, yarray_chi = loadData("good_data.chi", unpack=True)
4474
xarray_xy, yarray_xy = loadData("good_data.xy", unpack=True)
@@ -60,7 +90,7 @@ def test_loadData_with_input_files(user_filesystem):
6090
@pytest.mark.parametrize("inputs, expected", params1)
6191
def test_set_output_directory(inputs, expected, user_filesystem):
6292
expected_output_directory = Path(user_filesystem) / expected[0]
63-
cli_inputs = ["2.5"] + inputs
93+
cli_inputs = ["2.5", "data.xy"] + inputs
6494
actual_args = get_args(cli_inputs)
6595
actual_args.output_directory = set_output_directory(actual_args)
6696
assert actual_args.output_directory == expected_output_directory
@@ -69,7 +99,7 @@ def test_set_output_directory(inputs, expected, user_filesystem):
6999

70100

71101
def test_set_output_directory_bad(user_filesystem):
72-
cli_inputs = ["2.5", "--output-directory", "good_data.chi"]
102+
cli_inputs = ["2.5", "data.xy", "--output-directory", "good_data.chi"]
73103
actual_args = get_args(cli_inputs)
74104
with pytest.raises(FileExistsError):
75105
actual_args.output_directory = set_output_directory(actual_args)
@@ -88,7 +118,7 @@ def test_set_output_directory_bad(user_filesystem):
88118
@pytest.mark.parametrize("inputs, expected", params2)
89119
def test_set_wavelength(inputs, expected):
90120
expected_wavelength = expected[0]
91-
cli_inputs = ["2.5"] + inputs
121+
cli_inputs = ["2.5", "data.xy"] + inputs
92122
actual_args = get_args(cli_inputs)
93123
actual_args.wavelength = set_wavelength(actual_args)
94124
assert actual_args.wavelength == expected_wavelength
@@ -112,7 +142,7 @@ def test_set_wavelength(inputs, expected):
112142

113143
@pytest.mark.parametrize("inputs, msg", params3)
114144
def test_set_wavelength_bad(inputs, msg):
115-
cli_inputs = ["2.5"] + inputs
145+
cli_inputs = ["2.5", "data.xy"] + inputs
116146
actual_args = get_args(cli_inputs)
117147
with pytest.raises(ValueError, match=re.escape(msg[0])):
118148
actual_args.wavelength = set_wavelength(actual_args)
@@ -130,12 +160,12 @@ def test_set_wavelength_bad(inputs, msg):
130160

131161
@pytest.mark.parametrize("inputs, expected", params5)
132162
def test_load_user_metadata(inputs, expected):
133-
expected_args = get_args(["2.5"])
163+
expected_args = get_args(["2.5", "data.xy"])
134164
for expected_pair in expected:
135165
setattr(expected_args, expected_pair[0], expected_pair[1])
136166
delattr(expected_args, "user_metadata")
137167

138-
cli_inputs = ["2.5"] + inputs
168+
cli_inputs = ["2.5", "data.xy"] + inputs
139169
actual_args = get_args(cli_inputs)
140170
actual_args = load_user_metadata(actual_args)
141171
assert actual_args == expected_args
@@ -172,7 +202,7 @@ def test_load_user_metadata(inputs, expected):
172202

173203
@pytest.mark.parametrize("inputs, msg", params6)
174204
def test_load_user_metadata_bad(inputs, msg):
175-
cli_inputs = ["2.5"] + inputs
205+
cli_inputs = ["2.5", "data.xy"] + inputs
176206
actual_args = get_args(cli_inputs)
177207
with pytest.raises(ValueError, match=msg[0]):
178208
actual_args = load_user_metadata(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ def set_input_files(args):
2020
args argparse.Namespace
2121
2222
"""
23-
if not args.input_file or not Path(args.input_file).exists():
23+
if not Path(args.input).exists():
2424
raise ValueError("Please specify valid input file or directory.")
2525

26-
if not Path(args.input_file).is_dir():
27-
input_dir = Path.cwd() / Path(args.input_file).parent
28-
input_file_name = Path(args.input_file).name
29-
args.input_file = input_file_name
26+
if not Path(args.input).is_dir():
27+
input_dir = Path.cwd() / Path(args.input).parent
28+
input_file_name = Path(args.input).name
3029
else:
31-
input_dir = Path(args.input_file).resolve()
30+
input_dir = Path(args.input).resolve()
3231
input_files = [file for file in glob.glob(str(input_dir) + "/*", recursive=True) if os.path.isfile(file)]
33-
input_file_names = [os.path.basename(input_file_path) for input_file_path in input_files]
34-
args.input_file = input_file_names
32+
input_file_name = [os.path.basename(input_file_path) for input_file_path in input_files]
3533
setattr(args, "input_directory", input_dir)
34+
setattr(args, "input_file", input_file_name)
3635
return args
3736

3837

0 commit comments

Comments
 (0)