feat(Q3): Open-Meteo weather fetcher with hourly caching
This commit is contained in:
39
tests/unit/test_weather.py
Normal file
39
tests/unit/test_weather.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import time
|
||||
from unittest.mock import patch, MagicMock
|
||||
from vigilar.detection.weather import WeatherFetcher, _weather_code_to_text
|
||||
|
||||
|
||||
def test_get_conditions_returns_dict_on_success():
|
||||
fetcher = WeatherFetcher()
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {"current": {"temperature_2m": 15.5, "weather_code": 2}}
|
||||
with patch("vigilar.detection.weather.requests.get", return_value=mock_response):
|
||||
result = fetcher.get_conditions(45.0, -85.0)
|
||||
assert result is not None
|
||||
assert result["temperature_c"] == 15.5
|
||||
assert isinstance(result["conditions"], str)
|
||||
|
||||
|
||||
def test_get_conditions_returns_none_on_failure():
|
||||
fetcher = WeatherFetcher()
|
||||
with patch("vigilar.detection.weather.requests.get", side_effect=Exception("offline")):
|
||||
result = fetcher.get_conditions(45.0, -85.0)
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_get_conditions_caches():
|
||||
fetcher = WeatherFetcher()
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {"current": {"temperature_2m": 10.0, "weather_code": 0}}
|
||||
with patch("vigilar.detection.weather.requests.get", return_value=mock_response) as mock_get:
|
||||
fetcher.get_conditions(45.0, -85.0)
|
||||
fetcher.get_conditions(45.0, -85.0)
|
||||
assert mock_get.call_count == 1
|
||||
|
||||
|
||||
def test_weather_code_mapping():
|
||||
assert _weather_code_to_text(0) == "Clear sky"
|
||||
assert _weather_code_to_text(61) == "Light rain"
|
||||
assert _weather_code_to_text(999) == "Unknown"
|
||||
Reference in New Issue
Block a user