Skip to content

Commit

Permalink
Merge pull request #74 from Snailed/fix-energidataservice
Browse files Browse the repository at this point in the history
fixed energidataservice
  • Loading branch information
Snailed authored Jul 23, 2024
2 parents c3d7238 + 405b9d4 commit e404bdc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 14 additions & 4 deletions carbontracker/emissions/intensity/fetchers/energidataservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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())
Expand All @@ -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
)
Expand Down
38 changes: 26 additions & 12 deletions tests/intensity/test_carbonintensitygb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 6 additions & 9 deletions tests/intensity/test_energidataservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -60,27 +57,27 @@ 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
)
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")
Expand Down

0 comments on commit e404bdc

Please sign in to comment.