Skip to content

Commit 73d8ada

Browse files
committed
Refactor transform tests, pass variables to parameters
1 parent 5d88cf6 commit 73d8ada

File tree

2 files changed

+77
-70
lines changed

2 files changed

+77
-70
lines changed

tests/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ def _load(filename):
3737
def do_minimal_tth():
3838
return DiffractionObject(wavelength=2 * np.pi, xarray=np.array([30, 60]), yarray=np.array([1, 2]), xtype="tth")
3939

40+
4041
@pytest.fixture
4142
def wavelength_warning_msg():
4243
return (
43-
"No wavelength has been specified. You can continue to use the DiffractionObject, but "
44-
"some of its powerful features will not be available. "
45-
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), "
46-
"you may set do.wavelength = 1.54 for a wavelength of 1.54 angstroms."
47-
)
44+
"No wavelength has been specified. You can continue to use the DiffractionObject, but "
45+
"some of its powerful features will not be available. "
46+
"To specify a wavelength, if you have do = DiffractionObject(xarray, yarray, 'tth'), "
47+
"you may set do.wavelength = 1.54 for a wavelength of 1.54 angstroms."
48+
)

tests/test_transforms.py

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from diffpy.utils.transforms import d_to_q, d_to_tth, q_to_d, q_to_tth, tth_to_d, tth_to_q
77

8-
98
params_q_to_tth = [
109
# UC1: Empty q values, no wavelength, return empty arrays
1110
(None, np.empty((0)), np.empty((0))),
@@ -35,7 +34,7 @@ def test_q_to_tth(wavelength, q, expected_tth, wavelength_warning_msg):
3534
assert np.allclose(expected_tth, actual_tth)
3635

3736

38-
params_q_to_tth_bad = [
37+
test_q_to_tth_bad_params = [
3938
# UC1: user specified invalid q values that result in tth > 180 degrees
4039
(
4140
4 * np.pi,
@@ -55,7 +54,7 @@ def test_q_to_tth(wavelength, q, expected_tth, wavelength_warning_msg):
5554
]
5655

5756

58-
@pytest.mark.parametrize("q, wavelength, expected_error_type, expected_error_msg", params_q_to_tth_bad)
57+
@pytest.mark.parametrize("q, wavelength, expected_error_type, expected_error_msg", test_q_to_tth_bad_params)
5958
def test_q_to_tth_bad(q, wavelength, expected_error_type, expected_error_msg):
6059
with pytest.raises(expected_error_type, match=expected_error_msg):
6160
q_to_tth(wavelength, q)
@@ -81,17 +80,19 @@ def test_q_to_tth_bad(q, wavelength, expected_error_type, expected_error_msg):
8180
),
8281
]
8382

83+
8484
@pytest.mark.parametrize("wavelength, tth, expected_q", test_tth_t_q_params)
8585
def test_tth_to_q(wavelength, tth, expected_q, wavelength_warning_msg):
8686
if wavelength is None:
8787
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_msg)):
8888
actual_q = tth_to_q(tth, wavelength)
8989
else:
9090
actual_q = tth_to_q(tth, wavelength)
91-
91+
9292
assert np.allclose(actual_q, expected_q)
9393

94-
tth_to_q_bad_params = [
94+
95+
test_tth_to_q_bad_params = [
9596
# UC0: user specified an invalid tth value of > 180 degrees (without wavelength)
9697
(
9798
None,
@@ -109,138 +110,143 @@ def test_tth_to_q(wavelength, tth, expected_q, wavelength_warning_msg):
109110
]
110111

111112

112-
@pytest.mark.parametrize("wavelength, tth, expected_error_type, expected_error_msg", tth_to_q_bad_params)
113+
@pytest.mark.parametrize("wavelength, tth, expected_error_type, expected_error_msg", test_tth_to_q_bad_params)
113114
def test_tth_to_q_bad(wavelength, tth, expected_error_type, expected_error_msg):
114115
with pytest.raises(expected_error_type, match=expected_error_msg):
115116
tth_to_q(tth, wavelength)
116117

117118

118-
params_q_to_d = [
119+
test_q_to_d_params = [
119120
# UC1: User specified empty q values
120-
([np.array([])], np.array([])),
121+
(np.array([]), np.array([])),
121122
# UC2: User specified valid q values
122123
(
123-
[np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi])],
124+
np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi]),
124125
np.array([np.inf, 2, 1, 0.66667, 0.5, 0.4]),
125126
),
126127
]
127128

128129

129-
@pytest.mark.parametrize("inputs, expected", params_q_to_d)
130-
def test_q_to_d(inputs, expected):
131-
actual = q_to_d(inputs[0])
132-
assert np.allclose(actual, expected)
130+
@pytest.mark.parametrize("q, expected_d", test_q_to_d_params)
131+
def test_q_to_d(q, expected_d):
132+
actual_d = q_to_d(q)
133+
assert np.allclose(actual_d, expected_d)
133134

134135

135-
params_d_to_q = [
136+
test_d_to_q_params = [
136137
# UC1: User specified empty d values
137-
([np.array([])], np.array([])),
138+
(np.array([]), np.array([])),
138139
# UC2: User specified valid d values
139140
(
140-
[np.array([5 * np.pi, 4 * np.pi, 3 * np.pi, 2 * np.pi, np.pi, 0])],
141+
np.array([5 * np.pi, 4 * np.pi, 3 * np.pi, 2 * np.pi, np.pi, 0]),
141142
np.array([0.4, 0.5, 0.66667, 1, 2, np.inf]),
142143
),
143144
]
144145

145146

146-
@pytest.mark.parametrize("inputs, expected", params_d_to_q)
147-
def test_d_to_q(inputs, expected):
148-
actual = d_to_q(inputs[0])
149-
assert np.allclose(actual, expected)
147+
@pytest.mark.parametrize("d, expected_q", test_d_to_q_params)
148+
def test_d_to_q(d, expected_q):
149+
actual_q = d_to_q(d)
150+
assert np.allclose(actual_q, expected_q)
150151

151152

152-
params_tth_to_d = [
153+
test_tth_to_d_params = [
153154
# UC0: User specified empty tth values (without wavelength)
154-
([None, np.array([])], np.array([])),
155+
(None, np.array([]), np.array([])),
155156
# UC1: User specified empty tth values (with wavelength)
156-
([4 * np.pi, np.array([])], np.array([])),
157+
(4 * np.pi, np.array([]), np.array([])),
157158
# UC2: User specified valid tth values between 0-180 degrees (without wavelength)
158159
(
159-
[None, np.array([0, 30, 60, 90, 120, 180])],
160+
None,
161+
np.array([0, 30, 60, 90, 120, 180]),
160162
np.array([0, 1, 2, 3, 4, 5]),
161163
),
162164
# UC3: User specified valid tth values between 0-180 degrees (with wavelength)
163165
(
164-
[4 * np.pi, np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0])],
166+
4 * np.pi,
167+
np.array([0, 30.0, 60.0, 90.0, 120.0, 180.0]),
165168
np.array([np.inf, 24.27636, 12.56637, 8.88577, 7.25520, 6.28319]),
166169
),
167170
]
168171

169172

170-
@pytest.mark.parametrize("inputs, expected", params_tth_to_d)
171-
def test_tth_to_d(inputs, expected):
172-
actual = tth_to_d(inputs[1], inputs[0])
173-
assert np.allclose(actual, expected)
173+
@pytest.mark.parametrize("wavelength, tth, expected_d", test_tth_to_d_params)
174+
def test_tth_to_d(wavelength, tth, expected_d):
175+
actual_d = tth_to_d(tth, wavelength)
176+
assert np.allclose(actual_d, expected_d)
174177

175178

176-
params_tth_to_d_bad = [
179+
test_tth_to_d_invalid_params = [
177180
# UC1: user specified an invalid tth value of > 180 degrees (without wavelength)
178181
(
179-
[None, np.array([0, 30, 60, 90, 120, 181])],
180-
[ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."],
182+
None,
183+
np.array([0, 30, 60, 90, 120, 181]),
184+
ValueError,
185+
"Two theta exceeds 180 degrees. Please check the input values for errors.",
181186
),
182187
# UC2: user specified an invalid tth value of > 180 degrees (with wavelength)
183188
(
184-
[4 * np.pi, np.array([0, 30, 60, 90, 120, 181])],
185-
[ValueError, "Two theta exceeds 180 degrees. Please check the input values for errors."],
189+
4 * np.pi,
190+
np.array([0, 30, 60, 90, 120, 181]),
191+
ValueError,
192+
"Two theta exceeds 180 degrees. Please check the input values for errors.",
186193
),
187194
]
188195

189196

190-
@pytest.mark.parametrize("inputs, expected", params_tth_to_d_bad)
191-
def test_tth_to_d_bad(inputs, expected):
192-
with pytest.raises(expected[0], match=expected[1]):
193-
tth_to_d(inputs[1], inputs[0])
197+
@pytest.mark.parametrize("wavelength, tth, expected_error_type, expected_error_msg", test_tth_to_d_invalid_params)
198+
def test_tth_to_d_invalid(wavelength, tth, expected_error_type, expected_error_msg):
199+
with pytest.raises(expected_error_type, match=expected_error_msg):
200+
tth_to_d(tth, wavelength)
194201

195202

196-
params_d_to_tth = [
203+
test_d_to_tth_params = [
197204
# UC1: Empty d values, no wavelength, return empty arrays
198-
([None, np.empty((0))], np.empty((0))),
205+
(None, np.empty((0)), np.empty((0))),
199206
# UC2: Empty d values, wavelength specified, return empty arrays
200-
([4 * np.pi, np.empty((0))], np.empty(0)),
207+
(4 * np.pi, np.empty((0)), np.empty(0)),
201208
# UC3: User specified valid d values, no wavelength, return empty arrays
202209
(
203-
[None, np.array([1, 0.8, 0.6, 0.4, 0.2, 0])],
210+
None,
211+
np.array([1, 0.8, 0.6, 0.4, 0.2, 0]),
204212
np.array([0, 1, 2, 3, 4, 5]),
205213
),
206214
# UC4: User specified valid d values (with wavelength)
207215
(
208-
[4 * np.pi, np.array([4 * np.pi, 4 / np.sqrt(2) * np.pi, 4 / np.sqrt(3) * np.pi])],
216+
4 * np.pi,
217+
np.array([4 * np.pi, 4 / np.sqrt(2) * np.pi, 4 / np.sqrt(3) * np.pi]),
209218
np.array([60.0, 90.0, 120.0]),
210219
),
211220
]
212221

213222

214-
@pytest.mark.parametrize("inputs, expected", params_d_to_tth)
215-
def test_d_to_tth(inputs, expected):
216-
actual = d_to_tth(inputs[1], inputs[0])
223+
@pytest.mark.parametrize("wavelength, d, expected_tth", test_d_to_tth_params)
224+
def test_d_to_tth(wavelength, d, expected_tth):
225+
actual_tth = d_to_tth(d, wavelength)
226+
assert np.allclose(actual_tth, expected_tth)
217227

218-
assert np.allclose(expected, actual)
219228

220-
221-
params_d_to_tth_bad = [
229+
test_d_to_tth_bad_params = [
222230
# UC1: user specified invalid d values that result in tth > 180 degrees
223231
(
224-
[4 * np.pi, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2])],
225-
[
226-
ValueError,
227-
"The supplied input array and wavelength will result in an impossible two-theta. "
228-
"Please check these values and re-instantiate the DiffractionObject with correct values.",
229-
],
232+
4 * np.pi,
233+
np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2]),
234+
ValueError,
235+
"The supplied input array and wavelength will result in an impossible two-theta. "
236+
"Please check these values and re-instantiate the DiffractionObject with correct values.",
230237
),
231238
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
232239
(
233-
[100, np.array([1, 0.8, 0.6, 0.4, 0.2, 0])],
234-
[
235-
ValueError,
236-
"The supplied input array and wavelength will result in an impossible two-theta. "
237-
"Please check these values and re-instantiate the DiffractionObject with correct values.",
238-
],
240+
100,
241+
np.array([1, 0.8, 0.6, 0.4, 0.2, 0]),
242+
ValueError,
243+
"The supplied input array and wavelength will result in an impossible two-theta. "
244+
"Please check these values and re-instantiate the DiffractionObject with correct values.",
239245
),
240246
]
241247

242248

243-
@pytest.mark.parametrize("inputs, expected", params_d_to_tth_bad)
244-
def test_d_to_tth_bad(inputs, expected):
245-
with pytest.raises(expected[0], match=expected[1]):
246-
d_to_tth(inputs[1], inputs[0])
249+
@pytest.mark.parametrize("wavelength, d, expected_error_type, expected_error_msg", test_d_to_tth_bad_params)
250+
def test_d_to_tth_bad(wavelength, d, expected_error_type, expected_error_msg):
251+
with pytest.raises(expected_error_type, match=expected_error_msg):
252+
d_to_tth(d, wavelength)

0 commit comments

Comments
 (0)