@@ -57,36 +57,33 @@ def setUp(self):
5757 self .cost_func = lambda impact , data : 1.0
5858 self .impact_func_gen = lambda ** kwargs : ImpactFuncSet ()
5959
60- @patch ("climada.util.calibrate.impact_func.np.setdiff1d" )
61- def test_post_init_calls (self , setdiff1d_mock ):
60+ def test_post_init_calls (self ):
6261 """Test if post_init calls stuff correctly using mocks"""
6362 # Create mocks
64- hazard_mock_1 = create_autospec (Hazard ())
65- event_id = [10 ]
66- hazard_mock_1 .event_id = event_id
67- hazard_mock_2 = create_autospec (Hazard ())
6863 exposure_mock = create_autospec (Exposures ())
69- setdiff1d_mock .return_value = np .array ([])
7064
71- # Make first hazard mock return another instance
72- hazard_mock_1 .select .return_value = hazard_mock_2
73-
74- # Create input
65+ # Default
7566 input = Input (
76- hazard = hazard_mock_1 ,
67+ hazard = self . hazard ,
7768 exposure = exposure_mock ,
7869 data = self .data ,
7970 cost_func = self .cost_func ,
8071 impact_func_gen = self .impact_func_gen ,
8172 )
73+ exposure_mock .assign_centroids .assert_called_once_with (self .hazard )
74+ exposure_mock .reset_mock ()
8275
83- # Query checks
84- npt .assert_array_equal (setdiff1d_mock .call_args .args [0 ], self .data_events )
85- npt .assert_array_equal (setdiff1d_mock .call_args .args [1 ], event_id )
86- hazard_mock_1 .select .assert_called_once_with (event_id = self .data_events )
87- self .assertNotEqual (input .hazard , hazard_mock_1 )
88- self .assertEqual (input .hazard , hazard_mock_2 )
89- exposure_mock .assign_centroids .assert_called_once_with (hazard_mock_2 )
76+ # Default
77+ input = Input (
78+ hazard = self .hazard ,
79+ exposure = exposure_mock ,
80+ data = self .data ,
81+ cost_func = self .cost_func ,
82+ impact_func_gen = self .impact_func_gen ,
83+ assign_centroids = False
84+ )
85+ exposure_mock .assign_centroids .assert_not_called ()
86+
9087
9188 def test_post_init (self ):
9289 """Test if post_init results in a sensible hazard and exposure"""
@@ -100,37 +97,9 @@ def test_post_init(self):
10097 )
10198
10299 # Check hazard and exposure
103- npt .assert_array_equal (input .hazard .event_id , self .data .index )
104100 self .assertIn ("centr_" , input .exposure .gdf )
105101 npt .assert_array_equal (input .exposure .gdf ["centr_" ], [0 , 1 , - 1 ])
106102
107- def test_non_matching_events (self ):
108- """Test if non-matching events result in errors"""
109- data = pd .DataFrame (data = {"a" : [1 , 2 , 3 ]}, index = [9 , 3 , 12 ])
110- input_kwargs = {
111- "hazard" : self .hazard ,
112- "exposure" : self .exposure ,
113- "data" : data ,
114- "cost_func" : self .cost_func ,
115- "impact_func_gen" : self .impact_func_gen ,
116- "align" : False ,
117- }
118-
119- # No error without alignment
120- Input (** input_kwargs )
121-
122- # Error with alignment
123- input_kwargs .update (align = True )
124- with self .assertRaises (RuntimeError ) as cm :
125- Input (** input_kwargs )
126-
127- self .assertIn (
128- "Event IDs in 'data' do not match event IDs in 'hazard'" , str (cm .exception )
129- )
130- self .assertIn ("9" , str (cm .exception ))
131- self .assertIn ("12" , str (cm .exception ))
132- self .assertNotIn ("3" , str (cm .exception ))
133-
134103
135104class TestOptimizer (unittest .TestCase ):
136105 """Base class for testing optimizers. Creates an input mock"""
@@ -144,7 +113,7 @@ def setUp(self):
144113 data = create_autospec (pd .DataFrame , instance = True ),
145114 cost_func = MagicMock (),
146115 impact_func_gen = MagicMock (),
147- align = False ,
116+ assign_centroids = False ,
148117 )
149118 )
150119
0 commit comments