Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/2 #30

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies = [
"netcdf4",
"h5netcdf",
"h5py",
"pvlive-api",
]

[project.optional-dependencies]
Expand Down
46 changes: 46 additions & 0 deletions src/open_data_pvnet/scripts/fetch_pvlive_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from datetime import datetime
from pvlive_api import PVLive
import logging
import pytz


logger = logging.getLogger(__name__)

class PVLiveData:
def __init__(self):
self.pvl = PVLive()

def get_latest_data(self, period, entity_type="gsp", entity_id=0, extra_fields=""):
"""
Get the latest data from PVlive
"""
try:
df = self.pvl.latest(entity_type=entity_type, entity_id=entity_id, extra_fields=extra_fields, period=period, dataframe=True)
return df
except Exception as e:
logger.error(e)
return None

def get_data_between(self, start, end, entity_type="gsp", entity_id=0, extra_fields=""):
"""
Get the data between two dates
"""
try:
df = self.pvl.between(start=start, end=end, entity_type=entity_type, entity_id=entity_id, extra_fields=extra_fields, dataframe=True)
return df
except Exception as e:
logger.error(e)
return None

def get_data_at_time(self, dt):
"""
Get data at a specific time
"""
try:
df = self.pvl.at_time(dt, entity_type="pes", entity_id=0, extra_fields="", period=30, dataframe=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry i missed this, but entity_type should be "gsp"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, I'll create a pull request to change the default entity type to gsp

return df
except Exception as e:
logger.error(e)
return None


Empty file added tests/data/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions tests/data/test_pvlive_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest
from unittest.mock import MagicMock
from datetime import datetime
import pytz
from open_data_pvnet.scripts.fetch_pvlive_data import PVLiveData

@pytest.fixture
def pvlive_mock():
"""
Fixture to create a PVLiveData instance with mocked PVLive methods.
"""
pvlive = PVLiveData()
pvlive.pvl.latest = MagicMock()
pvlive.pvl.between = MagicMock()
pvlive.pvl.at_time = MagicMock()
return pvlive

def test_get_latest_data(pvlive_mock):
"""
Test the get_latest_data method.
"""
mock_data = {"column1": [1, 2], "column2": [3, 4]}
pvlive_mock.pvl.latest.return_value = mock_data

result = pvlive_mock.get_latest_data(period=30)
pvlive_mock.pvl.latest.assert_called_once_with(
entity_type="gsp", entity_id=0, extra_fields="", period=30, dataframe=True
)
assert result == mock_data

def test_get_data_between(pvlive_mock):
"""
Test the get_data_between method.
"""
mock_data = {"column1": [5, 6], "column2": [7, 8]}
pvlive_mock.pvl.between.return_value = mock_data

start = datetime(2021, 1, 1, 12, 0, tzinfo=pytz.utc)
end = datetime(2021, 1, 2, 12, 0, tzinfo=pytz.utc)

result = pvlive_mock.get_data_between(start, end)
pvlive_mock.pvl.between.assert_called_once_with(
start=start, end=end, entity_type="gsp", entity_id=0, extra_fields="", dataframe=True
)
assert result == mock_data

def test_get_data_at_time(pvlive_mock):
"""
Test the get_data_at_time method.
"""
mock_data = {"column1": [9, 10], "column2": [11, 12]}
pvlive_mock.pvl.at_time.return_value = mock_data

dt = datetime(2021, 1, 1, 12, 0, tzinfo=pytz.utc)

result = pvlive_mock.get_data_at_time(dt)
pvlive_mock.pvl.at_time.assert_called_once_with(
dt, entity_type="pes", entity_id=0, extra_fields="", period=30, dataframe=True
)
assert result == mock_data