Skip to content

Commit c7fb8ec

Browse files
add test functions for overloading mud with simulated data added in conftest
1 parent 3b08462 commit c7fb8ec

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/diffpy/labpdfproc/tests/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def user_filesystem(tmp_path):
1111
input_dir.mkdir(parents=True, exist_ok=True)
1212
home_dir = base_dir / "home_dir"
1313
home_dir.mkdir(parents=True, exist_ok=True)
14+
test_dir = base_dir / "test_dir"
15+
test_dir.mkdir(parents=True, exist_ok=True)
1416

1517
chi_data = "dataformat = twotheta\n mode = xray\n # chi_Q chi_I\n 1 2\n 3 4\n 5 6\n 7 8\n"
1618
xy_data = "1 2\n 3 4\n 5 6\n 7 8"
@@ -51,4 +53,19 @@ def user_filesystem(tmp_path):
5153
with open(home_dir / "diffpyconfig.json", "w") as f:
5254
json.dump(home_config_data, f)
5355

56+
z_scan_data = """
57+
-1.00000000 100000.00000000
58+
-0.77777778 100000.00000000
59+
-0.55555556 100000.00000000
60+
-0.33333333 10687.79256604
61+
-0.11111111 5366.53289631
62+
0.11111111 5366.53289631
63+
0.33333333 10687.79256604
64+
0.55555556 100000.00000000
65+
0.77777778 100000.00000000
66+
1.00000000 100000.00000000
67+
"""
68+
with open(test_dir / "testfile.xy", "w") as f:
69+
f.write(z_scan_data)
70+
5471
yield tmp_path

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
load_user_metadata,
1414
preprocessing_args,
1515
set_input_lists,
16+
set_mud,
1617
set_output_directory,
1718
set_wavelength,
1819
)
@@ -188,6 +189,32 @@ def test_set_wavelength_bad(inputs, msg):
188189
actual_args = set_wavelength(actual_args)
189190

190191

192+
def test_set_mud(user_filesystem):
193+
cli_inputs = ["2.5", "data.xy"]
194+
actual_args = get_args(cli_inputs)
195+
actual_args = set_mud(actual_args)
196+
assert actual_args.mud == pytest.approx(2.5, rel=0.1, abs=0.1)
197+
assert actual_args.z_scan_file is None
198+
199+
cwd = Path(user_filesystem)
200+
test_dir = cwd / "test_dir"
201+
os.chdir(cwd)
202+
inputs = ["--z-scan-file", "test_dir/testfile.xy"]
203+
expected = [3, str(test_dir / "testfile.xy")]
204+
cli_inputs = ["2.5", "data.xy"] + inputs
205+
actual_args = get_args(cli_inputs)
206+
actual_args = set_mud(actual_args)
207+
assert actual_args.mud == pytest.approx(expected[0], rel=0.1, abs=0.1)
208+
assert actual_args.z_scan_file == expected[1]
209+
210+
211+
def test_set_mud_bad():
212+
cli_inputs = ["2.5", "data.xy", "--z-scan-file", "invalid file"]
213+
actual_args = get_args(cli_inputs)
214+
with pytest.raises(FileNotFoundError, match="Cannot find invalid file. Please specify a valid file path."):
215+
actual_args = set_mud(actual_args)
216+
217+
191218
params5 = [
192219
([], []),
193220
(
@@ -317,5 +344,6 @@ def test_load_metadata(mocker, user_filesystem):
317344
"username": "cli_username",
318345
"email": "[email protected]",
319346
"package_info": {"diffpy.labpdfproc": "1.2.3", "diffpy.utils": "3.3.0"},
347+
"z_scan_file": None,
320348
}
321349
assert actual_metadata == expected_metadata

src/diffpy/labpdfproc/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def set_mud(args):
152152
filepath = Path(args.z_scan_file).resolve()
153153
if not filepath.is_file():
154154
raise FileNotFoundError(f"Cannot find {args.z_scan_file}. Please specify a valid file path.")
155-
args.z_scan_file = filepath
155+
args.z_scan_file = str(filepath)
156156
args.mud = compute_mud(filepath)
157157
return args
158158

0 commit comments

Comments
 (0)