Skip to content

Commit 5ea9346

Browse files
Add tests of the getters and setters
1 parent 9adc26a commit 5ea9346

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

src/easydynamics/sample_model/components/voigt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def gaussian_width(self, value: Numeric):
143143
"""Set the gaussian_width parameter."""
144144
if not isinstance(value, Numeric):
145145
raise TypeError("width must be a number.")
146-
self._width.value = float(value)
146+
self._gaussian_width.value = float(value)
147147

148148
@property
149149
def lorentzian_width(self) -> Parameter:

tests/unit_tests/sample_model/components/test_delta_function.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22

33
import numpy as np
4-
from scipp import UnitError
54

65
from easydynamics.sample_model import DeltaFunction
76
from easyscience.variable import Parameter
@@ -40,6 +39,31 @@ def test_negative_area_warns(self):
4039
with pytest.warns(UserWarning, match="may not be physically meaningful"):
4140
DeltaFunction(name="TestDeltaFunction", area=-2.0, center=0.5, unit="meV")
4241

42+
def test_area_property_getter(self, delta_function: DeltaFunction):
43+
assert delta_function.area.value == 2.0
44+
45+
def test_area_property_setter(self, delta_function: DeltaFunction):
46+
# WHEN
47+
delta_function.area = 3.0
48+
49+
# THEN EXPECT
50+
assert delta_function.area.value == 3.0
51+
with pytest.raises(TypeError, match="area must be a number."):
52+
delta_function.area = "invalid"
53+
54+
def test_center_property_getter(self, delta_function: DeltaFunction):
55+
# WHEN THEN EXPECT
56+
assert delta_function.center.value == 0.5
57+
58+
def test_center_property_setter(self, delta_function: DeltaFunction):
59+
# WHEN
60+
delta_function.center = 0.6
61+
62+
# THEN EXPECT
63+
assert delta_function.center.value == 0.6
64+
with pytest.raises(TypeError, match="center must be a number."):
65+
delta_function.center = "invalid"
66+
4367
@pytest.mark.xfail(
4468
reason="DeltaFunction.evaluate is not implemented yet without resolution convolution"
4569
)

tests/unit_tests/sample_model/components/test_gaussian.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def test_area_property_setter(self, gaussian: Gaussian):
6464
gaussian.area = "invalid"
6565

6666
def test_center_property_getter(self, gaussian: Gaussian):
67+
# WHEN THEN EXPECT
6768
assert gaussian.center.value == 0.5
6869

6970
def test_center_property_setter(self, gaussian: Gaussian):

tests/unit_tests/sample_model/components/test_lorentzian.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,43 @@ def test_negative_area_warns(self):
5757
name="TestLorentzian", area=-2.0, center=0.5, width=0.6, unit="meV"
5858
)
5959

60+
def test_area_property_getter(self, lorentzian: Lorentzian):
61+
assert lorentzian.area.value == 2.0
62+
63+
def test_area_property_setter(self, lorentzian: Lorentzian):
64+
# WHEN
65+
lorentzian.area = 3.0
66+
67+
# THEN EXPECT
68+
assert lorentzian.area.value == 3.0
69+
with pytest.raises(TypeError, match="area must be a number."):
70+
lorentzian.area = "invalid"
71+
72+
def test_center_property_getter(self, lorentzian: Lorentzian):
73+
# WHEN THEN EXPECT
74+
assert lorentzian.center.value == 0.5
75+
76+
def test_center_property_setter(self, lorentzian: Lorentzian):
77+
# WHEN
78+
lorentzian.center = 0.6
79+
80+
# THEN EXPECT
81+
assert lorentzian.center.value == 0.6
82+
with pytest.raises(TypeError, match="center must be a number."):
83+
lorentzian.center = "invalid"
84+
85+
def test_width_property_getter(self, lorentzian: Lorentzian):
86+
assert lorentzian.width.value == 0.6
87+
88+
def test_width_property_setter(self, lorentzian: Lorentzian):
89+
# WHEN
90+
lorentzian.width = 0.7
91+
92+
# THEN EXPECT
93+
assert lorentzian.width.value == 0.7
94+
with pytest.raises(TypeError, match="width must be a number."):
95+
lorentzian.width = "invalid"
96+
6097
def test_evaluate(self, lorentzian: Lorentzian):
6198
# WHEN
6299
x = np.array([0.0, 0.5, 1.0])

tests/unit_tests/sample_model/components/test_voigt.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from scipy.special import voigt_profile
1414

15+
from easydynamics.sample_model.components.lorentzian import Lorentzian
16+
1517

1618
class TestVoigt:
1719
@pytest.fixture
@@ -123,6 +125,56 @@ def test_negative_area_warns(self):
123125
unit="meV",
124126
)
125127

128+
def test_area_property_getter(self, voigt: Voigt):
129+
assert voigt.area.value == 2.0
130+
131+
def test_area_property_setter(self, voigt: Voigt):
132+
# WHEN
133+
voigt.area = 3.0
134+
135+
# THEN EXPECT
136+
assert voigt.area.value == 3.0
137+
with pytest.raises(TypeError, match="area must be a number."):
138+
voigt.area = "invalid"
139+
140+
def test_center_property_getter(self, voigt: Voigt):
141+
# WHEN THEN EXPECT
142+
assert voigt.center.value == 0.5
143+
144+
def test_center_property_setter(self, voigt: Voigt):
145+
# WHEN
146+
voigt.center = 0.6
147+
148+
# THEN EXPECT
149+
assert voigt.center.value == 0.6
150+
with pytest.raises(TypeError, match="center must be a number."):
151+
voigt.center = "invalid"
152+
153+
def test_gaussian_width_property_getter(self, voigt: Voigt):
154+
# WHEN THEN EXPECT
155+
assert voigt.gaussian_width.value == 0.6
156+
157+
def test_gaussian_width_property_setter(self, voigt: Voigt):
158+
# WHEN
159+
voigt.gaussian_width = 0.7
160+
161+
# THEN EXPECT
162+
assert voigt.gaussian_width.value == 0.7
163+
164+
def test_lorentzian_width_property_getter(self, voigt: Voigt):
165+
# WHEN THEN EXPECT
166+
assert voigt.lorentzian_width.value == 0.7
167+
168+
def test_lorentzian_width_property_setter(self, voigt: Voigt):
169+
# WHEN
170+
voigt.lorentzian_width = 0.8
171+
172+
# THEN EXPECT
173+
assert voigt.lorentzian_width.value == 0.8
174+
175+
with pytest.raises(TypeError, match="width must be a number."):
176+
voigt.lorentzian_width = "invalid"
177+
126178
def test_evaluate(self, voigt: Voigt):
127179
# WHEN
128180
x = np.array([0.0, 0.5, 1.0])

0 commit comments

Comments
 (0)