11import unittest
2+ from unittest import mock
3+
4+ import pandas as pd
25
3- from tests .testutils import get_test_data_source # Added back
46from codecarbon .core .emissions import Emissions
57from codecarbon .core .units import Energy
68from codecarbon .external .geography import CloudMetadata , GeoMetadata
79from codecarbon .input import DataSource
8- from unittest import mock
9- import pandas as pd
10+ from tests .testutils import get_test_data_source # Added back
1011
1112
1213class TestEmissions (unittest .TestCase ):
@@ -22,31 +23,35 @@ def test_get_emissions_CLOUD_AWS(self):
2223 # Test original behavior when no custom intensity is set
2324 emissions_calculator = Emissions (self ._data_source )
2425 # WHEN
25- emissions = emissions_calculator .get_cloud_emissions ( # Changed from self._emissions
26- Energy .from_energy (kWh = 0.6 ), # Using original energy value for this specific test
27- CloudMetadata (provider = "aws" , region = "us-east-1" ),
26+ emissions = (
27+ emissions_calculator .get_cloud_emissions ( # Changed from self._emissions
28+ Energy .from_energy (
29+ kWh = 0.6
30+ ), # Using original energy value for this specific test
31+ CloudMetadata (provider = "aws" , region = "us-east-1" ),
32+ )
2833 )
2934 # THEN
3035 assert isinstance (emissions , float )
31- self .assertAlmostEqual (emissions , 0.285 , places = 3 ) # Original assertion value
36+ self .assertAlmostEqual (emissions , 0.285 , places = 3 ) # Original assertion value
3237
3338 def test_emissions_CLOUD_AZURE (self ):
3439 # Test original behavior when no custom intensity is set
3540 emissions_calculator = Emissions (self ._data_source )
3641 # WHEN
3742 emissions = emissions_calculator .get_cloud_emissions (
38- Energy .from_energy (kWh = 1.5 ), # Original energy
43+ Energy .from_energy (kWh = 1.5 ), # Original energy
3944 CloudMetadata (provider = "azure" , region = "eastus" ),
4045 )
4146 # THEN
4247 assert isinstance (emissions , float )
43- self .assertAlmostEqual (emissions , 0.7125 , places = 4 ) # Original assertion
48+ self .assertAlmostEqual (emissions , 0.7125 , places = 4 ) # Original assertion
4449
4550 def test_emissions_CLOUD_GCP (self ):
4651 # Test original behavior when no custom intensity is set
4752 emissions_calculator = Emissions (self ._data_source )
4853 emissions = emissions_calculator .get_cloud_emissions (
49- Energy .from_energy (kWh = 0.01 ), # Original energy
54+ Energy .from_energy (kWh = 0.01 ), # Original energy
5055 CloudMetadata (provider = "gcp" , region = "us-central1" ),
5156 )
5257 # THEN
@@ -55,7 +60,6 @@ def test_emissions_CLOUD_GCP(self):
5560 # Actual output was 0.0043, implying 430 g/kWh
5661 self .assertAlmostEqual (emissions , 0.0043 , places = 6 )
5762
58-
5963 def test_get_carbon_intensity_per_source_data (self ):
6064 # This test doesn't use an Emissions instance directly for its main assertion
6165 carbon_intensity = self ._data_source .get_carbon_intensity_per_source_data ()
@@ -84,7 +88,9 @@ def test_get_emissions_PRIVATE_INFRA_UNKNOWN(self):
8488 self ._data_source .get_carbon_intensity_per_source_data ()
8589 )
8690 default_emissions_g_kwh = carbon_intensity_per_source .get ("world_average" )
87- expected_emissions_kg = default_emissions_g_kwh * 1000 / 1000 # kWh * g/kWh / g/kg
91+ expected_emissions_kg = (
92+ default_emissions_g_kwh * 1000 / 1000
93+ ) # kWh * g/kWh / g/kg
8894 assert isinstance (emissions , float )
8995 self .assertAlmostEqual (emissions , expected_emissions_kg , places = 2 )
9096
@@ -108,19 +114,28 @@ def test_private_infra_with_positive_custom_intensity(
108114 # f"Using custom carbon intensity for private infrastructure emissions: {custom_intensity} gCO2e/kWh"
109115 # )
110116 mock_co2_signal_get_emissions .assert_not_called ()
111- with mock .patch .object (self ._data_source , 'get_country_emissions_data' , wraps = self ._data_source .get_country_emissions_data ) as wrapped_method :
117+ with mock .patch .object (
118+ self ._data_source ,
119+ "get_country_emissions_data" ,
120+ wraps = self ._data_source .get_country_emissions_data ,
121+ ) as wrapped_method :
112122 # Call again to ensure no fallback logic is triggered by mistake
113- emissions_calculator .get_private_infra_emissions (self .mock_energy , self .mock_geo )
123+ emissions_calculator .get_private_infra_emissions (
124+ self .mock_energy , self .mock_geo
125+ )
114126 wrapped_method .assert_not_called ()
115127
116-
117- @mock .patch ("codecarbon.core.co2_signal.get_emissions" ) # Mock the co2_signal path
118- @mock .patch .object (DataSource , "get_global_energy_mix_data" ) # Mock the DataSource path for country emissions
128+ @mock .patch ("codecarbon.core.co2_signal.get_emissions" ) # Mock the co2_signal path
129+ @mock .patch .object (
130+ DataSource , "get_global_energy_mix_data"
131+ ) # Mock the DataSource path for country emissions
119132 def test_private_infra_with_none_custom_intensity_fallback_co2_signal (
120133 self , mock_get_global_energy_mix_data , mock_co2_signal_get_emissions
121134 ):
122135 mock_co2_signal_get_emissions .return_value = 0.123
123- emissions_calculator = Emissions (self ._data_source , co2_signal_api_token = "dummy_token" )
136+ emissions_calculator = Emissions (
137+ self ._data_source , co2_signal_api_token = "dummy_token"
138+ )
124139
125140 actual_emissions = emissions_calculator .get_private_infra_emissions (
126141 self .mock_energy , self .mock_geo
@@ -129,16 +144,24 @@ def test_private_infra_with_none_custom_intensity_fallback_co2_signal(
129144 mock_co2_signal_get_emissions .assert_called_once_with (
130145 self .mock_energy , self .mock_geo , "dummy_token"
131146 )
132- mock_get_global_energy_mix_data .assert_not_called () # Ensure it doesn't go to the other path
147+ mock_get_global_energy_mix_data .assert_not_called () # Ensure it doesn't go to the other path
133148
134- @mock .patch .object (DataSource , "get_global_energy_mix_data" ) # Mock the DataSource path for country emissions
149+ @mock .patch .object (
150+ DataSource , "get_global_energy_mix_data"
151+ ) # Mock the DataSource path for country emissions
135152 def test_private_infra_with_none_custom_intensity_fallback_datasource (
136153 self , mock_get_global_energy_mix_data
137154 ):
138155 # Simulate no CO2 signal token, forcing fallback to DataSource
139156 # USA (from self.mock_geo) has carbon_intensity: 381.98 g/kWh in test_data_source
140157 expected_intensity_g_kwh = 381.98
141- mock_get_global_energy_mix_data .return_value = {"USA" : {"country_name" : "United States" , "carbon_intensity" : expected_intensity_g_kwh , "total_TWh" : 100 }}
158+ mock_get_global_energy_mix_data .return_value = {
159+ "USA" : {
160+ "country_name" : "United States" ,
161+ "carbon_intensity" : expected_intensity_g_kwh ,
162+ "total_TWh" : 100 ,
163+ }
164+ }
142165
143166 emissions_calculator = Emissions (self ._data_source , co2_signal_api_token = None )
144167 expected_emissions = self .mock_energy .kWh * (expected_intensity_g_kwh / 1000.0 )
@@ -149,7 +172,6 @@ def test_private_infra_with_none_custom_intensity_fallback_datasource(
149172 self .assertAlmostEqual (actual_emissions , expected_emissions , places = 5 )
150173 mock_get_global_energy_mix_data .assert_called_once ()
151174
152-
153175 @mock .patch ("codecarbon.core.emissions.logger" )
154176 @mock .patch .object (DataSource , "get_cloud_emissions_data" )
155177 def test_cloud_emissions_with_positive_custom_intensity (
@@ -175,7 +197,13 @@ def test_cloud_emissions_with_positive_custom_intensity(
175197 def test_cloud_emissions_with_none_custom_intensity (self , mock_get_cloud_data ):
176198 expected_impact_g_kwh = 475.0 # For aws us-east-1 from test data
177199 # Create a DataFrame mock that behaves like the one from get_cloud_emissions_data
178- mock_df = pd .DataFrame ({'provider' : ['aws' ], 'region' : ['us-east-1' ], 'impact' : [expected_impact_g_kwh ]})
200+ mock_df = pd .DataFrame (
201+ {
202+ "provider" : ["aws" ],
203+ "region" : ["us-east-1" ],
204+ "impact" : [expected_impact_g_kwh ],
205+ }
206+ )
179207 mock_get_cloud_data .return_value = mock_df
180208
181209 emissions_calculator = Emissions (self ._data_source )
@@ -197,7 +225,6 @@ def test_get_emissions_PRIVATE_INFRA_NOR(self):
197225 assert isinstance (emissions , float )
198226 self .assertAlmostEqual (emissions , 26.4 / 1_000 , places = 4 )
199227
200-
201228 def test_get_emissions_PRIVATE_INFRA_USA_WITH_REGION (self ):
202229 emissions_calculator = Emissions (self ._data_source )
203230 emissions = emissions_calculator .get_private_infra_emissions (
@@ -211,7 +238,6 @@ def test_get_emissions_PRIVATE_INFRA_USA_WITH_REGION(self):
211238 # This implies an intensity of approx 0.36800765 kg/kWh for Illinois from test data.
212239 self .assertAlmostEqual (emissions , 0.11040229633309799 , places = 8 )
213240
214-
215241 def test_get_emissions_PRIVATE_INFRA_USA_WITHOUT_REGION (self ):
216242 emissions_calculator = Emissions (self ._data_source )
217243 emissions = emissions_calculator .get_private_infra_emissions (
@@ -229,7 +255,7 @@ def test_get_emissions_PRIVATE_INFRA_USA_WITHOUT_COUNTRYNAME(self):
229255 Energy .from_energy (kWh = 0.3 ), GeoMetadata (country_iso_code = "USA" )
230256 )
231257 assert isinstance (emissions , float )
232- self .assertAlmostEqual (emissions , 0.114594 , places = 6 ) # Same as above
258+ self .assertAlmostEqual (emissions , 0.114594 , places = 6 ) # Same as above
233259
234260 def test_get_emissions_PRIVATE_INFRA_CANADA_WITHOUT_REGION (self ):
235261 emissions_calculator = Emissions (self ._data_source )
@@ -255,7 +281,6 @@ def test_get_emissions_PRIVATE_INFRA_CANADA_WITH_REGION(self):
255281 # 3 kWh * 0.01814368 kg/kWh = 0.05443104
256282 self .assertAlmostEqual (emissions , 0.05443 , places = 5 )
257283
258-
259284 def test_get_emissions_PRIVATE_INFRA_unknown_country (self ):
260285 emissions_calculator = Emissions (self ._data_source )
261286 emissions = emissions_calculator .get_private_infra_emissions (
0 commit comments