Files
second-screen/tests/test_weather_data.py
rickzkmandClaude Opus 5.5 6c9a1420d2 Week ahead in the weather strip, reverse LCD at night
The weekday strip now shows today and the next six days (icon, max, min),
the current temperature and feels-like sit next to the clock, and the rain
graph spans the full width with the hour under every bar. From sunset to
sunrise the whole frame, seconds patch included, is drawn as a reverse LCD.
The rain label is now spelled DÉŠŤ.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 22:40:34 +01:00

149 lines
4.5 KiB
Python

import copy
import json
from datetime import date, datetime
from pathlib import Path
import weather_data
from weather_data import Day, Icon, icon_for, parse_weather
FIXTURE = json.loads((Path(__file__).parent / "fixtures" / "open-meteo-zlin.json").read_text())
NOW = datetime(2026, 10, 22, 12, 45)
def payload():
return copy.deepcopy(FIXTURE)
def test_parse_fixture_window():
w = parse_weather(payload(), NOW)
assert len(w.hours) == 12
assert w.hours[0].time == datetime(2026, 10, 22, 12, 0)
assert w.hours[-1].time == datetime(2026, 10, 22, 23, 0)
assert w.hours[0].rain_pct == FIXTURE["hourly"]["precipitation_probability"][12]
assert w.hours[0].mm == FIXTURE["hourly"]["precipitation"][12]
assert (w.temp, w.code) == (10.2, 61)
def test_parse_week_with_sunrise_and_sunset():
w = parse_weather(payload(), NOW)
assert len(w.days) == 7
assert w.days[0] == Day(date(2026, 10, 22), 63, 15.0, 6.0,
datetime(2026, 10, 22, 7, 20), datetime(2026, 10, 22, 17, 45))
assert w.days[6] == Day(date(2026, 10, 28), 45, 11.5, -0.5,
datetime(2026, 10, 28, 6, 30), datetime(2026, 10, 28, 16, 35))
def test_parse_feels_like():
assert parse_weather(payload(), NOW).feels == 8.1
def test_null_day_entries_become_none():
p = payload()
p["daily"]["weather_code"][1] = None
p["daily"]["sunset"][1] = None
day = parse_weather(p, NOW).days[1]
assert (day.code, day.sunset) == (None, None)
assert day.sunrise == datetime(2026, 10, 23, 7, 22)
def test_short_daily_lists_give_none_not_errors():
p = payload()
p["daily"]["temperature_2m_min"] = p["daily"]["temperature_2m_min"][:3]
w = parse_weather(p, NOW)
assert len(w.days) == 7
assert (w.days[2].low, w.days[3].low) == (4.3, None)
def test_window_runs_into_tomorrow_late_in_the_day():
w = parse_weather(payload(), datetime(2026, 10, 22, 20, 10))
assert w.hours[0].time == datetime(2026, 10, 22, 20, 0)
assert w.hours[-1].time == datetime(2026, 10, 23, 7, 0)
def test_icon_mapping():
assert icon_for(0) is Icon.SUN
assert icon_for(2) is Icon.PARTLY
assert icon_for(3) is Icon.CLOUDY
assert icon_for(45) is Icon.FOG
assert icon_for(61) is Icon.RAIN
assert icon_for(81) is Icon.RAIN
assert icon_for(73) is Icon.SNOW
assert icon_for(95) is Icon.STORM
assert icon_for(999) is None
assert icon_for(None) is None
def test_null_entries_become_none():
p = payload()
p["hourly"]["precipitation_probability"][13] = None
p["hourly"]["precipitation"][13] = None
w = parse_weather(p, NOW)
assert (w.hours[1].rain_pct, w.hours[1].mm) == (None, None)
assert w.hours[0].rain_pct is not None
def test_short_hourly_gives_fewer_hours():
p = payload()
for key in ("time", "precipitation_probability", "precipitation"):
p["hourly"][key] = p["hourly"][key][:14]
assert len(parse_weather(p, NOW).hours) == 2
def test_missing_current_gives_none_temp():
p = payload()
del p["current"]
w = parse_weather(p, NOW)
assert (w.temp, w.code) == (None, None)
assert len(w.hours) == 12
def test_missing_sections_do_not_raise():
w = parse_weather({}, NOW)
assert (w.temp, w.feels, w.days, w.hours) == (None, None, (), ())
def test_fetch_passes_coordinates():
calls = []
class Response:
def raise_for_status(self):
pass
def json(self):
return payload()
def fake_get(url, params=None, timeout=None):
calls.append((url, params, timeout))
return Response()
w = weather_data.fetch_weather(NOW, get=fake_get)
url, params, timeout = calls[0]
assert url.startswith("https://api.open-meteo.com/")
assert params["latitude"] == weather_data.LAT == 49.2265
assert params["longitude"] == weather_data.LON == 17.6627
assert params["timezone"] == "Europe/Prague"
assert timeout == 10
assert len(w.hours) == 12
assert len(w.days) == 7
def test_fetch_asks_for_a_week_with_sun_times_and_feels_like():
seen = {}
class Response:
def raise_for_status(self):
pass
def json(self):
return payload()
def fake_get(url, params=None, timeout=None):
seen.update(params)
return Response()
weather_data.fetch_weather(NOW, get=fake_get)
assert seen["forecast_days"] >= 7
assert set(seen["daily"].split(",")) >= {"weather_code", "temperature_2m_max", "temperature_2m_min",
"sunrise", "sunset"}
assert "apparent_temperature" in seen["current"].split(",")