|
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 |
@@ -172,3 +173,105 @@ def test_get_emissions_PRIVATE_INFRA_unknown_country(self): |
172 | 173 | ) |
173 | 174 | assert isinstance(emissions, float) |
174 | 175 | self.assertAlmostEqual(emissions, 0.475, places=2) |
| 176 | + |
| 177 | + def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION(self): |
| 178 | + # WHEN |
| 179 | + # Test Nordic region (Sweden SE2) |
| 180 | + |
| 181 | + emissions = self._emissions.get_private_infra_emissions( |
| 182 | + Energy.from_energy(kWh=1.0), |
| 183 | + GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2"), |
| 184 | + ) |
| 185 | + |
| 186 | + # THEN |
| 187 | + # Nordic regions use static emission factors from the JSON file |
| 188 | + # SE2 has an emission factor specified in nordic_country_energy_mix.json |
| 189 | + assert isinstance(emissions, float) |
| 190 | + self.assertAlmostEqual(emissions, 0.018, places=6) |
| 191 | + |
| 192 | + def test_get_emissions_PRIVATE_INFRA_NORDIC_FINLAND(self): |
| 193 | + # WHEN |
| 194 | + # Test Nordic region (Finland) |
| 195 | + |
| 196 | + emissions = self._emissions.get_private_infra_emissions( |
| 197 | + Energy.from_energy(kWh=2.5), |
| 198 | + GeoMetadata(country_iso_code="FIN", country_name="Finland", region="FI"), |
| 199 | + ) |
| 200 | + |
| 201 | + # THEN |
| 202 | + # Finland (FI) should use Nordic static emission factors |
| 203 | + assert isinstance(emissions, float) |
| 204 | + expected_emissions = 0.072 * 2.5 |
| 205 | + self.assertAlmostEqual(emissions, expected_emissions, places=6) |
| 206 | + |
| 207 | + def test_get_emissions_PRIVATE_INFRA_NORDIC_REGION_uses_static_factor_without_token( |
| 208 | + self, |
| 209 | + ): |
| 210 | + # GIVEN |
| 211 | + energy = Energy.from_energy(kWh=1.0) |
| 212 | + geo = GeoMetadata(country_iso_code="SWE", country_name="Sweden", region="SE2") |
| 213 | + |
| 214 | + # WHEN |
| 215 | + emissions = self._emissions.get_private_infra_emissions(energy, geo) |
| 216 | + |
| 217 | + # THEN |
| 218 | + expected_country = self._emissions.get_country_emissions(energy, geo) |
| 219 | + nordic_data = self._data_source.get_nordic_country_energy_mix_data() |
| 220 | + emission_factor_g = nordic_data["data"]["SE2"]["emission_factor"] |
| 221 | + expected_nordic = (emission_factor_g / 1000) * energy.kWh |
| 222 | + self.assertAlmostEqual(emissions, expected_nordic, places=6) |
| 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