@@ -174,6 +174,15 @@ def test_get_emissions_PRIVATE_INFRA_unknown_country(self):
174174 assert isinstance (emissions , float )
175175 self .assertAlmostEqual (emissions , 0.475 , places = 2 )
176176
177+ def test_get_emissions_PRIVATE_INFRA_without_country_metadata (self ):
178+ emissions = self ._emissions .get_private_infra_emissions (
179+ Energy .from_energy (kWh = 1 ),
180+ GeoMetadata (country_iso_code = None , country_name = None , region = None ),
181+ )
182+
183+ assert isinstance (emissions , float )
184+ self .assertAlmostEqual (emissions , 0.475 , places = 2 )
185+
177186 @patch ("codecarbon.core.electricitymaps_api.get_emissions" )
178187 def test_private_infra_uses_forced_intensity_when_set (self , mocked_get_emissions ):
179188 emissions_calculator = Emissions (
@@ -270,6 +279,69 @@ def test_try_get_nordic_region_emissions_returns_none_for_non_nordic_region(self
270279 # THEN
271280 self .assertIsNone (emissions )
272281
282+ def test_nordic_admin_regions_fall_back_to_country_without_regional_lookup (self ):
283+ energy = Energy .from_energy (kWh = 1.0 )
284+ admin_regions = [
285+ ("SWE" , "Sweden" , "Stockholm County" ),
286+ ("NOR" , "Norway" , "Oslo" ),
287+ ("FIN" , "Finland" , "Uusimaa" ),
288+ ]
289+
290+ for country_iso_code , country_name , region in admin_regions :
291+ with self .subTest (country_iso_code = country_iso_code , region = region ):
292+ geo = GeoMetadata (
293+ country_iso_code = country_iso_code ,
294+ country_name = country_name ,
295+ region = region ,
296+ )
297+ expected_emissions = self ._emissions .get_country_emissions (energy , geo )
298+
299+ with (
300+ patch .object (
301+ self ._data_source , "get_nordic_country_energy_mix_data"
302+ ) as get_nordic_data ,
303+ patch .object (
304+ self ._data_source , "get_country_emissions_data"
305+ ) as get_regional_emissions_data ,
306+ patch .object (
307+ self ._data_source , "get_country_energy_mix_data"
308+ ) as get_regional_energy_mix_data ,
309+ patch ("codecarbon.core.emissions.logger.error" ) as log_error ,
310+ patch ("codecarbon.core.emissions.logger.warning" ) as log_warning ,
311+ ):
312+ emissions = self ._emissions .get_private_infra_emissions (energy , geo )
313+
314+ self .assertAlmostEqual (emissions , expected_emissions , places = 6 )
315+ get_nordic_data .assert_not_called ()
316+ get_regional_emissions_data .assert_not_called ()
317+ get_regional_energy_mix_data .assert_not_called ()
318+ log_error .assert_not_called ()
319+ log_warning .assert_not_called ()
320+
321+ def test_nordic_region_missing_static_data_falls_back_to_country (self ):
322+ energy = Energy .from_energy (kWh = 1.0 )
323+ geo = GeoMetadata (country_iso_code = "SWE" , country_name = "Sweden" , region = "SE2" )
324+ expected_emissions = self ._emissions .get_country_emissions (energy , geo )
325+
326+ with (
327+ patch .object (
328+ self ._data_source ,
329+ "get_nordic_country_energy_mix_data" ,
330+ return_value = {"data" : {}},
331+ ),
332+ patch .object (
333+ self ._data_source , "get_country_emissions_data"
334+ ) as get_regional_emissions_data ,
335+ patch .object (
336+ self ._data_source , "get_country_energy_mix_data"
337+ ) as get_regional_energy_mix_data ,
338+ ):
339+ emissions = self ._emissions .get_private_infra_emissions (energy , geo )
340+
341+ self .assertAlmostEqual (emissions , expected_emissions , places = 6 )
342+ get_regional_emissions_data .assert_not_called ()
343+ get_regional_energy_mix_data .assert_not_called ()
344+
273345 def test_try_get_nordic_region_emissions_returns_none_if_region_data_missing (self ):
274346 # GIVEN
275347 energy = Energy .from_energy (kWh = 1.0 )
0 commit comments