Skip to content

Commit 9adc26a

Browse files
Add WHEN THEN EXPECT to tests
1 parent f89a218 commit 9adc26a

File tree

4 files changed

+125
-25
lines changed

4 files changed

+125
-25
lines changed

tests/unit_tests/sample_model/components/test_delta_function.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,50 @@ def test_negative_area_warns(self):
4444
reason="DeltaFunction.evaluate is not implemented yet without resolution convolution"
4545
)
4646
def test_evaluate(self, delta_function: DeltaFunction):
47+
# WHEN
4748
x = np.array([0.0, 0.5, 1.0])
48-
expected = delta_function.evaluate(x)
49+
50+
# THEN
51+
result = delta_function.evaluate(x)
52+
53+
# EXPECT
4954
expected_result = np.zeros_like(x)
50-
# expected_result[x == 0.5] = 2.0
51-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
55+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
5256

5357
def test_center_is_fixed_if_set_to_None(self):
58+
# WHEN THEN
5459
test_delta = DeltaFunction(
5560
name="TestDeltaFunction", area=2.0, center=None, unit="meV"
5661
)
62+
63+
# EXPECT
5764
assert test_delta._center.value == 0.0
5865
assert test_delta._center.fixed is True
5966

6067
def test_get_parameters(self, delta_function: DeltaFunction):
68+
# WHEN THEN
6169
params = delta_function.get_parameters()
70+
71+
# EXPECT
6272
assert len(params) == 2
6373
assert params[0].name == "TestDeltaFunction area"
6474
assert params[1].name == "TestDeltaFunction center"
6575
assert all(isinstance(param, Parameter) for param in params)
6676

6777
def test_convert_unit(self, delta_function: DeltaFunction):
78+
# WHEN THEN
6879
delta_function.convert_unit("microeV")
6980

81+
# EXPECT
7082
assert delta_function.unit == "microeV"
7183
assert delta_function._area.value == 2 * 1e3
7284
assert delta_function._center.value == 0.5 * 1e3
7385

7486
def test_copy(self, delta_function: DeltaFunction):
87+
# WHEN THEN
7588
delta_copy = delta_function.copy()
89+
90+
# EXPECT
7691
assert delta_copy is not delta_function
7792
assert delta_copy.name == "copy of " + delta_function.name
7893

@@ -85,7 +100,10 @@ def test_copy(self, delta_function: DeltaFunction):
85100
assert delta_copy.unit == delta_function.unit
86101

87102
def test_repr(self, delta_function: DeltaFunction):
103+
# WHEN THEN
88104
repr_str = repr(delta_function)
105+
106+
# EXPECT
89107
assert "DeltaFunction" in repr_str
90108
assert "name = TestDeltaFunction" in repr_str
91109
assert "unit = meV" in repr_str

tests/unit_tests/sample_model/components/test_lorentzian.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,42 +58,66 @@ def test_negative_area_warns(self):
5858
)
5959

6060
def test_evaluate(self, lorentzian: Lorentzian):
61+
# WHEN
6162
x = np.array([0.0, 0.5, 1.0])
62-
expected = lorentzian.evaluate(x)
63+
64+
# THEN
65+
result = lorentzian.evaluate(x)
66+
67+
# EXPECT
6368
expected_result = (2.0 / (np.pi * 0.6)) / (1 + ((x - 0.5) / 0.6) ** 2)
64-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
69+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
6570

6671
def test_evaluate_scipp_array(self, lorentzian: Lorentzian):
72+
# WHEN
6773
x = sc.array(dims=["x"], values=[0.0, 0.5, 1.0], unit="meV")
68-
expected = lorentzian.evaluate(x)
74+
75+
# THEN
76+
result = lorentzian.evaluate(x)
77+
78+
# EXPECT
6979
expected_result = (2.0 / (np.pi * 0.6)) / (1 + ((x.values - 0.5) / 0.6) ** 2)
70-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
80+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
7181

7282
def test_evaluate_with_different_unit(self, lorentzian: Lorentzian):
83+
# WHEN
7384
x = sc.array(dims=["x"], values=[0.0, 500.0, 1000.0], unit="microeV")
74-
expected = lorentzian.evaluate(x)
85+
86+
# THEN
87+
result = lorentzian.evaluate(x)
88+
89+
# EXPECT
7590
expected_result = (2.0 * 1e3 / (np.pi * 0.6 * 1e3)) / (
7691
1 + ((x.values - 0.5 * 1e3) / (0.6 * 1e3)) ** 2
7792
)
78-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
93+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
7994

8095
def test_evaluate_with_incompatible_unit(self, lorentzian: Lorentzian):
96+
# WHEN
8197
x = sc.array(dims=["x"], values=[0.0, 500.0, 1000.0], unit="nm")
98+
99+
# THEN EXPECT
82100
with pytest.raises(
83101
UnitError,
84102
match="Input x has unit nm, but Lorentzian component has unit meV. Failed to convert Lorentzian to nm.",
85103
):
86104
lorentzian.evaluate(x)
87105

88106
def test_center_is_fixed_if_set_to_None(self):
107+
# WHEN
89108
test_lorentzian = Lorentzian(
90109
name="TestLorentzian", area=2.0, center=None, width=0.6, unit="meV"
91110
)
111+
112+
# THEN EXPECT
92113
assert test_lorentzian._center.value == 0.0
93114
assert test_lorentzian._center.fixed is True
94115

95116
def test_get_parameters(self, lorentzian: Lorentzian):
117+
# WHEN THEN
96118
params = lorentzian.get_parameters()
119+
120+
# EXPECT
97121
assert len(params) == 3
98122
assert params[0].name == "TestLorentzian area"
99123
assert params[1].name == "TestLorentzian center"
@@ -114,15 +138,20 @@ def test_area_matches_parameter(self, lorentzian: Lorentzian):
114138
assert numerical_area == pytest.approx(lorentzian._area.value, rel=2e-3)
115139

116140
def test_convert_unit(self, lorentzian: Lorentzian):
141+
# WHEN THEN
117142
lorentzian.convert_unit("microeV")
118143

144+
# EXPECT
119145
assert lorentzian.unit == "microeV"
120146
assert lorentzian._area.value == 2 * 1e3
121147
assert lorentzian._center.value == 0.5 * 1e3
122148
assert lorentzian._width.value == 0.6 * 1e3
123149

124150
def test_copy(self, lorentzian: Lorentzian):
151+
# WHEN THEN
125152
lorentzian_copy = lorentzian.copy()
153+
154+
# EXPECT
126155
assert lorentzian_copy is not lorentzian
127156
assert lorentzian_copy.name == "copy of " + lorentzian.name
128157

@@ -138,7 +167,10 @@ def test_copy(self, lorentzian: Lorentzian):
138167
assert lorentzian_copy.unit == lorentzian.unit
139168

140169
def test_repr(self, lorentzian: Lorentzian):
170+
# WHEN THEN
141171
repr_str = repr(lorentzian)
172+
173+
# EXPECT
142174
assert "Lorentzian" in repr_str
143175
assert "name = TestLorentzian" in repr_str
144176
assert "unit = meV" in repr_str

tests/unit_tests/sample_model/components/test_polynomial.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,66 @@ def test_negative_value_warns_in_evaluate(self):
4343
test_polynomial.evaluate(np.array([0.0, 1.0, 2.0]))
4444

4545
def test_evaluate(self, polynomial: Polynomial):
46+
# WHEN
4647
x = np.array([0.0, 1.0, 2.0])
47-
expected = polynomial.evaluate(x)
48+
49+
# THEN
50+
result = polynomial.evaluate(x)
51+
52+
# EXPECT
4853
expected_result = 1.0 - 2.0 * x + 3.0 * x**2
49-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
54+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
5055

5156
def test_evaluate_scipp_array(self, polynomial: Polynomial):
57+
# WHEN
5258
x = sc.array(dims=["x"], values=[0.0, 1.0, 2.0], unit="meV")
53-
expected = polynomial.evaluate(x)
59+
60+
# THEN
61+
result = polynomial.evaluate(x)
62+
63+
# EXPECT
5464
expected_result = 1.0 - 2.0 * x.values + 3.0 * x.values**2
55-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
65+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
5666

5767
def test_evaluate_with_different_unit_error(self, polynomial: Polynomial):
68+
# WHEN
5869
x = sc.array(dims=["x"], values=[0.0, 1.0, 2.0], unit="microeV")
5970

71+
# THEN EXPECT
6072
with pytest.raises(
6173
UnitError,
6274
match="Change the unit of the Polynomial and try again",
6375
):
6476
polynomial.evaluate(x)
6577

6678
def test_degree(self, polynomial: Polynomial):
79+
# WHEN THEN EXPECT
6780
assert polynomial.degree() == 2
6881

6982
def test_get_parameters(self, polynomial: Polynomial):
83+
# WHEN THEN
7084
params = polynomial.get_parameters()
85+
86+
# EXPECT
7187
assert len(params) == 3
7288
assert params[0].name == "TestPolynomial_c0"
7389
assert params[1].name == "TestPolynomial_c1"
7490
assert params[2].name == "TestPolynomial_c2"
7591
assert all(isinstance(param, Parameter) for param in params)
7692

7793
def test_convert_unit_raises_for_polynomial(self, polynomial):
94+
# WHEN THEN EXPECT
7895
with pytest.raises(
7996
NotImplementedError,
8097
match="Unit conversion is not implemented for Polynomial components. The automatic unit converter does not like powers of units.",
8198
):
8299
polynomial.convert_unit("eV")
83100

84101
def test_copy(self, polynomial: Polynomial):
102+
# WHEN THEN
85103
polynomial_copy = polynomial.copy()
104+
105+
# EXPECT
86106
assert polynomial_copy is not polynomial
87107
assert polynomial_copy.name == polynomial.name
88108
assert len(polynomial_copy.coefficients) == len(polynomial.coefficients)
@@ -93,13 +113,10 @@ def test_copy(self, polynomial: Polynomial):
93113
assert copied_coeff.fixed == original_coeff.fixed
94114

95115
def test_repr(self, polynomial: Polynomial):
116+
# WHEN THEN
96117
repr_str = repr(polynomial)
118+
119+
# EXPECT
97120
assert "Polynomial" in repr_str
98121
assert "name = TestPolynomial" in repr_str
99122
assert "coefficients =" in repr_str
100-
101-
102-
# @pytest.mark.skip(reason="UserDefinedComponent not implemented yet")
103-
# class TestUserDefinedComponent:
104-
# def test_placeholder(self):
105-
# pass

tests/unit_tests/sample_model/components/test_voigt.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def test_input_type_validation_raises(self):
8383
)
8484

8585
def test_negative_gaussian_width_raises(self):
86+
# WHEN THEN EXPECT
8687
with pytest.raises(
8788
ValueError, match="The gaussian_width of a Voigt must be greater than."
8889
):
@@ -96,6 +97,7 @@ def test_negative_gaussian_width_raises(self):
9697
)
9798

9899
def test_negative_lorentzian_width_raises(self):
100+
# WHEN THEN EXPECT
99101
with pytest.raises(
100102
ValueError,
101103
match="The lorentzian_width of a Voigt must be greater than zero.",
@@ -110,6 +112,7 @@ def test_negative_lorentzian_width_raises(self):
110112
)
111113

112114
def test_negative_area_warns(self):
115+
# WHEN THEN EXPECT
113116
with pytest.warns(UserWarning, match="may not be physically meaningful"):
114117
Voigt(
115118
name="TestVoigt",
@@ -121,26 +124,42 @@ def test_negative_area_warns(self):
121124
)
122125

123126
def test_evaluate(self, voigt: Voigt):
127+
# WHEN
124128
x = np.array([0.0, 0.5, 1.0])
125-
expected = voigt.evaluate(x)
129+
130+
# THEN
131+
result = voigt.evaluate(x)
132+
133+
# EXPECT
126134
expected_result = 2.0 * voigt_profile(x - 0.5, 0.6, 0.7)
127-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
135+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
128136

129137
def test_evaluate_scipp_array(self, voigt: Voigt):
138+
# WHEN
130139
x = sc.array(dims=["x"], values=[0.0, 0.5, 1.0], unit="meV")
131-
expected = voigt.evaluate(x)
140+
141+
# THEN
142+
result = voigt.evaluate(x)
143+
144+
# EXPECT
132145
expected_result = 2.0 * voigt_profile(x.values - 0.5, 0.6, 0.7)
133-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
146+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
134147

135148
def test_evaluate_with_different_unit(self, voigt: Voigt):
149+
# WHEN
136150
x = sc.array(dims=["x"], values=[0.0, 500.0, 1000.0], unit="microeV")
137-
expected = voigt.evaluate(x)
151+
152+
# THEN
153+
result = voigt.evaluate(x)
154+
155+
# EXPECT
138156
expected_result = (
139157
2.0 * 1e3 * voigt_profile(x.values - 0.5 * 1e3, 0.6 * 1e3, 0.7 * 1e3)
140158
)
141-
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
159+
np.testing.assert_allclose(result, expected_result, rtol=1e-5)
142160

143161
def test_evaluate_with_incompatible_unit(self, voigt: Voigt):
162+
# WHEN THEN EXPECT
144163
x = sc.array(dims=["x"], values=[0.0, 500.0, 1000.0], unit="nm")
145164
with pytest.raises(
146165
UnitError,
@@ -149,6 +168,7 @@ def test_evaluate_with_incompatible_unit(self, voigt: Voigt):
149168
voigt.evaluate(x)
150169

151170
def test_center_is_fixed_if_set_to_None(self):
171+
# WHEN THEN
152172
test_voigt = Voigt(
153173
name="TestVoigt",
154174
area=2.0,
@@ -157,20 +177,27 @@ def test_center_is_fixed_if_set_to_None(self):
157177
lorentzian_width=0.7,
158178
unit="meV",
159179
)
180+
181+
# EXPECT
160182
assert test_voigt._center.value == 0.0
161183
assert test_voigt._center.fixed is True
162184

163185
def test_convert_unit(self, voigt: Voigt):
186+
# WHEN THEN
164187
voigt.convert_unit("microeV")
165188

189+
# EXPECT
166190
assert voigt.unit == "microeV"
167191
assert voigt._area.value == 2 * 1e3
168192
assert voigt._center.value == 0.5 * 1e3
169193
assert voigt._gaussian_width.value == 0.6 * 1e3
170194
assert voigt._lorentzian_width.value == 0.7 * 1e3
171195

172196
def test_get_parameters(self, voigt: Voigt):
197+
# WHEN THEN
173198
params = voigt.get_parameters()
199+
200+
# EXPECT
174201
assert len(params) == 4
175202
assert params[0].name == "TestVoigt area"
176203
assert params[1].name == "TestVoigt center"
@@ -196,7 +223,10 @@ def test_area_matches_parameter(self, voigt: Voigt):
196223
assert numerical_area == pytest.approx(voigt._area.value, rel=2e-3)
197224

198225
def test_copy(self, voigt: Voigt):
226+
# WHEN THEN
199227
voigt_copy = voigt.copy()
228+
229+
# EXPECT
200230
assert voigt_copy is not voigt
201231
assert voigt_copy.name == "copy of " + voigt.name
202232

@@ -215,7 +245,10 @@ def test_copy(self, voigt: Voigt):
215245
assert voigt_copy.unit == voigt.unit
216246

217247
def test_repr(self, voigt: Voigt):
248+
# WHEN THEN
218249
repr_str = repr(voigt)
250+
251+
# EXPECT
219252
assert "Voigt" in repr_str
220253
assert "name = TestVoigt" in repr_str
221254
assert "unit = meV" in repr_str

0 commit comments

Comments
 (0)