|
| 1 | +import re |
1 | 2 | from pathlib import Path
|
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | import pytest
|
5 | 6 | from freezegun import freeze_time
|
6 | 7 |
|
7 |
| -from diffpy.utils.diffraction_objects import DiffractionObject |
| 8 | +from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject |
8 | 9 | from diffpy.utils.transforms import wavelength_warning_emsg
|
9 | 10 |
|
10 | 11 |
|
@@ -212,21 +213,41 @@ def _test_valid_diffraction_objects(actual_diffraction_object, function, expecte
|
212 | 213 |
|
213 | 214 |
|
214 | 215 | def test_get_angle_index():
|
215 |
| - test = DiffractionObject() |
216 |
| - test.angles = np.array([10, 20, 30, 40, 50, 60]) |
217 |
| - actual_angle_index = test.get_angle_index(angle=10) |
218 |
| - assert actual_angle_index == 0 |
| 216 | + test = DiffractionObject( |
| 217 | + wavelength=0.71, xarray=np.array([30, 60, 90]), yarray=np.array([1, 2, 3]), xtype="tth" |
| 218 | + ) |
| 219 | + actual_index = test.get_array_index(xtype="tth", value=30) |
| 220 | + assert actual_index == 0 |
| 221 | + |
| 222 | + |
| 223 | +params_index_bad = [ |
| 224 | + # UC1: empty array |
| 225 | + ( |
| 226 | + [0.71, np.array([]), np.array([]), "tth", "tth", 10], |
| 227 | + [IndexError, "WARNING: no matching value 10 found in the tth array."], |
| 228 | + ), |
| 229 | + # UC2: invalid xtype |
| 230 | + ( |
| 231 | + [None, np.array([]), np.array([]), "tth", "invalid", 10], |
| 232 | + [ |
| 233 | + ValueError, |
| 234 | + f"WARNING: I don't know how to handle the xtype, 'invalid'. " |
| 235 | + f"Please rerun specifying an xtype from {*XQUANTITIES, }", |
| 236 | + ], |
| 237 | + ), |
| 238 | + # UC3: pre-defined array with non-matching value |
| 239 | + ( |
| 240 | + [0.71, np.array([30, 60, 90]), np.array([1, 2, 3]), "tth", "q", 30], |
| 241 | + [IndexError, "WARNING: no matching value 30 found in the q array."], |
| 242 | + ), |
| 243 | +] |
219 | 244 |
|
220 | 245 |
|
221 |
| -def test_get_angle_index_bad(): |
222 |
| - test = DiffractionObject() |
223 |
| - # empty angles list |
224 |
| - with pytest.raises(IndexError, match="WARNING: no angle 11 found in angles list."): |
225 |
| - test.get_angle_index(angle=11) |
226 |
| - # pre-defined angles list |
227 |
| - test.angles = np.array([10, 20, 30, 40, 50, 60]) |
228 |
| - with pytest.raises(IndexError, match="WARNING: no angle 11 found in angles list."): |
229 |
| - test.get_angle_index(angle=11) |
| 246 | +@pytest.mark.parametrize("inputs, expected", params_index_bad) |
| 247 | +def test_get_angle_index_bad(inputs, expected): |
| 248 | + test = DiffractionObject(wavelength=inputs[0], xarray=inputs[1], yarray=inputs[2], xtype=inputs[3]) |
| 249 | + with pytest.raises(expected[0], match=re.escape(expected[1])): |
| 250 | + test.get_array_index(xtype=inputs[4], value=inputs[5]) |
230 | 251 |
|
231 | 252 |
|
232 | 253 | def test_dump(tmp_path, mocker):
|
|
0 commit comments