|
1 | 1 | import unittest |
| 2 | +from unittest.mock import patch |
2 | 3 |
|
3 | 4 | from codecarbon.core.emissions import Emissions |
4 | 5 | from codecarbon.core.units import Energy |
@@ -220,3 +221,57 @@ def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION_uses_static_factor_without_to |
220 | 221 | expected_nordic = (emission_factor_g / 1000) * energy.kWh |
221 | 222 | self.assertAlmostEqual(emissions, expected_nordic, places=6) |
222 | 223 | self.assertNotAlmostEqual(emissions, expected_country, places=4) |
| 224 | + |
| 225 | + def test_try_get_nordic_region_emissions_returns_none_without_region(self): |
| 226 | + # GIVEN |
| 227 | + energy = Energy.from_energy(kWh=1.0) |
| 228 | + geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region=None) |
| 229 | + |
| 230 | + # WHEN |
| 231 | + emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) |
| 232 | + |
| 233 | + # THEN |
| 234 | + self.assertIsNone(emissions) |
| 235 | + |
| 236 | + def test_try_get_nordic_region_emissions_returns_none_for_non_nordic_region(self): |
| 237 | + # GIVEN |
| 238 | + energy = Energy.from_energy(kWh=1.0) |
| 239 | + geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="XYZ") |
| 240 | + |
| 241 | + # WHEN |
| 242 | + emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) |
| 243 | + |
| 244 | + # THEN |
| 245 | + self.assertIsNone(emissions) |
| 246 | + |
| 247 | + def test_try_get_nordic_region_emissions_returns_none_if_region_data_missing(self): |
| 248 | + # GIVEN |
| 249 | + energy = Energy.from_energy(kWh=1.0) |
| 250 | + geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2") |
| 251 | + |
| 252 | + # WHEN |
| 253 | + with patch.object( |
| 254 | + self._data_source, |
| 255 | + "get_nordic_country_energy_mix_data", |
| 256 | + return_value={"data": {}}, |
| 257 | + ): |
| 258 | + emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) |
| 259 | + |
| 260 | + # THEN |
| 261 | + self.assertIsNone(emissions) |
| 262 | + |
| 263 | + def test_try_get_nordic_region_emissions_returns_none_on_data_loading_error(self): |
| 264 | + # GIVEN |
| 265 | + energy = Energy.from_energy(kWh=1.0) |
| 266 | + geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2") |
| 267 | + |
| 268 | + # WHEN |
| 269 | + with patch.object( |
| 270 | + self._data_source, |
| 271 | + "get_nordic_country_energy_mix_data", |
| 272 | + side_effect=Exception("boom"), |
| 273 | + ): |
| 274 | + emissions = self._emissions._try_get_nordic_region_emissions(energy, geo) |
| 275 | + |
| 276 | + # THEN |
| 277 | + self.assertIsNone(emissions) |
0 commit comments