Config: Move lat/long/timezone from prediction to general

This commit is contained in:
Dominique Lasserre
2025-01-20 22:58:59 +01:00
parent 1658b491d2
commit c1dd31528b
19 changed files with 297 additions and 316 deletions

View File

@@ -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