-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45431fa
commit edf4107
Showing
2 changed files
with
92 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,59 @@ | ||
import pytest | ||
import requests | ||
import requests_mock | ||
from unittest.mock import patch | ||
|
||
import pandas as pd | ||
|
||
from pydantic_models import BaseModel | ||
from typing import Optional | ||
|
||
# InsightsApiModelsResponsesResponseWithMetadata1InsightsApiModelsResponsesTransparencyDayAheadGenerationForWindAndSolar | ||
|
||
API_URL = "/v0/solar/GB/national/elexon" | ||
|
||
|
||
@pytest.fixture | ||
def mock_data(): | ||
return { | ||
"data": [ | ||
{ | ||
"timestamp": "2024-07-24T16:45:09+00:00", | ||
"expected_power_generation_megawatts": 0, | ||
"plevels": None, | ||
}, | ||
{ | ||
"timestamp": "2024-07-24T16:45:09+00:00", | ||
"expected_power_generation_megawatts": 0, | ||
"plevels": None, | ||
}, | ||
] | ||
} | ||
|
||
|
||
def test_get_elexon_forecast_with_data(mock_data, api_client): | ||
with requests_mock.Mocker() as m: | ||
url = ( | ||
f"https://data.elexon.co.uk/bmrs/api/v1/forecast/generation/wind-and-solar/day-ahead" | ||
) | ||
m.get(url, json=mock_data, headers={"Content-Type": "application/json"}) | ||
|
||
response = api_client.get('/v0/solar/GB/national/elexon') | ||
print("Response Headers:", response.headers) | ||
# Assertions | ||
assert response.status_code == 200 | ||
assert response.headers.get("Content-Type") == "application/json" | ||
assert response.json() == mock_data | ||
|
||
|
||
@pytest.fixture | ||
def empty_mock_data(): | ||
return {"data": []} | ||
|
||
|
||
def test_get_elexon_forecast_no_data(empty_mock_data): | ||
with requests_mock.Mocker() as m: | ||
url = ( | ||
f"{API_URL}?start_datetime_utc=2024-07-22T10:56:59.194610" | ||
f"&end_datetime_utc=2024-07-28T10:56:59.194680" | ||
f"&process_type=Day Ahead" | ||
) | ||
|
||
m.get(url, json=empty_mock_data, headers={"Content-Type": "application/json"}) | ||
|
||
response = requests.get(url) | ||
print("Response Headers:", response.headers) | ||
# Assertions | ||
assert response.status_code == 200 | ||
assert response.headers.get("Content-Type") == "application/json" | ||
assert response.json() == empty_mock_data | ||
class MockClass(BaseModel): | ||
|
||
publish_time: str | ||
quantity: float | ||
business_type: Optional[str] = "Solar generation" | ||
|
||
def to_dict(self): | ||
return self.__dict__ | ||
|
||
|
||
mock_data = [ | ||
MockClass( | ||
**{ | ||
"publish_time": "2024-07-24T16:45:09+00:00", | ||
"quantity": 0, | ||
} | ||
), | ||
MockClass( | ||
**{ | ||
"publish_time": "2024-07-24T16:45:09+00:00", | ||
"quantity": 0, | ||
} | ||
), | ||
] | ||
|
||
|
||
class MockResponse: | ||
def __init__(self): | ||
self.data = mock_data | ||
|
||
|
||
@patch("national.forecast_generation_wind_and_solar_day_ahead_get") | ||
def test_get_elexon_forecast_with_data(mock_function, api_client): | ||
mock_function.return_value = MockResponse() | ||
|
||
response = api_client.get("/v0/solar/GB/national/elexon") | ||
print("Response Headers:", response.headers) | ||
# Assertions | ||
assert response.status_code == 200 | ||
assert response.headers.get("Content-Type") == "application/json" | ||
|
||
api_data = response.json()["data"] | ||
assert len(api_data) == len(mock_data) | ||
for i in range(len(api_data)): | ||
assert api_data[i]["expected_power_generation_megawatts"] == mock_data[i].quantity | ||
assert pd.Timestamp(api_data[i]["timestamp"]) == pd.Timestamp(mock_data[i].publish_time) | ||
|