|
4 | 4 | import pytest
|
5 | 5 |
|
6 | 6 | from diffpy.labpdfproc.labpdfprocapp import get_args
|
7 |
| -from diffpy.labpdfproc.tools import known_sources, set_output_directory, set_wavelength |
| 7 | +from diffpy.labpdfproc.tools import known_sources, load_user_metadata, set_output_directory, set_wavelength |
8 | 8 |
|
9 | 9 | params1 = [
|
10 | 10 | ([], ["."]),
|
|
16 | 16 |
|
17 | 17 | @pytest.mark.parametrize("inputs, expected", params1)
|
18 | 18 | def test_set_output_directory(inputs, expected, user_filesystem):
|
19 |
| - tmp_dir = user_filesystem |
20 |
| - expected_output_directory = tmp_dir / expected[0] |
21 |
| - |
| 19 | + expected_output_directory = Path(user_filesystem) / expected[0] |
22 | 20 | cli_inputs = ["2.5"] + inputs
|
23 | 21 | actual_args = get_args(cli_inputs)
|
24 | 22 | actual_args.output_directory = set_output_directory(actual_args)
|
@@ -75,3 +73,63 @@ def test_set_wavelength_bad(inputs, msg):
|
75 | 73 | actual_args = get_args(cli_inputs)
|
76 | 74 | with pytest.raises(ValueError, match=re.escape(msg[0])):
|
77 | 75 | actual_args.wavelength = set_wavelength(actual_args)
|
| 76 | + |
| 77 | + |
| 78 | +params5 = [ |
| 79 | + ([], []), |
| 80 | + ( |
| 81 | + ["--user-metadata", "facility=NSLS II", "beamline=28ID-2", "favorite color=blue"], |
| 82 | + [["facility", "NSLS II"], ["beamline", "28ID-2"], ["favorite color", "blue"]], |
| 83 | + ), |
| 84 | + (["--user-metadata", "x=y=z"], [["x", "y=z"]]), |
| 85 | +] |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize("inputs, expected", params5) |
| 89 | +def test_load_user_metadata(inputs, expected): |
| 90 | + expected_args = get_args(["2.5"]) |
| 91 | + for expected_pair in expected: |
| 92 | + setattr(expected_args, expected_pair[0], expected_pair[1]) |
| 93 | + delattr(expected_args, "user_metadata") |
| 94 | + |
| 95 | + cli_inputs = ["2.5"] + inputs |
| 96 | + actual_args = get_args(cli_inputs) |
| 97 | + actual_args = load_user_metadata(actual_args) |
| 98 | + assert actual_args == expected_args |
| 99 | + |
| 100 | + |
| 101 | +params6 = [ |
| 102 | + ( |
| 103 | + ["--user-metadata", "facility=", "NSLS II"], |
| 104 | + [ |
| 105 | + "Please provide key-value pairs in the format key=value. " |
| 106 | + "For more information, use `labpdfproc --help.`" |
| 107 | + ], |
| 108 | + ), |
| 109 | + ( |
| 110 | + ["--user-metadata", "favorite", "color=blue"], |
| 111 | + "Please provide key-value pairs in the format key=value. " |
| 112 | + "For more information, use `labpdfproc --help.`", |
| 113 | + ), |
| 114 | + ( |
| 115 | + ["--user-metadata", "beamline", "=", "28ID-2"], |
| 116 | + "Please provide key-value pairs in the format key=value. " |
| 117 | + "For more information, use `labpdfproc --help.`", |
| 118 | + ), |
| 119 | + ( |
| 120 | + ["--user-metadata", "facility=NSLS II", "facility=NSLS III"], |
| 121 | + "Please do not specify repeated keys: facility. ", |
| 122 | + ), |
| 123 | + ( |
| 124 | + ["--user-metadata", "wavelength=2"], |
| 125 | + "wavelength is a reserved name. Please rerun using a different key name. ", |
| 126 | + ), |
| 127 | +] |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize("inputs, msg", params6) |
| 131 | +def test_load_user_metadata_bad(inputs, msg): |
| 132 | + cli_inputs = ["2.5"] + inputs |
| 133 | + actual_args = get_args(cli_inputs) |
| 134 | + with pytest.raises(ValueError, match=msg[0]): |
| 135 | + actual_args = load_user_metadata(actual_args) |
0 commit comments