2024-12-15 14:40:03 +01:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pendulum
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from akkudoktoreos.core.ems import get_ems
|
2024-12-29 18:42:49 +01:00
|
|
|
from akkudoktoreos.measurement.measurement import MeasurementDataRecord, get_measurement
|
2024-12-15 14:40:03 +01:00
|
|
|
from akkudoktoreos.prediction.loadakkudoktor import (
|
|
|
|
LoadAkkudoktor,
|
|
|
|
LoadAkkudoktorCommonSettings,
|
|
|
|
)
|
2024-12-29 18:42:49 +01:00
|
|
|
from akkudoktoreos.utils.datetimeutil import compare_datetimes, to_datetime, to_duration
|
2024-12-15 14:40:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2025-01-18 14:26:34 +01:00
|
|
|
def provider(config_eos):
|
2024-12-29 18:42:49 +01:00
|
|
|
"""Fixture to initialise the LoadAkkudoktor instance."""
|
2024-12-15 14:40:03 +01:00
|
|
|
settings = {
|
2025-01-12 05:19:37 +01:00
|
|
|
"load": {
|
2025-01-18 14:26:34 +01:00
|
|
|
"provider": "LoadAkkudoktor",
|
2025-01-12 05:19:37 +01:00
|
|
|
"provider_settings": {
|
|
|
|
"load_name": "Akkudoktor Profile",
|
|
|
|
"loadakkudoktor_year_energy": "1000",
|
|
|
|
},
|
|
|
|
}
|
2024-12-15 14:40:03 +01:00
|
|
|
}
|
|
|
|
config_eos.merge_settings_from_dict(settings)
|
2025-02-12 21:35:51 +01:00
|
|
|
assert config_eos.load.provider == "LoadAkkudoktor"
|
|
|
|
assert config_eos.load.provider_settings.loadakkudoktor_year_energy == 1000
|
2024-12-15 14:40:03 +01:00
|
|
|
return LoadAkkudoktor()
|
|
|
|
|
|
|
|
|
2024-12-29 18:42:49 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def measurement_eos():
|
|
|
|
"""Fixture to initialise the Measurement instance."""
|
|
|
|
measurement = get_measurement()
|
|
|
|
load0_mr = 500
|
|
|
|
load1_mr = 500
|
|
|
|
dt = to_datetime("2024-01-01T00:00:00")
|
|
|
|
interval = to_duration("1 hour")
|
|
|
|
for i in range(25):
|
|
|
|
measurement.records.append(
|
|
|
|
MeasurementDataRecord(
|
|
|
|
date_time=dt,
|
2025-01-18 14:26:34 +01:00
|
|
|
load0_mr=load0_mr,
|
|
|
|
load1_mr=load1_mr,
|
2024-12-29 18:42:49 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
dt += interval
|
|
|
|
load0_mr += 50
|
|
|
|
load1_mr += 50
|
|
|
|
assert compare_datetimes(measurement.min_datetime, to_datetime("2024-01-01T00:00:00")).equal
|
|
|
|
assert compare_datetimes(measurement.max_datetime, to_datetime("2024-01-02T00:00:00")).equal
|
|
|
|
return measurement
|
|
|
|
|
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_load_profiles_file(tmp_path):
|
|
|
|
"""Fixture to create a mock load profiles file."""
|
|
|
|
load_profiles_path = tmp_path / "load_profiles.npz"
|
|
|
|
np.savez(
|
|
|
|
load_profiles_path,
|
|
|
|
yearly_profiles=np.random.rand(365, 24), # Random load profiles
|
|
|
|
yearly_profiles_std=np.random.rand(365, 24), # Random standard deviation
|
|
|
|
)
|
|
|
|
return load_profiles_path
|
|
|
|
|
|
|
|
|
|
|
|
def test_loadakkudoktor_settings_validator():
|
|
|
|
"""Test the field validator for `loadakkudoktor_year_energy`."""
|
|
|
|
settings = LoadAkkudoktorCommonSettings(loadakkudoktor_year_energy=1234)
|
|
|
|
assert isinstance(settings.loadakkudoktor_year_energy, float)
|
|
|
|
assert settings.loadakkudoktor_year_energy == 1234.0
|
|
|
|
|
|
|
|
settings = LoadAkkudoktorCommonSettings(loadakkudoktor_year_energy=1234.56)
|
|
|
|
assert isinstance(settings.loadakkudoktor_year_energy, float)
|
|
|
|
assert settings.loadakkudoktor_year_energy == 1234.56
|
|
|
|
|
|
|
|
|
2025-01-18 14:26:34 +01:00
|
|
|
def test_loadakkudoktor_provider_id(provider):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test the `provider_id` class method."""
|
2025-01-18 14:26:34 +01:00
|
|
|
assert provider.provider_id() == "LoadAkkudoktor"
|
2024-12-15 14:40:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
@patch("akkudoktoreos.prediction.loadakkudoktor.np.load")
|
2025-01-18 14:26:34 +01:00
|
|
|
def test_load_data_from_mock(mock_np_load, mock_load_profiles_file, provider):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test the `load_data` method."""
|
|
|
|
# Mock numpy load to return data similar to what would be in the file
|
|
|
|
mock_np_load.return_value = {
|
|
|
|
"yearly_profiles": np.ones((365, 24)),
|
|
|
|
"yearly_profiles_std": np.zeros((365, 24)),
|
|
|
|
}
|
|
|
|
|
|
|
|
# Test data loading
|
2025-01-18 14:26:34 +01:00
|
|
|
data_year_energy = provider.load_data()
|
2024-12-15 14:40:03 +01:00
|
|
|
assert data_year_energy is not None
|
|
|
|
assert data_year_energy.shape == (365, 2, 24)
|
|
|
|
|
|
|
|
|
2025-01-18 14:26:34 +01:00
|
|
|
def test_load_data_from_file(provider):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test `load_data` loads data from the profiles file."""
|
2025-01-18 14:26:34 +01:00
|
|
|
data_year_energy = provider.load_data()
|
2024-12-15 14:40:03 +01:00
|
|
|
assert data_year_energy is not None
|
|
|
|
|
|
|
|
|
|
|
|
@patch("akkudoktoreos.prediction.loadakkudoktor.LoadAkkudoktor.load_data")
|
2025-01-18 14:26:34 +01:00
|
|
|
def test_update_data(mock_load_data, provider):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test the `_update` method."""
|
|
|
|
mock_load_data.return_value = np.random.rand(365, 2, 24)
|
|
|
|
|
|
|
|
# Mock methods for updating values
|
2024-12-30 13:41:39 +01:00
|
|
|
ems_eos = get_ems()
|
2024-12-15 14:40:03 +01:00
|
|
|
ems_eos.set_start_datetime(pendulum.datetime(2024, 1, 1))
|
|
|
|
|
|
|
|
# Assure there are no prediction records
|
2025-01-18 14:26:34 +01:00
|
|
|
provider.clear()
|
|
|
|
assert len(provider) == 0
|
2024-12-15 14:40:03 +01:00
|
|
|
|
|
|
|
# Execute the method
|
2025-01-18 14:26:34 +01:00
|
|
|
provider._update_data()
|
2024-12-15 14:40:03 +01:00
|
|
|
|
|
|
|
# Validate that update_value is called
|
2025-01-18 14:26:34 +01:00
|
|
|
assert len(provider) > 0
|
2024-12-29 18:42:49 +01:00
|
|
|
|
|
|
|
|
2025-01-18 14:26:34 +01:00
|
|
|
def test_calculate_adjustment(provider, measurement_eos):
|
2024-12-29 18:42:49 +01:00
|
|
|
"""Test `_calculate_adjustment` for various scenarios."""
|
|
|
|
data_year_energy = np.random.rand(365, 2, 24)
|
|
|
|
|
|
|
|
# Call the method and validate results
|
2025-01-18 14:26:34 +01:00
|
|
|
weekday_adjust, weekend_adjust = provider._calculate_adjustment(data_year_energy)
|
2024-12-29 18:42:49 +01:00
|
|
|
assert weekday_adjust.shape == (24,)
|
|
|
|
assert weekend_adjust.shape == (24,)
|
|
|
|
|
|
|
|
data_year_energy = np.zeros((365, 2, 24))
|
2025-01-18 14:26:34 +01:00
|
|
|
weekday_adjust, weekend_adjust = provider._calculate_adjustment(data_year_energy)
|
2024-12-29 18:42:49 +01:00
|
|
|
|
|
|
|
assert weekday_adjust.shape == (24,)
|
|
|
|
expected = np.array(
|
|
|
|
[
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
100.0,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
np.testing.assert_array_equal(weekday_adjust, expected)
|
|
|
|
|
|
|
|
assert weekend_adjust.shape == (24,)
|
|
|
|
expected = np.array(
|
|
|
|
[
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
np.testing.assert_array_equal(weekend_adjust, expected)
|
|
|
|
|
|
|
|
|
2025-01-18 14:26:34 +01:00
|
|
|
def test_provider_adjustments_with_mock_data(provider):
|
2024-12-29 18:42:49 +01:00
|
|
|
"""Test full integration of adjustments with mock data."""
|
|
|
|
with patch(
|
|
|
|
"akkudoktoreos.prediction.loadakkudoktor.LoadAkkudoktor._calculate_adjustment"
|
|
|
|
) as mock_adjust:
|
|
|
|
mock_adjust.return_value = (np.zeros(24), np.zeros(24))
|
|
|
|
|
|
|
|
# Test execution
|
2025-01-18 14:26:34 +01:00
|
|
|
provider._update_data()
|
2024-12-29 18:42:49 +01:00
|
|
|
assert mock_adjust.called
|