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