Skip to content

Commit a33482b

Browse files
add more tests
1 parent 4e85b0a commit a33482b

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

tests/diffpy/utils/scattering_objects/test_diffraction_objects.py

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,52 @@ def test_diffraction_objects_equality(inputs1, inputs2, expected):
232232

233233

234234
def test_q_to_tth():
235-
# valid q values including edge cases when q=0 or tth=180 after converting
236-
# expected tth values are 2*arcsin(q)
235+
# Valid q values that should result in 0-180 tth values after conversion
236+
# expected tth values are 2*arcsin(q) in degrees
237237
actual = DiffractionObject(wavelength=4 * np.pi)
238238
setattr(actual, "on_q", [[0, 0.2, 0.4, 0.6, 0.8, 1], [1, 2, 3, 4, 5, 6]])
239239
actual_tth = actual.q_to_tth()
240240
expected_tth = [0, 23.07392, 47.15636, 73.73980, 106.26020, 180]
241241
assert np.allclose(actual_tth, expected_tth)
242242

243243

244-
def test_q_to_tth_bad():
245-
# invalid wavelength or q values when arcsin value is not in the range of [-1, 1]
246-
actual = DiffractionObject(wavelength=4 * np.pi)
247-
setattr(actual, "on_q", [[0.6, 0.8, 1, 1.2], [1, 2, 3, 4]])
244+
params_q_to_tth_bad = [
245+
# UC1: user did not specify wavelength
246+
(
247+
[None, [0, 0.2, 0.4, 0.6, 0.8, 1]],
248+
"Wavelength is not specified. Please provide a valid wavelength, "
249+
"e.g., DiffractionObject(wavelength=0.71).",
250+
),
251+
# UC2: user specified invalid q values that result in tth > 180 degrees
252+
(
253+
[4 * np.pi, [0.2, 0.4, 0.6, 0.8, 1, 1.2]],
254+
"Wavelength * q > 4 * pi. Please check if you entered an incorrect wavelength or q value.",
255+
),
256+
# UC3: user specified a wrong wavelength that result in tth > 180 degrees
257+
(
258+
[100, [0, 0.2, 0.4, 0.6, 0.8, 1]],
259+
"Wavelength * q > 4 * pi. Please check if you entered an incorrect wavelength or q value.",
260+
),
261+
# UC4: user specified an empty q array
262+
([4 * np.pi, []], "Q array is empty. Please provide valid q values."),
263+
# UC5: user specified a non-numeric value in q array
264+
(
265+
[4 * np.pi, [0, 0.2, 0.4, 0.6, 0.8, "invalid"]],
266+
"Invalid value found in q array. Please ensure all values are numeric.",
267+
),
268+
]
269+
270+
271+
@pytest.mark.parametrize("inputs, expected", params_q_to_tth_bad)
272+
def test_q_to_tth_bad(inputs, expected):
273+
actual = DiffractionObject(wavelength=inputs[0])
274+
setattr(actual, "on_q", [inputs[1], [1, 2, 3, 4, 5, 6]])
248275
with pytest.raises(ValueError):
249276
actual.q_to_tth()
250277

251278

252279
def test_tth_to_q():
253-
# valid tth values including edge cases when tth=0 or 180 degree
280+
# Valid tth values between 0-180 degrees
254281
# expected q vales are sin15, sin30, sin45, sin60, sin90
255282
actual = DiffractionObject(wavelength=4 * np.pi)
256283
setattr(actual, "on_tth", [[0, 30, 60, 90, 120, 180], [1, 2, 3, 4, 5, 6]])
@@ -259,11 +286,39 @@ def test_tth_to_q():
259286
assert np.allclose(actual_q, expected_q)
260287

261288

262-
def test_tth_to_q_bad():
263-
# invalid tth value of > 180 degree
264-
actual = DiffractionObject(wavelength=4 * np.pi)
265-
setattr(actual, "on_tth", [[0, 30, 60, 90, 120, 181], [1, 2, 3, 4, 5, 6]])
266-
with pytest.raises(ValueError):
289+
params_tth_to_q_bad = [
290+
# UC1: user did not specify wavelength
291+
(
292+
[None, [0, 30, 60, 90, 120, 180]],
293+
"Wavelength is not specified. Please provide a valid wavelength, "
294+
"e.g., DiffractionObject(wavelength=0.71).",
295+
),
296+
# UC2: user specified an invalid tth value of > 180 degrees
297+
(
298+
[4 * np.pi, [0, 30, 60, 90, 120, 181]],
299+
"Two theta exceeds 180 degrees. Please check the input values for errors.",
300+
),
301+
# UC3: user did not specify wavelength and specified invalid tth values
302+
(
303+
[None, [0, 30, 60, 90, 120, 181]],
304+
"Wavelength is not specified. Please provide a valid wavelength, "
305+
"e.g., DiffractionObject(wavelength=0.71).",
306+
),
307+
# UC4: user specified an empty two theta array
308+
([4 * np.pi, []], "Two theta array is empty. Please provide valid two theta values."),
309+
# UC5: user specified a non-numeric value in two theta array
310+
(
311+
[4 * np.pi, [0, 30, 60, 90, 120, "invalid"]],
312+
"Invalid value found in two theta array. Please ensure all values are numeric.",
313+
),
314+
]
315+
316+
317+
@pytest.mark.parametrize("inputs, expected", params_tth_to_q_bad)
318+
def test_tth_to_q_bad(inputs, expected):
319+
actual = DiffractionObject(wavelength=inputs[0])
320+
setattr(actual, "on_tth", [inputs[1], [1, 2, 3, 4, 5, 6]])
321+
with pytest.raises(ValueError, match=expected):
267322
actual.tth_to_q()
268323

269324

0 commit comments

Comments
 (0)