Skip to content

Commit 846c72a

Browse files
committed
test: combine do with another do or scalar value tests
1 parent 11c4166 commit 846c72a

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

tests/conftest.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def do_minimal_tth():
4747
return DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")
4848

4949

50+
@pytest.fixture
51+
def do_minimal_d():
52+
# Create an instance of DiffractionObject with non-empty xarray, yarray, and wavelength values
53+
return DiffractionObject(wavelength=1.54, xarray=np.array([1, 2]), yarray=np.array([1, 2]), xtype="d")
54+
55+
5056
@pytest.fixture
5157
def wavelength_warning_msg():
5258
return (
@@ -70,13 +76,13 @@ def invalid_add_type_error_msg():
7076
return (
7177
"You may only add a DiffractionObject with another DiffractionObject or a scalar value. "
7278
"Please rerun by adding another DiffractionObject instance or a scalar value. "
73-
"e.g., my_do_1 + my_do_2 or my_do + 10"
79+
"e.g., my_do_1 + my_do_2 or my_do + 10 or 10 + my_do"
7480
)
7581

7682

7783
@pytest.fixture
78-
def x_grid_size_mismatch_error_msg():
84+
def y_grid_size_mismatch_error_msg():
7985
return (
80-
"The two objects have different x-array lengths. "
81-
"Please ensure the length of the x-value during initialization is identical."
86+
"The two objects have different y-array lengths. "
87+
"Please ensure the length of the y-value during initialization is identical."
8288
)

tests/test_diffraction_objects.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -715,47 +715,57 @@ def test_copy_object(do_minimal):
715715
@pytest.mark.parametrize(
716716
"starting_all_arrays, scalar_to_add, expected_all_arrays",
717717
[
718-
# Test scalar addition to xarray values (q, tth, d) and expect no change to yarray values
719-
( # C1: Add integer of 5, expect xarray to increase by by 5
718+
# Test scalar addition to yarray values (intensity) and expect no change to xarrays (q, tth, d)
719+
( # C1: Add integer of 5, expect yarray to increase by by 5
720720
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]]),
721721
5,
722-
np.array([[1.0, 5.51763809, 35.0, 17.13818192], [2.0, 6.0, 65.0, 11.28318531]]),
722+
np.array([[6.0, 0.51763809, 30.0, 12.13818192], [7.0, 1.0, 60.0, 6.28318531]]),
723723
),
724-
( # C2: Add float of 5.1, expect xarray to be added by 5.1
724+
( # C2: Add float of 5.1, expect yarray to be added by 5.1
725725
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]]),
726726
5.1,
727-
np.array([[1.0, 5.61763809, 35.1, 17.23818192], [2.0, 6.1, 65.1, 11.38318531]]),
727+
np.array([[6.1, 0.51763809, 30.0, 12.13818192], [7.1, 1.0, 60.0, 6.28318531]]),
728728
),
729729
],
730730
)
731731
def test_addition_operator_by_scalar(starting_all_arrays, scalar_to_add, expected_all_arrays, do_minimal_tth):
732732
do = do_minimal_tth
733733
assert np.allclose(do.all_arrays, starting_all_arrays)
734-
do_sum_RHS = do + scalar_to_add
735-
do_sum_LHS = scalar_to_add + do
736-
assert np.allclose(do_sum_RHS.all_arrays, expected_all_arrays)
737-
assert np.allclose(do_sum_LHS.all_arrays, expected_all_arrays)
734+
do_scalar_right_sum = do + scalar_to_add
735+
assert np.allclose(do_scalar_right_sum.all_arrays, expected_all_arrays)
736+
do_scalar_left_sum = scalar_to_add + do
737+
assert np.allclose(do_scalar_left_sum.all_arrays, expected_all_arrays)
738738

739739

740740
@pytest.mark.parametrize(
741-
"LHS_all_arrays, RHS_all_arrays, expected_all_arrays_sum",
741+
"do_1_all_arrays, "
742+
"do_2_all_arrays, "
743+
"expected_do_1_all_arrays_with_y_summed, "
744+
"expected_do_2_all_arrays_with_y_summed",
742745
[
743746
# Test addition of two DO objects, expect combined xarray values (q, tth, d) and no change to yarray
744-
( # C1: Add two DO objects with identical xarray values, expect sum of xarray values
747+
( # C1: Add two DO objects, expect sum of yarray values
745748
(np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]]),),
746-
(np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]]),),
747-
np.array([[1.0, 1.03527618, 60.0, 24.27636384], [2.0, 2.0, 120.0, 12.56637061]]),
749+
(np.array([[1.0, 6.28318531, 100.70777771, 1], [2.0, 3.14159265, 45.28748053, 2.0]]),),
750+
(np.array([[2.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),),
751+
(np.array([[2.0, 6.28318531, 100.70777771, 1], [4.0, 3.14159265, 45.28748053, 2.0]]),),
748752
),
749753
],
750754
)
751-
def test_addition_operator_by_another_do(LHS_all_arrays, RHS_all_arrays, expected_all_arrays_sum, do_minimal_tth):
752-
assert np.allclose(do_minimal_tth.all_arrays, LHS_all_arrays)
753-
do_LHS = do_minimal_tth
754-
do_RHS = do_minimal_tth
755-
do_sum = do_LHS + do_RHS
756-
assert np.allclose(do_LHS.all_arrays, LHS_all_arrays)
757-
assert np.allclose(do_RHS.all_arrays, RHS_all_arrays)
758-
assert np.allclose(do_sum.all_arrays, expected_all_arrays_sum)
755+
def test_addition_operator_by_another_do(
756+
do_1_all_arrays,
757+
do_2_all_arrays,
758+
expected_do_1_all_arrays_with_y_summed,
759+
expected_do_2_all_arrays_with_y_summed,
760+
do_minimal_tth,
761+
do_minimal_d,
762+
):
763+
do_1 = do_minimal_tth
764+
assert np.allclose(do_1.all_arrays, do_1_all_arrays)
765+
do_2 = do_minimal_d
766+
assert np.allclose(do_2.all_arrays, do_2_all_arrays)
767+
assert np.allclose((do_1 + do_2).all_arrays, expected_do_1_all_arrays_with_y_summed)
768+
assert np.allclose((do_2 + do_1).all_arrays, expected_do_2_all_arrays_with_y_summed)
759769

760770

761771
def test_addition_operator_invalid_type(do_minimal_tth, invalid_add_type_error_msg):
@@ -767,9 +777,11 @@ def test_addition_operator_invalid_type(do_minimal_tth, invalid_add_type_error_m
767777
"string_value" + do
768778

769779

770-
def test_addition_operator_invalid_xarray_length(do_minimal, do_minimal_tth, x_grid_size_mismatch_error_msg):
780+
def test_addition_operator_invalid_yarray_length(do_minimal, do_minimal_tth, y_grid_size_mismatch_error_msg):
771781
# Combine two DO objects, one with empty xarrays (do_minimal) and the other with non-empty xarrays
772-
do_LHS = do_minimal
773-
do_RHS = do_minimal_tth
774-
with pytest.raises(ValueError, match=re.escape(x_grid_size_mismatch_error_msg)):
775-
do_LHS + do_RHS
782+
do_1 = do_minimal
783+
do_2 = do_minimal_tth
784+
assert len(do_1.all_arrays[:, 0]) == 0
785+
assert len(do_2.all_arrays[:, 0]) == 2
786+
with pytest.raises(ValueError, match=re.escape(y_grid_size_mismatch_error_msg)):
787+
do_1 + do_2

0 commit comments

Comments
 (0)