diff --git a/carbontracker/emissions/intensity/fetchers/carbonintensitygb.py b/carbontracker/emissions/intensity/fetchers/carbonintensitygb.py index b527c2a..8afdb04 100644 --- a/carbontracker/emissions/intensity/fetchers/carbonintensitygb.py +++ b/carbontracker/emissions/intensity/fetchers/carbonintensitygb.py @@ -74,7 +74,7 @@ def _time_from_to_str(self, time_dur): """Returns the current date in UTC (from) and time_dur seconds ahead (to) in ISO8601 format YYYY-MM-DDThh:mmZ.""" date_format = "%Y-%m-%dT%H:%MZ" - time_from = datetime.datetime.utcnow() + time_from = datetime.datetime.now(datetime.timezone.utc) time_to = time_from + datetime.timedelta(seconds=time_dur) from_str = time_from.strftime(date_format) to_str = time_to.strftime(date_format) diff --git a/carbontracker/emissions/intensity/fetchers/energidataservice.py b/carbontracker/emissions/intensity/fetchers/energidataservice.py index 5518616..fc04d12 100644 --- a/carbontracker/emissions/intensity/fetchers/energidataservice.py +++ b/carbontracker/emissions/intensity/fetchers/energidataservice.py @@ -26,7 +26,11 @@ def carbon_intensity(self, g_location, time_dur=None): def _emission_current(self): def url_creator(area): - return 'https://api.energidataservice.dk/dataset/CO2emis?filter={"PriceArea":"' + area + '"}' + return ( + 'https://api.energidataservice.dk/dataset/CO2emis?filter={"PriceArea":"' + + area + + '"}' + ) areas = ["DK1", "DK2"] carbon_intensities = [] @@ -41,7 +45,13 @@ def url_creator(area): def _emission_prognosis(self, time_dur): from_str, to_str = self._interval(time_dur=time_dur) - url = "https://api.energidataservice.dk/dataset/CO2Emis?start={" + from_str + "&end={" + to_str + "}&limit=4" + url = ( + "https://api.energidataservice.dk/dataset/CO2Emis?start=" + + from_str + + "&end=" + + to_str + + "&limit=4" + ) response = requests.get(url) if not response.ok: raise exceptions.CarbonIntensityFetcherError(response.json()) @@ -50,14 +60,14 @@ def _emission_prognosis(self, time_dur): return np.mean(carbon_intensities) def _interval(self, time_dur): - from_time = datetime.datetime.utcnow() + from_time = datetime.datetime.now(datetime.timezone.utc) to_time = from_time + datetime.timedelta(seconds=time_dur) from_str = self._nearest_5_min(from_time) to_str = self._nearest_5_min(to_time) return from_str, to_str def _nearest_5_min(self, time): - date_format = "%Y-%m-%d %H:%M" + date_format = "%Y-%m-%dT%H:%M" nearest_5_min = time - datetime.timedelta( minutes=time.minute % 5, seconds=time.second, microseconds=time.microsecond ) diff --git a/tests/intensity/test_carbonintensitygb.py b/tests/intensity/test_carbonintensitygb.py index 38e1bc4..a681feb 100644 --- a/tests/intensity/test_carbonintensitygb.py +++ b/tests/intensity/test_carbonintensitygb.py @@ -19,7 +19,9 @@ def test_suitable_with_non_gb_location(self): self.assertFalse(result) @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.datetime") - @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get") + @mock.patch( + "carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get" + ) def test_carbon_intensity_gb_regional(self, mock_get, mock_datetime): mock_response = mock.MagicMock() mock_response.ok = True @@ -43,8 +45,8 @@ def test_carbon_intensity_gb_regional(self, mock_get, mock_datetime): g_location = mock.MagicMock(postal="AB12 3CD") time_dur = 3600 - # Patch datetime.utcnow to return a fixed value - mock_datetime.datetime.utcnow.return_value = datetime.datetime(2023, 5, 20, 0, 0) + # Patch datetime.now to return a fixed value + mock_datetime.datetime.now.return_value = datetime.datetime(2023, 5, 20, 0, 0) # Patch datetime.timedelta to return a fixed value mock_datetime.timedelta().__radd__().strftime.return_value = "2023-05-20T01:00Z" @@ -59,7 +61,9 @@ def test_carbon_intensity_gb_regional(self, mock_get, mock_datetime): ) self.assertEqual(result, 250) - @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get") + @mock.patch( + "carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get" + ) def test_carbon_intensity_gb_regional_with_error_response(self, mock_get): mock_response = mock.MagicMock() mock_response.ok = False @@ -72,7 +76,9 @@ def test_carbon_intensity_gb_regional_with_error_response(self, mock_get): self.fetcher._carbon_intensity_gb_regional(g_location.postal, time_dur) @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.datetime") - @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get") + @mock.patch( + "carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get" + ) def test_carbon_intensity_gb_national(self, mock_get, mock_datetime): mock_response = mock.MagicMock() mock_response.ok = True @@ -90,8 +96,8 @@ def test_carbon_intensity_gb_national(self, mock_get, mock_datetime): from_str = "2023-05-20T00:00Z" to_str = "2023-05-20T01:00Z" - # Patch datetime.utcnow to return a fixed value - mock_datetime.datetime.utcnow.return_value = datetime.datetime(2023, 5, 20, 0, 0) + # Patch datetime.now to return a fixed value + mock_datetime.datetime.now.return_value = datetime.datetime(2023, 5, 20, 0, 0) # Patch datetime.timedelta to return a fixed value mock_datetime.timedelta().__radd__().strftime.return_value = to_str @@ -103,7 +109,9 @@ def test_carbon_intensity_gb_national(self, mock_get, mock_datetime): ) self.assertEqual(result, 250) - @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get") + @mock.patch( + "carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get" + ) def test_carbon_intensity_gb_national_with_error_response(self, mock_get): mock_response = mock.MagicMock() mock_response.ok = False @@ -116,7 +124,7 @@ def test_carbon_intensity_gb_national_with_error_response(self, mock_get): def test_time_from_to_str(self): time_dur = 3600 - time_from = datetime.datetime.utcnow() + time_from = datetime.datetime.now(datetime.timezone.utc) time_to = time_from + datetime.timedelta(seconds=time_dur) from_str = time_from.strftime("%Y-%m-%dT%H:%MZ") to_str = time_to.strftime("%Y-%m-%dT%H:%MZ") @@ -125,7 +133,9 @@ def test_time_from_to_str(self): self.assertEqual(result, (from_str, to_str)) - @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get") + @mock.patch( + "carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get" + ) def test_carbon_intensity_with_postal(self, mock_get): mock_response = mock.MagicMock() mock_response.ok = True @@ -140,7 +150,9 @@ def test_carbon_intensity_with_postal(self, mock_get): self.assertEqual(carbon_intensity_obj.carbon_intensity, 250) self.assertEqual(carbon_intensity_obj.is_prediction, True) - @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get") + @mock.patch( + "carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get" + ) def test_carbon_intensity_without_postal(self, mock_get): mock_response = mock.MagicMock() mock_response.ok = True @@ -154,7 +166,9 @@ def test_carbon_intensity_without_postal(self, mock_get): self.assertEqual(carbon_intensity_obj.carbon_intensity, 250) self.assertEqual(carbon_intensity_obj.is_prediction, True) - @mock.patch("carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get") + @mock.patch( + "carbontracker.emissions.intensity.fetchers.carbonintensitygb.requests.get" + ) def test_carbon_intensity_gb_regional_without_time_dur(self, mock_get): mock_response = mock.MagicMock() mock_response.ok = True diff --git a/tests/intensity/test_energidataservice.py b/tests/intensity/test_energidataservice.py index e65e835..3b83b93 100644 --- a/tests/intensity/test_energidataservice.py +++ b/tests/intensity/test_energidataservice.py @@ -21,10 +21,7 @@ def test_carbon_intensity_no_time_dur(self, mock_get): mock_response = mock.MagicMock() mock_response.ok = True mock_response.json.return_value = { - "records": [ - {"CO2Emission": 1.0}, - {"CO2Emission": 2.0} - ] + "records": [{"CO2Emission": 1.0}, {"CO2Emission": 2.0}] } mock_get.return_value = mock_response result = self.fetcher.carbon_intensity(self.geocoder) @@ -41,7 +38,7 @@ def test_carbon_intensity_with_time_dur(self, mock_get): {"CO2Emission": 1.0}, {"CO2Emission": 2.0}, {"CO2Emission": 3.0}, - {"CO2Emission": 4.0} + {"CO2Emission": 4.0}, ] } mock_get.return_value = mock_response @@ -60,14 +57,14 @@ def test_nearest_5_min(self, mock_get): {"CO2Emission": 1.0}, {"CO2Emission": 2.0}, {"CO2Emission": 3.0}, - {"CO2Emission": 4.0} + {"CO2Emission": 4.0}, ] } mock_get.return_value = mock_response _result = self.fetcher.carbon_intensity(self.geocoder, time_dur=1800) - now = datetime.datetime.utcnow() + now = datetime.datetime.now(datetime.timezone.utc) from_time = now - datetime.timedelta( minutes=now.minute % 5, seconds=now.second, microseconds=now.microsecond @@ -75,12 +72,12 @@ def test_nearest_5_min(self, mock_get): to_time = from_time + datetime.timedelta(seconds=1800) # Format the from_time and to_time to strings - date_format = "%Y-%m-%d %H:%M" + date_format = "%Y-%m-%dT%H:%M" expected_from_time = from_time.strftime(date_format) expected_to_time = to_time.strftime(date_format) # Check that the mocked requests.get was called with the expected URL - expected_url = f"https://api.energidataservice.dk/dataset/CO2Emis?start={{{expected_from_time}&end={{{expected_to_time}}}&limit=4" + expected_url = f"https://api.energidataservice.dk/dataset/CO2Emis?start={expected_from_time}&end={expected_to_time}&limit=4" mock_get.assert_called_once_with(expected_url) @mock.patch("requests.get")