Skip to content

Commit 2dfb953

Browse files
committed
Continue refactor test functions, replace UC to C
1 parent ce6c9cc commit 2dfb953

File tree

2 files changed

+112
-103
lines changed

2 files changed

+112
-103
lines changed

tests/test_diffraction_objects.py

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def test_init_invalid_xtype():
186186
"org_do_args, target_do_args, scale_inputs, expected",
187187
[
188188
# Test that scale_to() scales to the correct values
189-
# Case 1: same x-array and y-array, check offset
189+
# C1: same x-array and y-array, check offset
190190
(
191191
{
192192
"xarray": np.array([10, 15, 25, 30, 60, 140]),
@@ -208,7 +208,7 @@ def test_init_invalid_xtype():
208208
},
209209
{"xtype": "tth", "yarray": np.array([4.1, 5.1, 6.1, 7.1, 8.1, 9.1])},
210210
),
211-
# Case 2: same length x-arrays with exact x-value match
211+
# C2: same length x-arrays with exact x-value match
212212
(
213213
{
214214
"xarray": np.array([10, 15, 25, 30, 60, 140]),
@@ -230,7 +230,7 @@ def test_init_invalid_xtype():
230230
},
231231
{"xtype": "tth", "yarray": np.array([1, 2, 2.5, 3, 6, 10])},
232232
),
233-
# Case 3: same length x-arrays with approximate x-value match
233+
# C3: same length x-arrays with approximate x-value match
234234
(
235235
{
236236
"xarray": np.array([0.12, 0.24, 0.31, 0.4]),
@@ -252,7 +252,7 @@ def test_init_invalid_xtype():
252252
},
253253
{"xtype": "q", "yarray": np.array([1, 2, 4, 6])},
254254
),
255-
# Case 4: different x-array lengths with approximate x-value match
255+
# C4: different x-array lengths with approximate x-value match
256256
(
257257
{
258258
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
@@ -272,7 +272,7 @@ def test_init_invalid_xtype():
272272
"d": None,
273273
"offset": 0,
274274
},
275-
# Case 5: Scaling factor is calculated at index = 4 (tth=61) for self and index = 5 for target (tth=62)
275+
# C5: Scaling factor is calculated at index = 4 (tth=61) for self and index = 5 for target (tth=62)
276276
{"xtype": "tth", "yarray": np.array([1, 2, 3, 4, 5, 6, 10])},
277277
),
278278
],
@@ -287,76 +287,81 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
287287
assert np.allclose(scaled_do.on_xtype(expected["xtype"])[1], expected["yarray"])
288288

289289

290-
params_scale_to_bad = [
291-
# UC1: user did not specify anything
292-
(
293-
{
294-
"xarray": np.array([0.1, 0.2, 0.3]),
295-
"yarray": np.array([1, 2, 3]),
296-
"xtype": "q",
297-
"wavelength": 2 * np.pi,
298-
"target_xarray": np.array([0.05, 0.1, 0.2, 0.3]),
299-
"target_yarray": np.array([5, 10, 20, 30]),
300-
"target_xtype": "q",
301-
"target_wavelength": 2 * np.pi,
302-
"q": None,
303-
"tth": None,
304-
"d": None,
305-
"offset": 0,
306-
}
307-
),
308-
# UC2: user specified more than one of q, tth, and d
309-
(
310-
{
311-
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
312-
"yarray": np.array([10, 20, 30, 40, 50, 60, 100]),
313-
"xtype": "tth",
314-
"wavelength": 2 * np.pi,
315-
"target_xarray": np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
316-
"target_yarray": np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
317-
"target_xtype": "tth",
318-
"target_wavelength": 2 * np.pi,
319-
"q": None,
320-
"tth": 60,
321-
"d": 10,
322-
"offset": 0,
323-
}
324-
),
325-
]
326-
327-
328-
@pytest.mark.parametrize("inputs", params_scale_to_bad)
329-
def test_scale_to_bad(inputs):
330-
orig_diff_object = DiffractionObject(
331-
xarray=inputs["xarray"], yarray=inputs["yarray"], xtype=inputs["xtype"], wavelength=inputs["wavelength"]
332-
)
333-
target_diff_object = DiffractionObject(
334-
xarray=inputs["target_xarray"],
335-
yarray=inputs["target_yarray"],
336-
xtype=inputs["target_xtype"],
337-
wavelength=inputs["target_wavelength"],
338-
)
290+
@pytest.mark.parametrize(
291+
"org_do_args, target_do_args, scale_inputs",
292+
[
293+
# UC1: user did not specify anything
294+
(
295+
{
296+
"xarray": np.array([0.1, 0.2, 0.3]),
297+
"yarray": np.array([1, 2, 3]),
298+
"xtype": "q",
299+
"wavelength": 2 * np.pi,
300+
},
301+
{
302+
"xarray": np.array([0.05, 0.1, 0.2, 0.3]),
303+
"yarray": np.array([5, 10, 20, 30]),
304+
"xtype": "q",
305+
"wavelength": 2 * np.pi,
306+
},
307+
{
308+
"q": None,
309+
"tth": None,
310+
"d": None,
311+
"offset": 0,
312+
},
313+
),
314+
# UC2: user specified more than one of q, tth, and d
315+
(
316+
{
317+
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
318+
"yarray": np.array([10, 20, 30, 40, 50, 60, 100]),
319+
"xtype": "tth",
320+
"wavelength": 2 * np.pi,
321+
},
322+
{
323+
"xarray": np.array([20, 25.5, 32, 45, 50, 62, 100, 125, 140]),
324+
"yarray": np.array([1.1, 2, 3, 3.5, 4, 5, 10, 12, 13]),
325+
"xtype": "tth",
326+
"wavelength": 2 * np.pi,
327+
},
328+
{
329+
"q": None,
330+
"tth": 60,
331+
"d": 10,
332+
"offset": 0,
333+
},
334+
),
335+
],
336+
)
337+
def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
338+
original_do = DiffractionObject(**org_do_args)
339+
target_do = DiffractionObject(**target_do_args)
339340
with pytest.raises(
340341
ValueError, match="You must specify exactly one of 'q', 'tth', or 'd'. Please rerun specifying only one."
341342
):
342-
orig_diff_object.scale_to(
343-
target_diff_object, q=inputs["q"], tth=inputs["tth"], d=inputs["d"], offset=inputs["offset"]
343+
original_do.scale_to(
344+
target_do,
345+
q=scale_inputs["q"],
346+
tth=scale_inputs["tth"],
347+
d=scale_inputs["d"],
348+
offset=scale_inputs["offset"],
344349
)
345350

346351

347-
params_index = [
348-
# UC1: exact match
349-
(4 * np.pi, np.array([30.005, 60]), np.array([1, 2]), "tth", "tth", 30.005, [0]),
350-
# UC2: target value lies in the array, returns the (first) closest index
351-
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 45, [0]),
352-
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "q", 0.25, [0]),
353-
# UC3: target value out of the range, returns the closest index
354-
(4 * np.pi, np.array([0.25, 0.5, 0.71]), np.array([1, 2, 3]), "q", "q", 0.1, [0]),
355-
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 63, [1]),
356-
]
357-
358-
359-
@pytest.mark.parametrize("wavelength, xarray, yarray, xtype_1, xtype_2, value, expected_index", params_index)
352+
@pytest.mark.parametrize(
353+
"wavelength, xarray, yarray, xtype_1, xtype_2, value, expected_index",
354+
[
355+
# UC1: exact match
356+
(4 * np.pi, np.array([30.005, 60]), np.array([1, 2]), "tth", "tth", 30.005, [0]),
357+
# UC2: target value lies in the array, returns the (first) closest index
358+
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 45, [0]),
359+
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "q", 0.25, [0]),
360+
# UC3: target value out of the range, returns the closest index
361+
(4 * np.pi, np.array([0.25, 0.5, 0.71]), np.array([1, 2, 3]), "q", "q", 0.1, [0]),
362+
(4 * np.pi, np.array([30, 60]), np.array([1, 2]), "tth", "tth", 63, [1]),
363+
],
364+
)
360365
def test_get_array_index(wavelength, xarray, yarray, xtype_1, xtype_2, value, expected_index):
361366
do = DiffractionObject(wavelength=wavelength, xarray=xarray, yarray=yarray, xtype=xtype_1)
362367
actual_index = do.get_array_index(value=value, xtype=xtype_2)
@@ -482,11 +487,11 @@ def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expec
482487
@pytest.mark.parametrize(
483488
"do_init_args, expected_error_msg",
484489
[
485-
( # Case 1: no arguments provided
490+
( # C1: no arguments provided
486491
{},
487492
"missing 3 required positional arguments: 'xarray', 'yarray', and 'xtype'",
488493
),
489-
( # Case 2: only xarray and yarray provided
494+
( # C2: only xarray and yarray provided
490495
{"xarray": np.array([0.0, 90.0]), "yarray": np.array([0.0, 90.0])},
491496
"missing 1 required positional argument: 'xtype'",
492497
),

tests/test_transforms.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@
99
@pytest.mark.parametrize(
1010
"wavelength, q, expected_tth",
1111
[
12-
# Case 1: Allow empty arrays for q
13-
# 1. Empty q values, no wavelength, return empty arrays
12+
# Test conversion of q to tth with valid values
13+
# C1. Empty q values
14+
# 1. No wavelength, expect empty arrays
1415
(None, np.empty((0)), np.empty((0))),
15-
# 2. Empty q values, wavelength specified, return empty arrays
16+
# 2. wavelength, expect empty arrays
1617
(4 * np.pi, np.empty((0)), np.empty(0)),
17-
# Case 2: Allow wavelength to be missing.
18-
# Valid q values, no wavelength, return index array
18+
19+
# C2. Non-empty q values
20+
# 2. No wavelength, expect value tth values
1921
(
2022
None,
2123
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]),
2224
np.array([0, 1, 2, 3, 4, 5]),
2325
),
24-
# Case 3: Correctly specified q and wavelength
25-
# Expected tth values are 2*arcsin(q) in degrees
26+
27+
# C3: Both valid q and wavelength provided
28+
# 1. Expected tth values are 2*arcsin(q) in degrees
2629
(4 * np.pi, np.array([0, 1 / np.sqrt(2), 1.0]), np.array([0, 90.0, 180.0])),
2730
],
2831
)
@@ -38,14 +41,15 @@ def test_q_to_tth(wavelength, q, expected_tth, wavelength_warning_msg):
3841

3942
@pytest.mark.parametrize(
4043
"wavelength, q, expected_error_type",
41-
[
42-
# UC1: user specified invalid q values that result in tth > 180 degrees
44+
[
45+
# Test error messages in q to tth conversion with invalid Two theta values.
46+
# C1: Invalid q values that result in tth > 180 degrees
4347
(
4448
4 * np.pi,
4549
np.array([0.2, 0.4, 0.6, 0.8, 1, 1.2]),
4650
ValueError,
4751
),
48-
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
52+
# C2: Wrong wavelength that results in tth > 180 degrees
4953
(
5054
100,
5155
np.array([0, 0.2, 0.4, 0.6, 0.8, 1]),
@@ -62,17 +66,17 @@ def test_q_to_tth_bad(wavelength, q, expected_error_type, invalid_q_or_d_or_wave
6266
@pytest.mark.parametrize(
6367
"wavelength, tth, expected_q",
6468
[
65-
# UC0: user specified empty tth values (without wavelength)
69+
# C0: Empty tth values (without wavelength)
6670
(None, np.array([]), np.array([])),
67-
# UC1: user specified empty tth values (with wavelength)
71+
# C1: Empty tth values (with wavelength)
6872
(4 * np.pi, np.array([]), np.array([])),
69-
# UC2: user specified valid tth values between 0-180 degrees (without wavelength)
73+
# C2: valid tth values between 0-180 degrees (without wavelength)
7074
(
7175
None,
7276
np.array([0, 30, 60, 90, 120, 180]),
7377
np.array([0, 1, 2, 3, 4, 5]),
7478
),
75-
# UC3: user specified valid tth values between 0-180 degrees (with wavelength)
79+
# C3: valid tth values between 0-180 degrees (with wavelength)
7680
# expected q values are sin15, sin30, sin45, sin60, sin90
7781
(
7882
4 * np.pi,
@@ -94,14 +98,14 @@ def test_tth_to_q(wavelength, tth, expected_q, wavelength_warning_msg):
9498
@pytest.mark.parametrize(
9599
"wavelength, tth, expected_error_type, expected_error_msg",
96100
[
97-
# UC0: user specified an invalid tth value of > 180 degrees (without wavelength)
101+
# C1: invalid tth value of > 180 degrees, no wavelength, expect two theta ValueError
98102
(
99103
None,
100104
np.array([0, 30, 60, 90, 120, 181]),
101105
ValueError,
102106
"Two theta exceeds 180 degrees. Please check the input values for errors.",
103107
),
104-
# UC1: user specified an invalid tth value of > 180 degrees (with wavelength)
108+
# C2: invalid tth value of > 180 degrees with wavelength, expect two theta ValueError
105109
(
106110
4 * np.pi,
107111
np.array([0, 30, 60, 90, 120, 181]),
@@ -119,9 +123,9 @@ def test_tth_to_q_bad(wavelength, tth, expected_error_type, expected_error_msg):
119123
"q, expected_d, warning_expected",
120124
[
121125
# Test conversion of q to d with valid values
122-
# Case 1: empty q values, expect empty d values
126+
# C1: empty q values, expect empty d values
123127
(np.array([]), np.array([]), False),
124-
# Case 2:
128+
# C2:
125129
# 1. valid q values, expect d values without warning
126130
(
127131
np.array([0.1, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi]),
@@ -148,9 +152,9 @@ def test_q_to_d(q, expected_d, warning_expected):
148152
@pytest.mark.parametrize(
149153
"d, expected_q, zero_divide_error_expected",
150154
[
151-
# UC1: User specified empty d values
155+
# C1: User specified empty d values
152156
(np.array([]), np.array([]), False),
153-
# UC2: User specified valid d values
157+
# C2: User specified valid d values
154158
(
155159
np.array([5 * np.pi, 4 * np.pi, 3 * np.pi, 2 * np.pi, np.pi, 0]),
156160
np.array([0.4, 0.5, 0.66667, 1, 2, np.inf]),
@@ -171,13 +175,13 @@ def test_d_to_q(d, expected_q, zero_divide_error_expected):
171175
"wavelength, tth, expected_d, divide_by_zero_warning_expected",
172176
[
173177
# Test conversion of q to d with valid values
174-
# Case 1: empty tth values, no, expect empty d values
178+
# C1: empty tth values, no, expect empty d values
175179
(None, np.array([]), np.array([]), False),
176-
# Case 2: empty tth values, wavelength provided, expect empty d values
180+
# C2: empty tth values, wavelength provided, expect empty d values
177181
(4 * np.pi, np.array([]), np.array([]), False),
178-
# Case 3: User specified valid tth values between 0-180 degrees (without wavelength)
182+
# C3: User specified valid tth values between 0-180 degrees (without wavelength)
179183
(None, np.array([0, 30, 60, 90, 120, 180]), np.array([0, 1, 2, 3, 4, 5]), False),
180-
# Case 4: User specified valid tth values between 0-180 degrees (with wavelength)
184+
# C4: User specified valid tth values between 0-180 degrees (with wavelength)
181185
(
182186
4 * np.pi,
183187
np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0]),
@@ -201,14 +205,14 @@ def test_tth_to_d(wavelength, tth, expected_d, divide_by_zero_warning_expected,
201205
@pytest.mark.parametrize(
202206
"wavelength, tth, expected_error_type, expected_error_msg",
203207
[
204-
# UC1: user specified an invalid tth value of > 180 degrees (without wavelength)
208+
# C1: user specified an invalid tth value of > 180 degrees (without wavelength)
205209
(
206210
None,
207211
np.array([0, 30, 60, 90, 120, 181]),
208212
ValueError,
209213
"Two theta exceeds 180 degrees. Please check the input values for errors.",
210214
),
211-
# UC2: user specified an invalid tth value of > 180 degrees (with wavelength)
215+
# C2: user specified an invalid tth value of > 180 degrees (with wavelength)
212216
(
213217
4 * np.pi,
214218
np.array([0, 30, 60, 90, 120, 181]),
@@ -225,13 +229,13 @@ def test_tth_to_d_invalid(wavelength, tth, expected_error_type, expected_error_m
225229
@pytest.mark.parametrize(
226230
"wavelength, d, expected_tth, divide_by_zero_warning_expected",
227231
[
228-
# UC1: Empty d values, no wavelength, return empty arrays
232+
# C1: Empty d values, no wavelength, return empty arrays
229233
(None, np.empty((0)), np.empty((0)), False),
230-
# UC2: Empty d values, wavelength specified, return empty arrays
234+
# C2: Empty d values, wavelength specified, return empty arrays
231235
(4 * np.pi, np.empty((0)), np.empty(0), False),
232-
# UC3: User specified valid d values, no wavelength, return empty arrays
236+
# C3: User specified valid d values, no wavelength, return empty arrays
233237
(None, np.array([1, 0.8, 0.6, 0.4, 0.2, 0]), np.array([0, 1, 2, 3, 4, 5]), True),
234-
# UC4: User specified valid d values (with wavelength)
238+
# C4: User specified valid d values (with wavelength)
235239
(
236240
4 * np.pi,
237241
np.array([4 * np.pi, 4 / np.sqrt(2) * np.pi, 4 / np.sqrt(3) * np.pi]),
@@ -257,9 +261,9 @@ def test_d_to_tth(wavelength, d, expected_tth, divide_by_zero_warning_expected,
257261
@pytest.mark.parametrize(
258262
"wavelength, d, expected_error_type",
259263
[
260-
# UC1: user specified invalid d values that result in tth > 180 degrees
264+
# C1: user specified invalid d values that result in tth > 180 degrees
261265
(4 * np.pi, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2]), ValueError),
262-
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
266+
# C2: user specified a wrong wavelength that result in tth > 180 degrees
263267
(100, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2]), ValueError),
264268
],
265269
)

0 commit comments

Comments
 (0)