Skip to content

Commit b73d9d8

Browse files
authored
Merge pull request #273 from bobleesj/pytest-reformat
Improve test comments under `@pytest.mark.parametrize` in `test_diffraction_objects.py`
2 parents bd1b346 + 499a1c3 commit b73d9d8

File tree

2 files changed

+133
-31
lines changed

2 files changed

+133
-31
lines changed

news/pytest-comment.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* Group's Pytest practices for using @pytest.mark.parametrize in test_diffraction_objects.py
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: 110 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313

1414
@pytest.mark.parametrize(
15-
"do_args_1, do_args_2, expected_equality, warning_expected",
15+
"do_args_1, do_args_2, expected_equality, wavelength_warning_expected",
1616
[
1717
# Test when __eq__ returns True and False
18-
# Identical args, expect equality
18+
# C1: Identical args, expect equality
1919
(
2020
{
2121
"name": "same",
@@ -38,7 +38,8 @@
3838
True,
3939
False,
4040
),
41-
( # Different names, expect inequality
41+
# Different names, expect inequality
42+
(
4243
{
4344
"name": "something",
4445
"xtype": "tth",
@@ -56,7 +57,8 @@
5657
False,
5758
True,
5859
),
59-
( # One without wavelength, expect inequality
60+
# C2: One without wavelength, expect inequality
61+
(
6062
{
6163
"wavelength": 0.71,
6264
"xtype": "tth",
@@ -73,7 +75,8 @@
7375
False,
7476
True,
7577
),
76-
( # Different wavelengths, expect inequality
78+
# C3: Different wavelength values, expect inequality
79+
(
7780
{
7881
"wavelength": 0.71,
7982
"xtype": "tth",
@@ -91,7 +94,8 @@
9194
False,
9295
False,
9396
),
94-
( # Different scat_quantity, expect inequality
97+
# C4: Different scat_quantity, expect inequality
98+
(
9599
{
96100
"scat_quantity": "x-ray",
97101
"xtype": "tth",
@@ -109,7 +113,8 @@
109113
False,
110114
True,
111115
),
112-
( # Different q xarray values, expect inequality
116+
# C5: Different q xarray values, expect inequality
117+
(
113118
{
114119
"xtype": "q",
115120
"xarray": np.array([1.0, 2.0]),
@@ -124,7 +129,8 @@
124129
False,
125130
True,
126131
),
127-
( # Different metadata, expect inequality
132+
# C6: Different metadata, expect inequality
133+
(
128134
{
129135
"xtype": "q",
130136
"xarray": np.empty(0),
@@ -143,9 +149,9 @@
143149
],
144150
)
145151
def test_diffraction_objects_equality(
146-
do_args_1, do_args_2, expected_equality, warning_expected, wavelength_warning_msg
152+
do_args_1, do_args_2, expected_equality, wavelength_warning_expected, wavelength_warning_msg
147153
):
148-
if warning_expected:
154+
if wavelength_warning_expected:
149155
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_msg)):
150156
do_1 = DiffractionObject(**do_args_1)
151157
do_2 = DiffractionObject(**do_args_2)
@@ -158,9 +164,15 @@ def test_diffraction_objects_equality(
158164
@pytest.mark.parametrize(
159165
"xtype, expected_xarray",
160166
[
167+
# Test whether on_xtype returns the correct xarray values.
168+
# C1: tth to tth, expect no change in xarray value
169+
# 1. "tth" provided, expect tth
170+
# 2. "2theta" provided, expect tth
161171
("tth", np.array([30, 60])),
162172
("2theta", np.array([30, 60])),
173+
# C2: "q" provided, expect q converted from tth
163174
("q", np.array([0.51764, 1])),
175+
# C3: "d" provided, expect d converted from tth
164176
("d", np.array([12.13818, 6.28319])),
165177
],
166178
)
@@ -185,8 +197,8 @@ def test_init_invalid_xtype():
185197
@pytest.mark.parametrize(
186198
"org_do_args, target_do_args, scale_inputs, expected",
187199
[
188-
# Test that scale_to() scales to the correct values
189-
# C1: Same x-array and y-array, check offset
200+
# Test whether scale_to() scales to the expected values
201+
# C1: Same x-array and y-array with 2.1 offset, expect yarray shifted by 2.1 offset
190202
(
191203
{
192204
"xarray": np.array([10, 15, 25, 30, 60, 140]),
@@ -290,7 +302,8 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
290302
@pytest.mark.parametrize(
291303
"org_do_args, target_do_args, scale_inputs",
292304
[
293-
# UC1: User did not specify anything
305+
# Test expected errors produced from scale_to() with invalid inputs
306+
# C1: none of q, tth, d, provided, expect ValueError
294307
(
295308
{
296309
"xarray": np.array([0.1, 0.2, 0.3]),
@@ -311,7 +324,7 @@ def test_scale_to(org_do_args, target_do_args, scale_inputs, expected):
311324
"offset": 0,
312325
},
313326
),
314-
# UC2: User specified more than one of q, tth, and d
327+
# C2: tth and d both provided, expect ValueErrort
315328
(
316329
{
317330
"xarray": np.array([10, 25, 30.1, 40.2, 61, 120, 140]),
@@ -350,21 +363,84 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
350363

351364

352365
@pytest.mark.parametrize(
353-
"wavelength, xarray, yarray, xtype_1, xtype_2, value, expected_index",
366+
"do_args, get_array_index_inputs, expected_index",
354367
[
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]),
368+
# Test get_array_index() returns the expected index given xtype and value
369+
# C1: Target value is in the xarray and xtype is identical, expect exact index match
370+
(
371+
{
372+
"wavelength": 4 * np.pi,
373+
"xarray": np.array([30.005, 60]),
374+
"yarray": np.array([1, 2]),
375+
"xtype": "tth",
376+
},
377+
{
378+
"xtype": "tth",
379+
"value": 30.005,
380+
},
381+
[0],
382+
),
383+
# C2: Target value lies in the array, expect the (first) closest index
384+
(
385+
{
386+
"wavelength": 4 * np.pi,
387+
"xarray": np.array([30, 60]),
388+
"yarray": np.array([1, 2]),
389+
"xtype": "tth",
390+
},
391+
{
392+
"xtype": "tth",
393+
"value": 45,
394+
},
395+
[0],
396+
),
397+
(
398+
{
399+
"wavelength": 4 * np.pi,
400+
"xarray": np.array([30, 60]),
401+
"yarray": np.array([1, 2]),
402+
"xtype": "tth",
403+
},
404+
{
405+
"xtype": "q",
406+
"value": 0.25,
407+
},
408+
[0],
409+
),
410+
# C3: Target value out of the range, expect the closest index
411+
# 1. Test with xtype of "q"
412+
(
413+
{
414+
"wavelength": 4 * np.pi,
415+
"xarray": np.array([0.25, 0.5, 0.71]),
416+
"yarray": np.array([1, 2, 3]),
417+
"xtype": "q",
418+
},
419+
{
420+
"xtype": "q",
421+
"value": 0.1,
422+
},
423+
[0],
424+
),
425+
# 2. Test with xtype of "tth"
426+
(
427+
{
428+
"wavelength": 4 * np.pi,
429+
"xarray": np.array([30, 60]),
430+
"yarray": np.array([1, 2]),
431+
"xtype": "tth",
432+
},
433+
{
434+
"xtype": "tth",
435+
"value": 63,
436+
},
437+
[1],
438+
),
363439
],
364440
)
365-
def test_get_array_index(wavelength, xarray, yarray, xtype_1, xtype_2, value, expected_index):
366-
do = DiffractionObject(wavelength=wavelength, xarray=xarray, yarray=yarray, xtype=xtype_1)
367-
actual_index = do.get_array_index(value=value, xtype=xtype_2)
441+
def test_get_array_index(do_args, get_array_index_inputs, expected_index):
442+
do = DiffractionObject(**do_args)
443+
actual_index = do.get_array_index(get_array_index_inputs["value"], get_array_index_inputs["xtype"])
368444
assert actual_index == expected_index
369445

370446

@@ -411,7 +487,9 @@ def test_dump(tmp_path, mocker):
411487
@pytest.mark.parametrize(
412488
"do_init_args, expected_do_dict, divide_by_zero_warning_expected",
413489
[
414-
( # Instantiate just array attributes
490+
# Test __dict__ of DiffractionObject instance initialized with valid arguments
491+
(
492+
# C1: Minimum arguments provided for init, expect all attributes set without None
415493
{
416494
"xarray": np.array([0.0, 90.0, 180.0]),
417495
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -440,7 +518,8 @@ def test_dump(tmp_path, mocker):
440518
},
441519
True,
442520
),
443-
( # Instantiate just array attributes
521+
# C2: Initialize with an optional scat_quantity argument, expect non-empty string for scat_quantity
522+
(
444523
{
445524
"xarray": np.array([np.inf, 2 * np.sqrt(2) * np.pi, 2 * np.pi]),
446525
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -487,11 +566,12 @@ def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expec
487566
@pytest.mark.parametrize(
488567
"do_init_args, expected_error_msg",
489568
[
490-
( # C1: No arguments provided
569+
# Test expected error messages when 3 required arguments not provided in DiffractionObject init
570+
( # C1: No arguments provided, expect 3 required positional arguments error
491571
{},
492572
"missing 3 required positional arguments: 'xarray', 'yarray', and 'xtype'",
493573
),
494-
( # C2: Only xarray and yarray provided
574+
( # C2: Only xarray and yarray provided, expect 1 required positional argument error
495575
{"xarray": np.array([0.0, 90.0]), "yarray": np.array([0.0, 90.0])},
496576
"missing 1 required positional argument: 'xtype'",
497577
),
@@ -514,7 +594,6 @@ def test_all_array_getter(do_minimal_tth):
514594

515595
def test_all_array_setter(do_minimal):
516596
do = do_minimal
517-
# Attempt to directly modify the property
518597
with pytest.raises(
519598
AttributeError,
520599
match="Direct modification of attribute 'all_arrays' is not allowed. "

0 commit comments

Comments
 (0)