Skip to content

Commit 7584105

Browse files
fix: fix test functions using new group standard, remove _instantiate_test_do
1 parent 08032ab commit 7584105

File tree

2 files changed

+145
-80
lines changed

2 files changed

+145
-80
lines changed

news/docstring-tests.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
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_functions.py

Lines changed: 122 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,130 +6,172 @@
66
from diffpy.labpdfproc.functions import CVE_METHODS, Gridded_circle, apply_corr, compute_cve
77
from diffpy.utils.diffraction_objects import DiffractionObject
88

9-
params1 = [
10-
([0.5, 3, 1], {(0.0, -0.5), (0.0, 0.0), (0.5, 0.0), (-0.5, 0.0), (0.0, 0.5)}),
11-
([1, 4, 1], {(-0.333333, -0.333333), (0.333333, -0.333333), (-0.333333, 0.333333), (0.333333, 0.333333)}),
12-
]
139

14-
15-
@pytest.mark.parametrize("inputs, expected", params1)
16-
def test_get_grid_points(inputs, expected):
17-
expected_grid = expected
18-
actual_gs = Gridded_circle(radius=inputs[0], n_points_on_diameter=inputs[1], mu=inputs[2])
10+
@pytest.mark.parametrize(
11+
"inputs, expected_grid",
12+
[
13+
(
14+
{"radius": 0.5, "n_points_on_diameter": 3, "mu": 1},
15+
{(0.0, -0.5), (0.0, 0.0), (0.5, 0.0), (-0.5, 0.0), (0.0, 0.5)},
16+
),
17+
(
18+
{"radius": 1, "n_points_on_diameter": 4, "mu": 1},
19+
{(-0.333333, -0.333333), (0.333333, -0.333333), (-0.333333, 0.333333), (0.333333, 0.333333)},
20+
),
21+
],
22+
)
23+
def test_get_grid_points(inputs, expected_grid):
24+
actual_gs = Gridded_circle(
25+
radius=inputs["radius"], n_points_on_diameter=inputs["n_points_on_diameter"], mu=inputs["mu"]
26+
)
1927
actual_grid_sorted = sorted(actual_gs.grid)
2028
expected_grid_sorted = sorted(expected_grid)
2129
for actual_point, expected_point in zip(actual_grid_sorted, expected_grid_sorted):
2230
assert actual_point == pytest.approx(expected_point, rel=1e-4, abs=1e-6)
2331

2432

25-
params2 = [
26-
([1, 3, 1, 45], [0, 1.4142135, 1.4142135, 2, 2]),
27-
([1, 3, 1, 90], [0, 0, 2, 2, 2]),
28-
([1, 3, 1, 120], [0, 0, 2, 3, 1.73205]),
29-
([1, 4, 1, 30], [2.057347, 2.044451, 1.621801, 1.813330]),
30-
([1, 4, 1, 90], [1.885618, 1.885618, 2.552285, 1.218951]),
31-
([1, 4, 1, 140], [1.139021, 2.200102, 2.744909, 1.451264]),
32-
]
33-
34-
35-
@pytest.mark.parametrize("inputs, expected", params2)
36-
def test_set_distances_at_angle(inputs, expected):
37-
expected_distances = expected
38-
actual_gs = Gridded_circle(radius=inputs[0], n_points_on_diameter=inputs[1], mu=inputs[2])
39-
actual_gs.set_distances_at_angle(inputs[3])
33+
@pytest.mark.parametrize(
34+
"inputs, expected_distances",
35+
[
36+
({"radius": 1, "n_points_on_diameter": 3, "mu": 1, "angle": 45}, [0, 1.4142135, 1.4142135, 2, 2]),
37+
({"radius": 1, "n_points_on_diameter": 3, "mu": 1, "angle": 90}, [0, 0, 2, 2, 2]),
38+
({"radius": 1, "n_points_on_diameter": 3, "mu": 1, "angle": 120}, [0, 0, 2, 3, 1.73205]),
39+
({"radius": 1, "n_points_on_diameter": 4, "mu": 1, "angle": 30}, [2.057347, 2.044451, 1.621801, 1.813330]),
40+
({"radius": 1, "n_points_on_diameter": 4, "mu": 1, "angle": 90}, [1.885618, 1.885618, 2.552285, 1.218951]),
41+
(
42+
{"radius": 1, "n_points_on_diameter": 4, "mu": 1, "angle": 140},
43+
[1.139021, 2.200102, 2.744909, 1.451264],
44+
),
45+
],
46+
)
47+
def test_set_distances_at_angle(inputs, expected_distances):
48+
actual_gs = Gridded_circle(
49+
radius=inputs["radius"], n_points_on_diameter=inputs["n_points_on_diameter"], mu=inputs["mu"]
50+
)
51+
actual_gs.set_distances_at_angle(inputs["angle"])
4052
actual_distances_sorted = sorted(actual_gs.distances)
4153
expected_distances_sorted = sorted(expected_distances)
4254
assert actual_distances_sorted == pytest.approx(expected_distances_sorted, rel=1e-4, abs=1e-6)
4355

4456

45-
params3 = [
46-
([1], [1, 1, 0.135335, 0.049787, 0.176921]),
47-
([2], [1, 1, 0.018316, 0.002479, 0.031301]),
48-
]
49-
50-
51-
@pytest.mark.parametrize("inputs, expected", params3)
52-
def test_set_muls_at_angle(inputs, expected):
53-
expected_muls = expected
54-
actual_gs = Gridded_circle(radius=1, n_points_on_diameter=3, mu=inputs[0])
57+
@pytest.mark.parametrize(
58+
"input_mu, expected_muls",
59+
[
60+
(1, [1, 1, 0.135335, 0.049787, 0.176921]),
61+
(2, [1, 1, 0.018316, 0.002479, 0.031301]),
62+
],
63+
)
64+
def test_set_muls_at_angle(input_mu, expected_muls):
65+
actual_gs = Gridded_circle(radius=1, n_points_on_diameter=3, mu=input_mu)
5566
actual_gs.set_muls_at_angle(120)
5667
actual_muls_sorted = sorted(actual_gs.muls)
5768
expected_muls_sorted = sorted(expected_muls)
5869
assert actual_muls_sorted == pytest.approx(expected_muls_sorted, rel=1e-4, abs=1e-6)
5970

6071

61-
def _instantiate_test_do(xarray, yarray, xtype="tth", name="test", scat_quantity="x-ray"):
62-
test_do = DiffractionObject(
72+
@pytest.mark.parametrize(
73+
"input_xtype, expected",
74+
[
75+
("tth", {"xarray": np.array([90, 90.1, 90.2]), "yarray": np.array([0.5, 0.5, 0.5]), "xtype": "tth"}),
76+
(
77+
"q",
78+
{"xarray": np.array([5.76998, 5.77501, 5.78004]), "yarray": np.array([0.5, 0.5, 0.5]), "xtype": "q"},
79+
),
80+
],
81+
)
82+
def test_compute_cve(input_xtype, expected, mocker):
83+
xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2])
84+
expected_cve = np.array([0.5, 0.5, 0.5])
85+
mocker.patch("numpy.interp", return_value=expected_cve)
86+
input_pattern = DiffractionObject(
6387
xarray=xarray,
6488
yarray=yarray,
65-
xtype=xtype,
89+
xtype="tth",
6690
wavelength=1.54,
67-
scat_quantity=scat_quantity,
68-
name=name,
91+
scat_quantity="x-ray",
92+
name="test",
6993
metadata={"thing1": 1, "thing2": "thing2"},
7094
)
71-
return test_do
72-
73-
74-
params4 = [
75-
(["tth"], [np.array([90, 90.1, 90.2]), np.array([0.5, 0.5, 0.5]), "tth"]),
76-
(["q"], [np.array([5.76998, 5.77501, 5.78004]), np.array([0.5, 0.5, 0.5]), "q"]),
77-
]
78-
79-
80-
@pytest.mark.parametrize("inputs, expected", params4)
81-
def test_compute_cve(inputs, expected, mocker):
82-
xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2])
83-
expected_cve = np.array([0.5, 0.5, 0.5])
84-
mocker.patch("numpy.interp", return_value=expected_cve)
85-
input_pattern = _instantiate_test_do(xarray, yarray)
86-
actual_cve_do = compute_cve(input_pattern, mud=1, method="polynomial_interpolation", xtype=inputs[0])
87-
expected_cve_do = _instantiate_test_do(
88-
xarray=expected[0],
89-
yarray=expected[1],
90-
xtype=expected[2],
91-
name="absorption correction, cve, for test",
95+
actual_cve_do = compute_cve(input_pattern, mud=1, method="polynomial_interpolation", xtype=input_xtype)
96+
expected_cve_do = DiffractionObject(
97+
xarray=expected["xarray"],
98+
yarray=expected["yarray"],
99+
xtype=expected["xtype"],
100+
wavelength=1.54,
92101
scat_quantity="cve",
102+
name="absorption correction, cve, for test",
103+
metadata={"thing1": 1, "thing2": "thing2"},
93104
)
94105
assert actual_cve_do == expected_cve_do
95106

96107

97-
params_cve_bad = [
98-
(
99-
[7, "polynomial_interpolation"],
100-
[
108+
@pytest.mark.parametrize(
109+
"inputs, msg",
110+
[
111+
(
112+
{"mud": 7, "method": "polynomial_interpolation"},
101113
f"mu*D is out of the acceptable range (0.5 to 6) for polynomial interpolation. "
102-
f"Please rerun with a value within this range or specifying another method from {*CVE_METHODS, }."
103-
],
104-
),
105-
([1, "invalid_method"], [f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }."]),
106-
([7, "invalid_method"], [f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }."]),
107-
]
108-
109-
110-
@pytest.mark.parametrize("inputs, msg", params_cve_bad)
114+
f"Please rerun with a value within this range or specifying another method from {*CVE_METHODS, }.",
115+
),
116+
(
117+
{"mud": 1, "method": "invalid_method"},
118+
f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }.",
119+
),
120+
(
121+
{"mud": 7, "method": "invalid_method"},
122+
f"Unknown method: invalid_method. Allowed methods are {*CVE_METHODS, }.",
123+
),
124+
],
125+
)
111126
def test_compute_cve_bad(mocker, inputs, msg):
112127
xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2])
113128
expected_cve = np.array([0.5, 0.5, 0.5])
114129
mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray)
115130
mocker.patch("numpy.interp", return_value=expected_cve)
116-
input_pattern = _instantiate_test_do(xarray, yarray)
117-
with pytest.raises(ValueError, match=re.escape(msg[0])):
118-
compute_cve(input_pattern, mud=inputs[0], method=inputs[1])
131+
input_pattern = DiffractionObject(
132+
xarray=xarray,
133+
yarray=yarray,
134+
xtype="tth",
135+
wavelength=1.54,
136+
scat_quantity="x-ray",
137+
name="test",
138+
metadata={"thing1": 1, "thing2": "thing2"},
139+
)
140+
with pytest.raises(ValueError, match=re.escape(msg)):
141+
compute_cve(input_pattern, mud=inputs["mud"], method=inputs["method"])
119142

120143

121144
def test_apply_corr(mocker):
122145
xarray, yarray = np.array([90, 90.1, 90.2]), np.array([2, 2, 2])
123146
expected_cve = np.array([0.5, 0.5, 0.5])
124147
mocker.patch("diffpy.labpdfproc.functions.TTH_GRID", xarray)
125148
mocker.patch("numpy.interp", return_value=expected_cve)
126-
input_pattern = _instantiate_test_do(xarray, yarray)
127-
absorption_correction = _instantiate_test_do(
149+
input_pattern = DiffractionObject(
150+
xarray=xarray,
151+
yarray=yarray,
152+
xtype="tth",
153+
wavelength=1.54,
154+
scat_quantity="x-ray",
155+
name="test",
156+
metadata={"thing1": 1, "thing2": "thing2"},
157+
)
158+
absorption_correction = DiffractionObject(
128159
xarray=xarray,
129160
yarray=expected_cve,
130-
name="absorption correction, cve, for test",
161+
xtype="tth",
162+
wavelength=1.54,
131163
scat_quantity="cve",
164+
name="absorption correction, cve, for test",
165+
metadata={"thing1": 1, "thing2": "thing2"},
132166
)
133167
actual_corr = apply_corr(input_pattern, absorption_correction)
134-
expected_corr = _instantiate_test_do(xarray, np.array([1, 1, 1]))
168+
expected_corr = DiffractionObject(
169+
xarray=xarray,
170+
yarray=np.array([1, 1, 1]),
171+
xtype="tth",
172+
wavelength=1.54,
173+
scat_quantity="x-ray",
174+
name="test",
175+
metadata={"thing1": 1, "thing2": "thing2"},
176+
)
135177
assert actual_corr == expected_corr

0 commit comments

Comments
 (0)