Skip to content

Commit c67fec3

Browse files
authored
Merge pull request #266 from bobleesj/runtime-warning
Continue refactor test functions, replace `UC` to `C` in `test_transforms.py`
2 parents ce6c9cc + 17c87f0 commit c67fec3

File tree

3 files changed

+140
-108
lines changed

3 files changed

+140
-108
lines changed

news/pytest-test-refactor.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* No news added: refactoring tests that have been mentioned in previous news
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

tests/test_diffraction_objects.py

Lines changed: 78 additions & 73 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)
@@ -406,7 +411,7 @@ def test_dump(tmp_path, mocker):
406411
@pytest.mark.parametrize(
407412
"do_init_args, expected_do_dict, divide_by_zero_warning_expected",
408413
[
409-
( # instantiate just array attributes
414+
( # Instantiate just array attributes
410415
{
411416
"xarray": np.array([0.0, 90.0, 180.0]),
412417
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -435,7 +440,7 @@ def test_dump(tmp_path, mocker):
435440
},
436441
True,
437442
),
438-
( # instantiate just array attributes
443+
( # Instantiate just array attributes
439444
{
440445
"xarray": np.array([np.inf, 2 * np.sqrt(2) * np.pi, 2 * np.pi]),
441446
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -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
),

0 commit comments

Comments
 (0)