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

54 lines
1.8 KiB
Python

# tests/test_main.py
import pytest
from datetime import datetime
import main
def test_date_string_formatting():
# March 19, 2026 is a Thursday (weekday index 3)
dt = datetime(2026, 3, 19, 14, 5)
date_line, time_line = main.date_string(dt)
# Verify the translation dictionaries work properly
assert date_line == "Torsdag, 19 mars 2026"
assert time_line == "14:05" # Ensure the leading zero logic works
def test_is_network_up_success(mocker):
# Create a fake ping object that says the internet is alive
mock_host = mocker.MagicMock()
mock_host.is_alive = True
# Intercept the `_ping` function in main.py
mocker.patch("main._ping", return_value=mock_host)
assert main.is_network_up() is True
def test_is_network_up_failure(mocker):
# Simulate the ping throwing an exception (e.g., DNS failure or cable unplugged)
mocker.patch("main._ping", side_effect=Exception("Network down"))
# Assert our function catches it and safely returns False
assert main.is_network_up() is False
def test_update_orchestration(mocker):
# We want to test the update() function without making real web requests or drawing to the screen.
# So, we "mock" (fake) the inner components.
mocker.patch("main.weather.get", return_value={"temperature": 20.0})
mock_mqtt = mocker.patch("main.mqtt.send")
mock_make_image = mocker.patch(
"main.display.image.make_image", return_value="dummy_image_object"
)
mock_refresh = mocker.patch("main.display.refresh")
# Run the function
main.update(first=True)
# Verify that the orchestrator passed the data perfectly down the chain!
mock_mqtt.assert_called_once_with({"temperature": 20.0})
mock_make_image.assert_called_once()
mock_refresh.assert_called_once()