Skip to content
25 changes: 16 additions & 9 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@

params_q_to_tth = [
# UC1: Empty q values, no wavelength, return empty arrays
([None, np.empty((0))], np.empty((0))),
# UC2: Empty q values, wavelength specified, return empty arrays
([4 * np.pi, np.empty((0))], np.empty(0)),
(None, np.empty((0)), np.empty((0))),
# # UC2: Empty q values, wavelength specified, return empty arrays
(4 * np.pi, np.empty((0)), np.empty(0)),
# UC3: User specified valid q values, no wavelength, return empty arrays
(
[None, np.array([0, 0.2, 0.4, 0.6, 0.8, 1])],
None,
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]),
np.array([0, 1, 2, 3, 4, 5]),
),
# UC4: User specified valid q values (with wavelength)
# expected tth values are 2*arcsin(q) in degrees
([4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0])], np.array([0, 90.0, 180.0])),
(4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0]), np.array([0, 90.0, 180.0])),
]


@pytest.mark.parametrize("inputs, expected", params_q_to_tth)
def test_q_to_tth(inputs, expected):
actual = q_to_tth(inputs[1], inputs[0])
assert np.allclose(expected, actual)
@pytest.mark.parametrize("wavelength, q, expected_tth", params_q_to_tth)
def test_q_to_tth(wavelength, q, expected_tth):

if wavelength is None:
with pytest.warns(UserWarning, match="INFO: no wavelength has been specified"):
Copy link
Contributor Author

@bobleesj bobleesj Dec 13, 2024

Choose a reason for hiding this comment

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

Second, here, it feels counter productive to write all the warning msgs:

UserWarning: INFO: 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, set diffraction_object.wavelength = [number], where diffraction_object is the variable name of you Diffraction Object, and number is the wavelength in angstroms.

assuming we have a specific test func for this warning msg.

Please suggest if this is okay @sbillinge

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the user warning written out because the test defines the behavior. So the workflow is to write the test and review it until we like it. Then write the code. Here, it slipped by but we don't want "INFO" here. It is either a WARNING or an INFO, it can't be both. Also, I think when we are describing code it is better to write the code than describe it in words, so say ".... To speciffy a wavelength, if my_object = DiffractionObject(xarray, yarray, "tth") then you can type my_object.wavelength = 1.54` if the wavelength is 1.54 angstroms." Something like that.

If test the same message in more than one test we can assign it to a variable. But it is better if it is closer to the test and we are not importing it orsomething.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, done.

actual_tth = q_to_tth(q, wavelength)
else:
actual_tth = q_to_tth(q, wavelength)

assert np.allclose(expected_tth, actual_tth)


params_q_to_tth_bad = [
Expand Down
Loading