Skip to content

Commit

Permalink
Merge pull request #30 from alirashidAR/issue/2
Browse files Browse the repository at this point in the history
Issue/2
  • Loading branch information
jcamier authored Jan 9, 2025
2 parents c266d93 + a3ef323 commit a579f23
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
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)
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

0 comments on commit a579f23

Please sign in to comment.