Files
weather-base-station/tests/test_image_maker.py
T

84 lines
2.5 KiB
Python

# tests/test_image_maker.py
import pytest
from PIL import Image
from display.image_maker import closest, eInkImage
def test_closest_math_logic():
# Test that your wind direction logic snaps to the correct cardinal degree
angles = [0, 45, 90, 135, 180, 225, 270, 315]
assert closest(angles, 10) == 0 # 10 is closest to 0
assert closest(angles, 85) == 90 # 85 is closest to 90
assert closest(angles, 190) == 180 # 190 is closest to 180
assert closest(angles, 100) == 90
def test_make_image_compilation():
"""
Tests the entire Pillow drawing loop.
If you miss a .get() check or use an invalid font size, this test will catch it.
"""
image_maker = eInkImage()
# A perfectly standard mock payload simulating SMHI and sensors
dummy_weather_data = {
"indoor_temp": 22.5,
"indoor_humidity": 45,
"pressure": 1013,
"sunrise": "06:30",
"sunset": "18:45",
"moon_illumination": 0.8,
"moon_phase": "Fullmåne",
"moon_to_full": False,
"temperature": 15.2,
"humidity": 60,
"dew": 5.0,
"uv_index": 3,
"fire_index": 1,
"weather_alerts": [],
"forecast": {
"wind": {"deg": 180, "speed": 5.0, "gust": 7.5},
"weather_symbol": 1,
},
}
try:
# Generate the image
img = image_maker.make_image(
date=("Torsdag, 19 mars 2026", "22:10"),
weather_data=dummy_weather_data,
network_status=True,
)
# Verify it successfully created an image object
assert isinstance(img, Image.Image)
# Verify the dimensions match your Waveshare 4.2inch E-ink display
assert img.size == (400, 300)
# Verify it converted it to a 1-bit color palette (pure black and white) for the e-ink screen
assert img.mode == "L"
except Exception as e:
pytest.fail(f"Image generation crashed with valid data: {e}")
def test_make_image_with_missing_data():
"""
Tests that the image maker doesn't crash if the internet is down
and the weather data is mostly empty.
"""
image_maker = eInkImage()
try:
img = image_maker.make_image(
date=("Torsdag, 19 mars 2026", "22:10"),
weather_data={},
network_status=False,
)
assert isinstance(img, Image.Image)
except Exception as e:
pytest.fail(f"Image generation crashed when weather data was missing: {e}")