-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from alirashidAR/issue/2
Issue/2
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ dependencies = [ | |
"netcdf4", | ||
"h5netcdf", | ||
"h5py", | ||
"pvlive-api", | ||
] | ||
|
||
[project.optional-dependencies] | ||
|
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 |
---|---|---|
@@ -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.
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 |
---|---|---|
@@ -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 |