Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor test_transfor.py: pass variables to pytest parametrize, use conftest.py for common error messages #236

Merged
merged 8 commits into from
Dec 16, 2024
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@ def _load(filename):
@pytest.fixture
def do_minimal_tth():
return DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")


@pytest.fixture
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following two warning messages are used often in test_transform.py We could have access to this string within tests via conftest? We aren't really importing from src... so I think it really increases readability for some test cases (not all):

For example

From:

params_d_to_tth_bad = [
    # UC1: user specified invalid d values that result in tth > 180 degrees
    (
        [4 * np.pi, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2])],
        [
            ValueError,
            "The supplied input array and wavelength will result in an impossible two-theta. "
            "Please check these values and re-instantiate the DiffractionObject with correct values.",
        ],
    ),
    # UC2: user specified a wrong wavelength that result in tth > 180 degrees
    (
        [100, np.array([1, 0.8, 0.6, 0.4, 0.2, 0])],
        [
            ValueError,
            "The supplied input array and wavelength will result in an impossible two-theta. "
            "Please check these values and re-instantiate the DiffractionObject with correct values.",
        ],
    ),
]


@pytest.mark.parametrize("inputs, expected", params_d_to_tth_bad)
def test_d_to_tth_bad(inputs, expected):
    with pytest.raises(expected[0], match=expected[1]):
        d_to_tth(inputs[1], inputs[0])

to:

@pytest.mark.parametrize(
    "wavelength, d, expected_error_type",
    [
        # UC1: user specified invalid d values that result in tth > 180 degrees
        (4 * np.pi, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2]), ValueError),
        # UC2: user specified a wrong wavelength that result in tth > 180 degrees
        (100, np.array([1, 0.8, 0.6, 0.4, 0.2, 0]), ValueError),
    ],
)
def test_d_to_tth_bad(wavelength, d, expected_error_type, invalid_q_or_d_or_wavelength_error_msg):
    expected_error_msg = invalid_q_or_d_or_wavelength_error_msg
    with pytest.raises(expected_error_type, match=expected_error_msg):
        d_to_tth(d, wavelength)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes on that

def wavelength_warning_msg():
return (
"No wavelength has been specified. You can continue to use the DiffractionObject, but "
"some of its powerful features will not be available. "
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), "
"you may set do.wavelength = 1.54 for a wavelength of 1.54 angstroms."
)


@pytest.fixture
def invalid_q_or_d_or_wavelength_error_msg():
return (
"The supplied input array and wavelength will result in an impossible two-theta. "
"Please check these values and re-instantiate the DiffractionObject with correct values."
)
Loading
Loading