mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2025-09-13 07:21:16 +00:00
Config: Move lat/long/timezone from prediction to general
This commit is contained in:
@@ -3,8 +3,9 @@ from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from akkudoktoreos.config.config import ConfigEOS
|
||||
from akkudoktoreos.config.config import ConfigCommonSettings, ConfigEOS
|
||||
from akkudoktoreos.core.logging import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -147,3 +148,67 @@ def test_config_copy(config_eos, monkeypatch):
|
||||
assert config_eos._get_config_file_path() == (temp_config_file_path, False)
|
||||
config_eos.update()
|
||||
assert temp_config_file_path.exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"latitude, longitude, expected_timezone",
|
||||
[
|
||||
(40.7128, -74.0060, "America/New_York"), # Valid latitude/longitude
|
||||
(None, None, None), # No location
|
||||
(51.5074, -0.1278, "Europe/London"), # Another valid location
|
||||
],
|
||||
)
|
||||
def test_config_common_settings_valid(latitude, longitude, expected_timezone):
|
||||
"""Test valid settings for ConfigCommonSettings."""
|
||||
general_settings = ConfigCommonSettings(
|
||||
latitude=latitude,
|
||||
longitude=longitude,
|
||||
)
|
||||
assert general_settings.latitude == latitude
|
||||
assert general_settings.longitude == longitude
|
||||
assert general_settings.timezone == expected_timezone
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"field_name, invalid_value, expected_error",
|
||||
[
|
||||
("latitude", -91.0, "Input should be greater than or equal to -90"),
|
||||
("latitude", 91.0, "Input should be less than or equal to 90"),
|
||||
("longitude", -181.0, "Input should be greater than or equal to -180"),
|
||||
("longitude", 181.0, "Input should be less than or equal to 180"),
|
||||
],
|
||||
)
|
||||
def test_config_common_settings_invalid(field_name, invalid_value, expected_error):
|
||||
"""Test invalid settings for PredictionCommonSettings."""
|
||||
valid_data = {
|
||||
"latitude": 40.7128,
|
||||
"longitude": -74.0060,
|
||||
}
|
||||
assert ConfigCommonSettings(**valid_data) is not None
|
||||
valid_data[field_name] = invalid_value
|
||||
|
||||
with pytest.raises(ValidationError, match=expected_error):
|
||||
ConfigCommonSettings(**valid_data)
|
||||
|
||||
|
||||
def test_config_common_settings_no_location():
|
||||
"""Test that timezone is None when latitude and longitude are not provided."""
|
||||
settings = ConfigCommonSettings(latitude=None, longitude=None)
|
||||
assert settings.timezone is None
|
||||
|
||||
|
||||
def test_config_common_settings_with_location():
|
||||
"""Test that timezone is correctly computed when latitude and longitude are provided."""
|
||||
settings = ConfigCommonSettings(latitude=34.0522, longitude=-118.2437)
|
||||
assert settings.timezone == "America/Los_Angeles"
|
||||
|
||||
|
||||
def test_config_common_settings_timezone_none_when_coordinates_missing():
|
||||
"""Test that timezone is None when latitude or longitude is missing."""
|
||||
config_no_latitude = ConfigCommonSettings(latitude=None, longitude=-74.0060)
|
||||
config_no_longitude = ConfigCommonSettings(latitude=40.7128, longitude=None)
|
||||
config_no_coords = ConfigCommonSettings(latitude=None, longitude=None)
|
||||
|
||||
assert config_no_latitude.timezone is None
|
||||
assert config_no_longitude.timezone is None
|
||||
assert config_no_coords.timezone is None
|
||||
|
@@ -39,49 +39,18 @@ def forecast_providers():
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"hours, historic_hours, latitude, longitude, expected_timezone",
|
||||
[
|
||||
(48, 24, 40.7128, -74.0060, "America/New_York"), # Valid latitude/longitude
|
||||
(0, 0, None, None, None), # No location
|
||||
(100, 50, 51.5074, -0.1278, "Europe/London"), # Another valid location
|
||||
],
|
||||
)
|
||||
def test_prediction_common_settings_valid(
|
||||
hours, historic_hours, latitude, longitude, expected_timezone
|
||||
):
|
||||
"""Test valid settings for PredictionCommonSettings."""
|
||||
settings = PredictionCommonSettings(
|
||||
hours=hours,
|
||||
historic_hours=historic_hours,
|
||||
latitude=latitude,
|
||||
longitude=longitude,
|
||||
)
|
||||
assert settings.hours == hours
|
||||
assert settings.historic_hours == historic_hours
|
||||
assert settings.latitude == latitude
|
||||
assert settings.longitude == longitude
|
||||
assert settings.timezone == expected_timezone
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"field_name, invalid_value, expected_error",
|
||||
[
|
||||
("hours", -1, "Input should be greater than or equal to 0"),
|
||||
("historic_hours", -5, "Input should be greater than or equal to 0"),
|
||||
("latitude", -91.0, "Input should be greater than or equal to -90"),
|
||||
("latitude", 91.0, "Input should be less than or equal to 90"),
|
||||
("longitude", -181.0, "Input should be greater than or equal to -180"),
|
||||
("longitude", 181.0, "Input should be less than or equal to 180"),
|
||||
],
|
||||
)
|
||||
def test_prediction_common_settings_invalid(field_name, invalid_value, expected_error):
|
||||
def test_prediction_common_settings_invalid(field_name, invalid_value, expected_error, config_eos):
|
||||
"""Test invalid settings for PredictionCommonSettings."""
|
||||
valid_data = {
|
||||
"hours": 48,
|
||||
"historic_hours": 24,
|
||||
"latitude": 40.7128,
|
||||
"longitude": -74.0060,
|
||||
}
|
||||
assert PredictionCommonSettings(**valid_data) is not None
|
||||
valid_data[field_name] = invalid_value
|
||||
@@ -90,31 +59,6 @@ def test_prediction_common_settings_invalid(field_name, invalid_value, expected_
|
||||
PredictionCommonSettings(**valid_data)
|
||||
|
||||
|
||||
def test_prediction_common_settings_no_location():
|
||||
"""Test that timezone is None when latitude and longitude are not provided."""
|
||||
settings = PredictionCommonSettings(hours=48, historic_hours=24, latitude=None, longitude=None)
|
||||
assert settings.timezone is None
|
||||
|
||||
|
||||
def test_prediction_common_settings_with_location():
|
||||
"""Test that timezone is correctly computed when latitude and longitude are provided."""
|
||||
settings = PredictionCommonSettings(
|
||||
hours=48, historic_hours=24, latitude=34.0522, longitude=-118.2437
|
||||
)
|
||||
assert settings.timezone == "America/Los_Angeles"
|
||||
|
||||
|
||||
def test_prediction_common_settings_timezone_none_when_coordinates_missing():
|
||||
"""Test that timezone is None when latitude or longitude is missing."""
|
||||
config_no_latitude = PredictionCommonSettings(latitude=None, longitude=-74.0060)
|
||||
config_no_longitude = PredictionCommonSettings(latitude=40.7128, longitude=None)
|
||||
config_no_coords = PredictionCommonSettings(latitude=None, longitude=None)
|
||||
|
||||
assert config_no_latitude.timezone is None
|
||||
assert config_no_longitude.timezone is None
|
||||
assert config_no_coords.timezone is None
|
||||
|
||||
|
||||
def test_initialization(prediction, forecast_providers):
|
||||
"""Test that Prediction is initialized with the correct providers in sequence."""
|
||||
assert isinstance(prediction, Prediction)
|
||||
|
@@ -88,27 +88,27 @@ class TestPredictionBase:
|
||||
@pytest.fixture
|
||||
def base(self, monkeypatch):
|
||||
# Provide default values for configuration
|
||||
monkeypatch.setenv("EOS_PREDICTION__LATITUDE", "50.0")
|
||||
monkeypatch.setenv("EOS_PREDICTION__LONGITUDE", "10.0")
|
||||
monkeypatch.setenv("EOS_PREDICTION__HOURS", "10")
|
||||
derived = DerivedBase()
|
||||
derived.config.reset_settings()
|
||||
assert derived.config.prediction.hours == 10
|
||||
return derived
|
||||
|
||||
def test_config_value_from_env_variable(self, base, monkeypatch):
|
||||
# From Prediction Config
|
||||
monkeypatch.setenv("EOS_PREDICTION__LATITUDE", "2.5")
|
||||
monkeypatch.setenv("EOS_PREDICTION__HOURS", "2")
|
||||
base.config.reset_settings()
|
||||
assert base.config.prediction.latitude == 2.5
|
||||
assert base.config.prediction.hours == 2
|
||||
|
||||
def test_config_value_from_field_default(self, base, monkeypatch):
|
||||
assert base.config.prediction.model_fields["hours"].default == 48
|
||||
assert base.config.prediction.hours == 48
|
||||
monkeypatch.setenv("EOS_PREDICTION__HOURS", "128")
|
||||
assert base.config.prediction.model_fields["historic_hours"].default == 48
|
||||
assert base.config.prediction.historic_hours == 48
|
||||
monkeypatch.setenv("EOS_PREDICTION__HISTORIC_HOURS", "128")
|
||||
base.config.reset_settings()
|
||||
assert base.config.prediction.hours == 128
|
||||
monkeypatch.delenv("EOS_PREDICTION__HOURS")
|
||||
assert base.config.prediction.historic_hours == 128
|
||||
monkeypatch.delenv("EOS_PREDICTION__HISTORIC_HOURS")
|
||||
base.config.reset_settings()
|
||||
assert base.config.prediction.hours == 48
|
||||
assert base.config.prediction.historic_hours == 48
|
||||
|
||||
def test_get_config_value_key_error(self, base):
|
||||
with pytest.raises(AttributeError):
|
||||
@@ -185,10 +185,6 @@ class TestPredictionProvider:
|
||||
# The following values are currently not set in EOS config, we can override
|
||||
monkeypatch.setenv("EOS_PREDICTION__HISTORIC_HOURS", "2")
|
||||
assert os.getenv("EOS_PREDICTION__HISTORIC_HOURS") == "2"
|
||||
monkeypatch.setenv("EOS_PREDICTION__LATITUDE", "37.7749")
|
||||
assert os.getenv("EOS_PREDICTION__LATITUDE") == "37.7749"
|
||||
monkeypatch.setenv("EOS_PREDICTION__LONGITUDE", "-122.4194")
|
||||
assert os.getenv("EOS_PREDICTION__LONGITUDE") == "-122.4194"
|
||||
provider.config.reset_settings()
|
||||
|
||||
ems_eos.set_start_datetime(sample_start_datetime)
|
||||
@@ -196,8 +192,6 @@ class TestPredictionProvider:
|
||||
|
||||
assert provider.config.prediction.hours == config_eos.prediction.hours
|
||||
assert provider.config.prediction.historic_hours == 2
|
||||
assert provider.config.prediction.latitude == 37.7749
|
||||
assert provider.config.prediction.longitude == -122.4194
|
||||
assert provider.start_datetime == sample_start_datetime
|
||||
assert provider.end_datetime == sample_start_datetime + to_duration(
|
||||
f"{provider.config.prediction.hours} hours"
|
||||
|
@@ -25,11 +25,13 @@ FILE_TESTDATA_PV_FORECAST_RESULT_1 = DIR_TESTDATA.joinpath("pv_forecast_result_1
|
||||
def sample_settings(config_eos):
|
||||
"""Fixture that adds settings data to the global config."""
|
||||
settings = {
|
||||
"general": {
|
||||
"latitude": 52.52,
|
||||
"longitude": 13.405,
|
||||
},
|
||||
"prediction": {
|
||||
"hours": 48,
|
||||
"historic_hours": 24,
|
||||
"latitude": 52.52,
|
||||
"longitude": 13.405,
|
||||
},
|
||||
"pvforecast": {
|
||||
"provider": "PVForecastAkkudoktor",
|
||||
@@ -155,11 +157,13 @@ sample_value = AkkudoktorForecastValue(
|
||||
windspeed_10m=10.0,
|
||||
)
|
||||
sample_config_data = {
|
||||
"general": {
|
||||
"latitude": 52.52,
|
||||
"longitude": 13.405,
|
||||
},
|
||||
"prediction": {
|
||||
"hours": 48,
|
||||
"historic_hours": 24,
|
||||
"latitude": 52.52,
|
||||
"longitude": 13.405,
|
||||
},
|
||||
"pvforecast": {
|
||||
"provider": "PVForecastAkkudoktor",
|
||||
|
@@ -67,8 +67,8 @@ def test_invalid_provider(provider, monkeypatch):
|
||||
|
||||
def test_invalid_coordinates(provider, monkeypatch):
|
||||
"""Test invalid coordinates raise ValueError."""
|
||||
monkeypatch.setenv("EOS_PREDICTION__LATITUDE", "1000")
|
||||
monkeypatch.setenv("EOS_PREDICTION__LONGITUDE", "1000")
|
||||
monkeypatch.setenv("EOS_GENERAL__LATITUDE", "1000")
|
||||
monkeypatch.setenv("EOS_GENERAL__LONGITUDE", "1000")
|
||||
with pytest.raises(
|
||||
ValueError, # match="Latitude '1000' and/ or longitude `1000` out of valid range."
|
||||
):
|
||||
|
@@ -27,7 +27,7 @@ def provider(config_eos):
|
||||
"weather": {
|
||||
"provider": "ClearOutside",
|
||||
},
|
||||
"prediction": {
|
||||
"general": {
|
||||
"latitude": 50.0,
|
||||
"longitude": 10.0,
|
||||
},
|
||||
@@ -87,7 +87,7 @@ def test_invalid_coordinates(provider, config_eos):
|
||||
"weather": {
|
||||
"provider": "ClearOutside",
|
||||
},
|
||||
"prediction": {
|
||||
"general": {
|
||||
"latitude": 1000.0,
|
||||
"longitude": 1000.0,
|
||||
},
|
||||
|
Reference in New Issue
Block a user