|
| 1 | +import json |
1 | 2 | import os
|
2 | 3 | import re
|
3 | 4 | from pathlib import Path
|
|
11 | 12 | load_package_info,
|
12 | 13 | load_user_info,
|
13 | 14 | load_user_metadata,
|
| 15 | + load_wavelength_from_config_file, |
14 | 16 | preprocessing_args,
|
15 | 17 | set_input_lists,
|
16 | 18 | set_mud,
|
@@ -201,6 +203,131 @@ def test_set_output_directory_bad(user_filesystem):
|
201 | 203 | assert not Path(actual_args.output_directory).is_dir()
|
202 | 204 |
|
203 | 205 |
|
| 206 | +@pytest.mark.parametrize( |
| 207 | + "inputs, expected", |
| 208 | + [ |
| 209 | + # Test with only a home config file (no local config), |
| 210 | + # expect to return values directly from args |
| 211 | + # if either wavelength or anode type is specified, |
| 212 | + # otherwise update args with values from the home config file |
| 213 | + # (wavelength=0.3, no anode type). |
| 214 | + # This test only checks loading behavior, |
| 215 | + # not value validation (which is handled by `set_wavelength`). |
| 216 | + # C1: no args, expect to update arg values from home config |
| 217 | + ([], {"wavelength": 0.3, "anode_type": None}), |
| 218 | + # C2: wavelength provided, expect to return args unchanged |
| 219 | + (["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}), |
| 220 | + # C3: anode type provided, expect to return args unchanged |
| 221 | + (["--anode-type", "Mo"], {"wavelength": None, "anode_type": "Mo"}), |
| 222 | + # C4: both wavelength and anode type provided, |
| 223 | + # expect to return args unchanged |
| 224 | + ( |
| 225 | + ["--wavelength", "0.7", "--anode-type", "Mo"], |
| 226 | + {"wavelength": 0.7, "anode_type": "Mo"}, |
| 227 | + ), |
| 228 | + ], |
| 229 | +) |
| 230 | +def test_load_wavelength_from_config_file_with_home_conf_file( |
| 231 | + mocker, user_filesystem, inputs, expected |
| 232 | +): |
| 233 | + cwd = Path(user_filesystem) |
| 234 | + home_dir = cwd / "home_dir" |
| 235 | + mocker.patch("pathlib.Path.home", lambda _: home_dir) |
| 236 | + os.chdir(cwd) |
| 237 | + |
| 238 | + cli_inputs = ["data.xy", "--mud", "2.5"] + inputs |
| 239 | + actual_args = get_args(cli_inputs) |
| 240 | + actual_args = load_wavelength_from_config_file(actual_args) |
| 241 | + assert actual_args.wavelength == expected["wavelength"] |
| 242 | + assert actual_args.anode_type == expected["anode_type"] |
| 243 | + |
| 244 | + |
| 245 | +@pytest.mark.parametrize( |
| 246 | + "inputs, expected", |
| 247 | + [ |
| 248 | + # Test when a local config file exists, |
| 249 | + # expect to return values directly from args |
| 250 | + # if either wavelength or anode type is specified, |
| 251 | + # otherwise update args with values from the local config file |
| 252 | + # (wavelength=0.6, no anode type). |
| 253 | + # Results should be the same whether if the home config exists. |
| 254 | + # This test only checks loading behavior, |
| 255 | + # not value validation (which is handled by `set_wavelength`). |
| 256 | + # C1: no args, expect to update arg values from local config |
| 257 | + ([], {"wavelength": 0.6, "anode_type": None}), |
| 258 | + # C2: wavelength provided, expect to return args unchanged |
| 259 | + (["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}), |
| 260 | + # C3: anode type provided, expect to return args unchanged |
| 261 | + (["--anode-type", "Mo"], {"wavelength": None, "anode_type": "Mo"}), |
| 262 | + # C4: both wavelength and anode type provided, |
| 263 | + # expect to return args unchanged |
| 264 | + ( |
| 265 | + ["--wavelength", "0.7", "--anode-type", "Mo"], |
| 266 | + {"wavelength": 0.7, "anode_type": "Mo"}, |
| 267 | + ), |
| 268 | + ], |
| 269 | +) |
| 270 | +def test_load_wavelength_from_config_file_with_local_conf_file( |
| 271 | + mocker, user_filesystem, inputs, expected |
| 272 | +): |
| 273 | + cwd = Path(user_filesystem) |
| 274 | + home_dir = cwd / "home_dir" |
| 275 | + mocker.patch("pathlib.Path.home", lambda _: home_dir) |
| 276 | + os.chdir(cwd) |
| 277 | + local_config_data = {"wavelength": 0.6} |
| 278 | + with open(cwd / "diffpyconfig.json", "w") as f: |
| 279 | + json.dump(local_config_data, f) |
| 280 | + |
| 281 | + cli_inputs = ["data.xy", "--mud", "2.5"] + inputs |
| 282 | + actual_args = get_args(cli_inputs) |
| 283 | + actual_args = load_wavelength_from_config_file(actual_args) |
| 284 | + assert actual_args.wavelength == expected["wavelength"] |
| 285 | + assert actual_args.anode_type == expected["anode_type"] |
| 286 | + |
| 287 | + # remove home config file, expect the same results |
| 288 | + confile = home_dir / "diffpyconfig.json" |
| 289 | + os.remove(confile) |
| 290 | + assert actual_args.wavelength == expected["wavelength"] |
| 291 | + assert actual_args.anode_type == expected["anode_type"] |
| 292 | + |
| 293 | + |
| 294 | +@pytest.mark.parametrize( |
| 295 | + "inputs, expected", |
| 296 | + [ |
| 297 | + # Test when no config files exist, |
| 298 | + # expect to return args without modification. |
| 299 | + # This test only checks loading behavior, |
| 300 | + # not value validation (which is handled by `set_wavelength`). |
| 301 | + # C1: no args |
| 302 | + ([], {"wavelength": None, "anode_type": None}), |
| 303 | + # C1: wavelength provided |
| 304 | + (["--wavelength", "0.25"], {"wavelength": 0.25, "anode_type": None}), |
| 305 | + # C2: anode type provided |
| 306 | + (["--anode-type", "Mo"], {"wavelength": None, "anode_type": "Mo"}), |
| 307 | + # C4: both wavelength and anode type provided |
| 308 | + ( |
| 309 | + ["--wavelength", "0.7", "--anode-type", "Mo"], |
| 310 | + {"wavelength": 0.7, "anode_type": "Mo"}, |
| 311 | + ), |
| 312 | + ], |
| 313 | +) |
| 314 | +def test_load_wavelength_from_config_file_without_conf_files( |
| 315 | + mocker, user_filesystem, inputs, expected |
| 316 | +): |
| 317 | + cwd = Path(user_filesystem) |
| 318 | + home_dir = cwd / "home_dir" |
| 319 | + mocker.patch("pathlib.Path.home", lambda _: home_dir) |
| 320 | + os.chdir(cwd) |
| 321 | + confile = home_dir / "diffpyconfig.json" |
| 322 | + os.remove(confile) |
| 323 | + |
| 324 | + cli_inputs = ["data.xy", "--mud", "2.5"] + inputs |
| 325 | + actual_args = get_args(cli_inputs) |
| 326 | + actual_args = load_wavelength_from_config_file(actual_args) |
| 327 | + assert actual_args.wavelength == expected["wavelength"] |
| 328 | + assert actual_args.anode_type == expected["anode_type"] |
| 329 | + |
| 330 | + |
204 | 331 | @pytest.mark.parametrize(
|
205 | 332 | "inputs, expected",
|
206 | 333 | [
|
@@ -279,13 +406,13 @@ def test_set_wavelength(inputs, expected):
|
279 | 406 | ( # C3: invalid anode type
|
280 | 407 | # expect error asking to specify a valid anode type
|
281 | 408 | ["--anode-type", "invalid"],
|
282 |
| - f"Anode type not recognized. " |
| 409 | + f"Anode type 'invalid' not recognized. " |
283 | 410 | f"Please rerun specifying an anode_type from {*known_sources, }.",
|
284 | 411 | ),
|
285 | 412 | ( # C4: invalid wavelength
|
286 | 413 | # expect error asking to specify a valid wavelength or anode type
|
287 |
| - ["--wavelength", "0"], |
288 |
| - "No valid wavelength. " |
| 414 | + ["--wavelength", "-0.2"], |
| 415 | + "Wavelength = -0.2 is not valid. " |
289 | 416 | "Please rerun specifying a known anode_type "
|
290 | 417 | "or a positive wavelength.",
|
291 | 418 | ),
|
|
0 commit comments