22
33import numpy as np
44import scipp as sc
5+ from scipp import UnitError
56
67from scipy .integrate import simpson
78
@@ -18,14 +19,14 @@ def dho(self):
1819 )
1920
2021 def test_initialization (self , dho : DampedHarmonicOscillator ):
21- assert dho .name == "TestDHO"
22- assert dho .area .value == 2.0
23- assert dho .center .value == 1.5
24- assert dho .width .value == 0.3
25- assert dho .unit == "meV"
22+ assert dho ._name == "TestDHO"
23+ assert dho ._area .value == 2.0
24+ assert dho ._center .value == 1.5
25+ assert dho ._width .value == 0.3
26+ assert dho ._unit == "meV"
2627
2728 def test_input_type_validation_raises (self ):
28- with pytest .raises (TypeError , match = "area must be a number or a Parameter " ):
29+ with pytest .raises (TypeError , match = "area must be a number" ):
2930 DampedHarmonicOscillator (
3031 name = "TestDampedHarmonicOscillator" ,
3132 area = "invalid" ,
@@ -34,7 +35,7 @@ def test_input_type_validation_raises(self):
3435 unit = "meV" ,
3536 )
3637
37- with pytest .raises (TypeError , match = "center must be a number or a Parameter " ):
38+ with pytest .raises (TypeError , match = "center must be a number" ):
3839 DampedHarmonicOscillator (
3940 name = "TestDampedHarmonicOscillator" ,
4041 area = 2.0 ,
@@ -43,7 +44,7 @@ def test_input_type_validation_raises(self):
4344 unit = "meV" ,
4445 )
4546
46- with pytest .raises (TypeError , match = "width must be a number or a Parameter " ):
47+ with pytest .raises (TypeError , match = "width must be a number" ):
4748 DampedHarmonicOscillator (
4849 name = "TestDampedHarmonicOscillator" ,
4950 area = 2.0 ,
@@ -74,21 +75,6 @@ def test_negative_width_raises(self):
7475 unit = "meV" ,
7576 )
7677
77- def test_negative_width_raises_in_evaluate (self ):
78- test_dho = DampedHarmonicOscillator (
79- name = "TestDampedHarmonicOscillator" ,
80- area = 2.0 ,
81- center = 0.5 ,
82- width = 0.6 ,
83- unit = "meV" ,
84- )
85- test_dho .width .value = - 0.6
86- with pytest .raises (
87- ValueError ,
88- match = "The width of a DampedHarmonicOscillator must be greater than zero." ,
89- ):
90- test_dho .evaluate (np .array ([0.0 , 1.5 , 3.0 ]))
91-
9278 def test_negative_area_warns (self ):
9379 with pytest .warns (UserWarning , match = "may not be physically meaningful" ):
9480 DampedHarmonicOscillator (
@@ -99,18 +85,6 @@ def test_negative_area_warns(self):
9985 unit = "meV" ,
10086 )
10187
102- def test_negative_area_warns_in_evaluate (self ):
103- test_dho = DampedHarmonicOscillator (
104- name = "TestDampedHarmonicOscillator" ,
105- area = 2.0 ,
106- center = 0.5 ,
107- width = 0.6 ,
108- unit = "meV" ,
109- )
110- test_dho .area .value = - 2.0
111- with pytest .warns (UserWarning , match = "may not be physically meaningful" ):
112- test_dho .evaluate (np .array ([0.0 , 1.5 , 3.0 ]))
113-
11488 def test_evaluate (self , dho : DampedHarmonicOscillator ):
11589 x = np .array ([0.0 , 1.5 , 3.0 ])
11690 expected = dho .evaluate (x )
@@ -151,20 +125,13 @@ def test_evaluate_with_different_unit(self, dho: DampedHarmonicOscillator):
151125 )
152126 np .testing .assert_allclose (expected , expected_result , rtol = 1e-5 )
153127
154- def test_input_as_parameter (self ):
155- param_area = Parameter (name = "area_param" , value = 2.0 , unit = "meV" )
156- param_center = Parameter (name = "center_param" , value = 0.5 , unit = "meV" )
157- param_width = Parameter (name = "width_param" , value = 0.6 , unit = "meV" )
158- test_dho = DampedHarmonicOscillator (
159- name = "TestDHO" ,
160- area = param_area ,
161- center = param_center ,
162- width = param_width ,
163- unit = "meV" ,
164- )
165- assert test_dho .area == param_area
166- assert test_dho .center == param_center
167- assert test_dho .width == param_width
128+ def test_evaluate_with_incompatible_unit (self , dho : DampedHarmonicOscillator ):
129+ x = sc .array (dims = ["x" ], values = [0.0 , 500.0 , 1000.0 ], unit = "nm" )
130+ with pytest .raises (
131+ UnitError ,
132+ match = "Input x has unit nm, but DampedHarmonicOscillator component has unit meV. Failed to convert DampedHarmonicOscillator to nm." ,
133+ ):
134+ dho .evaluate (x )
168135
169136 def test_get_parameters (self , dho : DampedHarmonicOscillator ):
170137 params = dho .get_parameters ()
@@ -177,39 +144,39 @@ def test_get_parameters(self, dho: DampedHarmonicOscillator):
177144 def test_area_matches_parameter (self , dho : DampedHarmonicOscillator ):
178145 # WHEN
179146 x = np .linspace (
180- - dho .center .value - 20 * dho .width .value ,
181- dho .center .value + 20 * dho .width .value ,
147+ - dho ._center .value - 20 * dho ._width .value ,
148+ dho ._center .value + 20 * dho ._width .value ,
182149 5000 ,
183150 )
184151 y = dho .evaluate (x )
185152 numerical_area = simpson (y , x )
186153
187154 # THEN EXPECT
188- assert numerical_area == pytest .approx (dho .area .value , rel = 2e-3 )
155+ assert numerical_area == pytest .approx (dho ._area .value , rel = 2e-3 )
189156
190157 def test_convert_unit (self , dho : DampedHarmonicOscillator ):
191158 dho .convert_unit ("microeV" )
192159
193- assert dho .unit == "microeV"
194- assert dho .area .value == 2 * 1e3
195- assert dho .center .value == 1.5 * 1e3
196- assert dho .width .value == 0.3 * 1e3
160+ assert dho ._unit == "microeV"
161+ assert dho ._area .value == 2 * 1e3
162+ assert dho ._center .value == 1.5 * 1e3
163+ assert dho ._width .value == 0.3 * 1e3
197164
198165 def test_copy (self , dho : DampedHarmonicOscillator ):
199166 dho_copy = dho .copy ()
200167 assert dho_copy is not dho
201- assert dho_copy .name == dho .name
168+ assert dho_copy .name == "copy of " + dho ._name
202169
203- assert dho_copy .area .value == dho .area .value
204- assert dho_copy .area .fixed == dho .area .fixed
170+ assert dho_copy ._area .value == dho ._area .value
171+ assert dho_copy ._area .fixed == dho ._area .fixed
205172
206- assert dho_copy .center .value == dho .center .value
207- assert dho_copy .center .fixed == dho .center .fixed
173+ assert dho_copy ._center .value == dho ._center .value
174+ assert dho_copy ._center .fixed == dho ._center .fixed
208175
209- assert dho_copy .width .value == dho .width .value
210- assert dho_copy .width .fixed == dho .width .fixed
176+ assert dho_copy ._width .value == dho ._width .value
177+ assert dho_copy ._width .fixed == dho ._width .fixed
211178
212- assert dho_copy .unit == dho .unit
179+ assert dho_copy ._unit == dho ._unit
213180
214181 def test_repr (self , dho : DampedHarmonicOscillator ):
215182 repr_str = repr (dho )
0 commit comments