2026-05-09 22:31:28 +02:00
|
|
|
import pytest
|
|
|
|
|
import sys
|
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
|
|
|
|
|
# Mock RPi.GPIO and pygame before importing wecker
|
2026-05-11 15:05:29 +02:00
|
|
|
sys.modules["RPi"] = MagicMock()
|
|
|
|
|
sys.modules["RPi.GPIO"] = MagicMock()
|
|
|
|
|
sys.modules["pygame"] = MagicMock()
|
2026-05-09 22:31:28 +02:00
|
|
|
|
|
|
|
|
import wecker # noqa: E402
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_gpio():
|
2026-05-11 15:05:29 +02:00
|
|
|
with patch.object(wecker, "GPIO") as mock:
|
2026-05-09 22:31:28 +02:00
|
|
|
mock.LOW = 0
|
|
|
|
|
mock.HIGH = 1
|
|
|
|
|
yield mock
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_pygame():
|
2026-05-11 15:05:29 +02:00
|
|
|
with patch.object(wecker, "pygame") as mock:
|
2026-05-09 22:31:28 +02:00
|
|
|
yield mock
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
def test_set_led(mock_gpio):
|
|
|
|
|
wecker.set_led(True)
|
|
|
|
|
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.LOW)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-05-09 22:31:28 +02:00
|
|
|
wecker.set_led(False)
|
|
|
|
|
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.HIGH)
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
|
|
|
|
@patch("wecker.time.time")
|
2026-07-31 16:19:14 +02:00
|
|
|
def test_run_alarm_plays_music(mock_time, mock_gpio, mock_pygame):
|
2026-05-09 22:31:28 +02:00
|
|
|
mock_pygame.mixer.get_init.return_value = True
|
|
|
|
|
wecker.run_alarm(test_mode=True)
|
|
|
|
|
assert mock_pygame.mixer.music.play.called
|
|
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
def test_run_alarm_unknown_style_raises(mock_gpio, mock_pygame):
|
|
|
|
|
"""An unknown style is rejected before any hardware is touched."""
|
|
|
|
|
with pytest.raises(ValueError, match="Unknown alarm style"):
|
|
|
|
|
wecker.run_alarm(style_name="nope", test_mode=True)
|
|
|
|
|
mock_pygame.mixer.init.assert_not_called()
|
2026-05-09 22:31:28 +02:00
|
|
|
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
def test_run_alarm_uses_selected_style(mock_gpio, mock_pygame):
|
|
|
|
|
"""run_alarm looks up the style by name and drives the returned style."""
|
|
|
|
|
mock_pygame.mixer.get_init.return_value = True
|
|
|
|
|
fake = MagicMock()
|
|
|
|
|
fake.update.return_value = True # keep ringing
|
|
|
|
|
fake_style_cls = MagicMock(return_value=fake)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
with patch("wecker.get_style", return_value=fake_style_cls) as get_style:
|
|
|
|
|
wecker.run_alarm(style_name="whatever", test_mode=True)
|
2026-05-11 15:05:29 +02:00
|
|
|
|
2026-07-31 16:19:14 +02:00
|
|
|
get_style.assert_called_once_with("whatever")
|
|
|
|
|
fake_style_cls.assert_called_once_with(wecker.set_led)
|
|
|
|
|
fake.update.assert_called()
|
2026-06-18 17:01:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_setup_uses_env_music_file(mock_gpio, mock_pygame, monkeypatch):
|
|
|
|
|
monkeypatch.setenv("MUSIC_FILE", "/custom/track.mp3")
|
|
|
|
|
wecker.setup()
|
|
|
|
|
mock_pygame.mixer.music.load.assert_called_with("/custom/track.mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_setup_music_file_argument_overrides_env(mock_gpio, mock_pygame, monkeypatch):
|
|
|
|
|
monkeypatch.setenv("MUSIC_FILE", "/env/track.mp3")
|
|
|
|
|
wecker.setup(music_file="/arg/track.mp3")
|
|
|
|
|
mock_pygame.mixer.music.load.assert_called_with("/arg/track.mp3")
|