@@ -120,6 +120,38 @@ def test_convert_unit(self, sample_model):
120120 for component in list (sample_model ):
121121 assert component .unit == "eV"
122122
123+ def test_convert_unit_failure_rolls_back (self , sample_model ):
124+ # WHEN THEN
125+ # Introduce a faulty component that will fail conversion
126+ class FaultyComponent (Gaussian ):
127+ def convert_unit (self , unit : str ) -> None :
128+ raise RuntimeError ("Conversion failed." )
129+
130+ faulty_component = FaultyComponent (
131+ name = "FaultyComponent" , area = 1.0 , center = 0.0 , width = 1.0 , unit = "meV"
132+ )
133+ sample_model .add_component (faulty_component )
134+
135+ original_units = {
136+ component .name : component .unit for component in list (sample_model )
137+ }
138+
139+ # EXPECT
140+ with pytest .raises (RuntimeError , match = "Conversion failed." ):
141+ sample_model .convert_unit ("eV" )
142+
143+ # Check that all components have their original units
144+ for component in list (sample_model ):
145+ assert component .unit == original_units [component .name ]
146+
147+ def test_set_unit (self , sample_model ):
148+ # WHEN THEN EXPECT
149+ with pytest .raises (
150+ AttributeError ,
151+ match = "Unit is read-only. Use convert_unit to change the unit" ,
152+ ):
153+ sample_model .unit = "eV"
154+
123155 def test_evaluate (self , sample_model ):
124156 # WHEN
125157 x = np .linspace (- 5 , 5 , 100 )
@@ -290,6 +322,16 @@ def test_fix_and_free_all_parameters(self, sample_model):
290322 for param in sample_model .get_parameters ():
291323 assert param .fixed is False
292324
325+ def test_contains (self , sample_model ):
326+ # WHEN THEN
327+ assert "TestGaussian1" in sample_model
328+ assert "NonExistentComponent" not in sample_model
329+ assert sample_model ["TestLorentzian1" ] in sample_model
330+ fake_component = Gaussian (
331+ name = "FakeGaussian" , area = 1.0 , center = 0.0 , width = 1.0 , unit = "meV"
332+ )
333+ assert fake_component not in sample_model
334+
293335 def test_repr_contains_name_and_components (self , sample_model ):
294336 # WHEN THEN
295337 rep = repr (sample_model )
0 commit comments