Skip to content

Commit 7543305

Browse files
committed
Refactor test.parameter test cases using BG standard
1 parent 697e799 commit 7543305

File tree

1 file changed

+95
-24
lines changed

1 file changed

+95
-24
lines changed

tests/test_diffraction_objects.py

Lines changed: 95 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
( # C2: 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-
( # C3: Different wavelength values, 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-
( # C4: 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-
( # C5: 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-
( # C6: Different metadata, expect inequality
132+
# C6: Different metadata, expect inequality
133+
(
128134
{
129135
"xtype": "q",
130136
"xarray": np.empty(0),
@@ -358,22 +364,84 @@ def test_scale_to_bad(org_do_args, target_do_args, scale_inputs):
358364

359365

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

379447

@@ -420,7 +488,9 @@ def test_dump(tmp_path, mocker):
420488
@pytest.mark.parametrize(
421489
"do_init_args, expected_do_dict, divide_by_zero_warning_expected",
422490
[
423-
( # Instantiate just array attributes
491+
# Test __dict__ of DiffractionObject instance initialized with valid arguments
492+
(
493+
# C1: Minimum arguments provided for init, expect all attributes set without None
424494
{
425495
"xarray": np.array([0.0, 90.0, 180.0]),
426496
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -449,7 +519,8 @@ def test_dump(tmp_path, mocker):
449519
},
450520
True,
451521
),
452-
( # Instantiate just array attributes
522+
# C2: Initialize with an optional scat_quantity argument, expect non-empty string for scat_quantity
523+
(
453524
{
454525
"xarray": np.array([np.inf, 2 * np.sqrt(2) * np.pi, 2 * np.pi]),
455526
"yarray": np.array([1.0, 2.0, 3.0]),
@@ -496,11 +567,12 @@ def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expec
496567
@pytest.mark.parametrize(
497568
"do_init_args, expected_error_msg",
498569
[
499-
( # C1: No arguments provided
570+
# Test expected error messages due to required arguments in DiffractionObject constructor
571+
( # C1: No arguments provided, expect 3 required positional arguments error
500572
{},
501573
"missing 3 required positional arguments: 'xarray', 'yarray', and 'xtype'",
502574
),
503-
( # C2: Only xarray and yarray provided
575+
( # C2: Only xarray and yarray provided, expect 1 required positional argument error
504576
{"xarray": np.array([0.0, 90.0]), "yarray": np.array([0.0, 90.0])},
505577
"missing 1 required positional argument: 'xtype'",
506578
),
@@ -523,7 +595,6 @@ def test_all_array_getter(do_minimal_tth):
523595

524596
def test_all_array_setter(do_minimal):
525597
do = do_minimal
526-
# Attempt to directly modify the property
527598
with pytest.raises(
528599
AttributeError,
529600
match="Direct modification of attribute 'all_arrays' is not allowed. "

0 commit comments

Comments
 (0)