Skip to content

Commit c580d82

Browse files
committed
Add test for mismatch for y and xarray lenght
1 parent 2a7428c commit c580d82

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/diffpy/utils/diffraction_objects.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ def insert_scattering_quantity(
346346
Nothing. Updates the object in place.
347347
348348
"""
349+
350+
# Check xarray and yarray have the same length
351+
if len(xarray) != len(yarray):
352+
raise ValueError(
353+
"'xarray' and 'yarray' must have the same length. "
354+
"Please re-initialize 'DiffractionObject' or re-run the method 'insert_scattering_quantity' "
355+
"with 'xarray' and 'yarray' of identical length."
356+
)
357+
349358
self._set_xarrays(xarray, xtype)
350359
self._all_arrays[:, 0] = yarray
351360
self._input_xtype = xtype
@@ -360,14 +369,6 @@ def insert_scattering_quantity(
360369
if wavelength is not None:
361370
self.wavelength = wavelength
362371

363-
# Check xarray and yarray have the same length
364-
if len(xarray) != len(yarray):
365-
raise ValueError(
366-
"`xarray` and `yarray` must have the same length. "
367-
"Please re-initialize `DiffractionObject` or re-run the method `insert_scattering_quantity` "
368-
"with `xarray` and `yarray` of identical length."
369-
)
370-
371372
# Check xtype is valid. An empty string is the default value.
372373
if xtype != "":
373374
if xtype not in XQUANTITIES:

tests/test_diffraction_objects.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,16 @@ def test_all_array_setter():
411411
actual_do.all_arrays = np.empty((4, 4))
412412

413413

414+
def test_xarray_yarray_length_mismatch():
415+
with pytest.raises(
416+
ValueError,
417+
match="'xarray' and 'yarray' must have the same length. "
418+
"Please re-initialize 'DiffractionObject' or re-run the method 'insert_scattering_quantity' "
419+
"with 'xarray' and 'yarray' of identical length",
420+
):
421+
DiffractionObject(xarray=np.array([1.0, 2.0]), yarray=np.array([0.0, 0.0, 0.0]))
422+
423+
414424
def test_input_xtype_getter():
415425
do = DiffractionObject(xtype="tth")
416426
assert do.input_xtype == "tth"

0 commit comments

Comments
 (0)