Files
wecker/tests/test_wecker.py
T
gurix 3a7b6ac7a3 refactor: styles own their ringing tone directly (Pattern C)
Drop the injected AlarmSound Protocol and the wecker.Sound class. Each style
now owns its tone by using pygame directly; the runner owns only the audio
engine lifecycle (mixer init/quit), button sampling, and cleanup. This is the
seam for future styles that handle their own tone — a 'talk' style would just
import pygame and play speech, with no shared interface to extend.

- styles/base.py: AlarmStyle.__init__(set_led, music_file) + start() + update()
- styles/blink.py: start() loads+plays music via pygame.mixer.music
- styles/simple.py: owns its beep — synthesises a square-wave buffer in
  module (stdlib array+math) and plays it via pygame.mixer.Sound
- wecker.py: setup() brings up the mixer only; run_alarm constructs the style
  and guards start() with a clean log+exit on failure

Tests: a shared tests/conftest.py stubs RPi.GPIO/pygame in sys.modules before
any SUT import (order-independent, removes duplicated inline mocking); the
wecker mock_pygame fixture patches one fresh pygame mock into wecker + both
style modules so assertions see the same calls.

README: tone-ownership and plugin-contract updated.
2026-08-02 21:50:00 +02:00

118 lines
4.2 KiB
Python

import pytest
from unittest.mock import MagicMock, patch
import styles.blink
import styles.simple
import wecker
@pytest.fixture
def mock_gpio():
with patch.object(wecker, "GPIO") as mock:
mock.LOW = 0
mock.HIGH = 1
yield mock
@pytest.fixture
def mock_pygame():
"""One fresh pygame mock shared by wecker and both styles.
Under Pattern C the styles use pygame directly, so a single mock must be
patched into wecker (setup) and the style modules (start) for assertions
to see the same calls.
"""
mock = MagicMock()
with (
patch.object(wecker, "pygame", mock),
patch.object(styles.blink, "pygame", mock),
patch.object(styles.simple, "pygame", mock),
):
yield mock
def test_set_led(mock_gpio):
wecker.set_led(True)
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.LOW)
wecker.set_led(False)
mock_gpio.output.assert_called_with(wecker.LED_PIN, mock_gpio.HIGH)
def test_setup_inits_mixer_and_gpio_not_music(mock_gpio, mock_pygame):
wecker.setup()
assert mock_pygame.mixer.pre_init.called
assert mock_pygame.mixer.init.called
assert mock_gpio.setmode.called
assert mock_gpio.setup.called
# setup() no longer loads music — that is now the style's job.
assert not mock_pygame.mixer.music.load.called
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()
def test_run_alarm_uses_selected_style(mock_gpio, mock_pygame):
"""run_alarm looks up the style, injects set_led + music_file, calls start()."""
fake = MagicMock()
fake.update.return_value = True # keep ringing
fake_style_cls = MagicMock(return_value=fake)
with patch("wecker.get_style", return_value=fake_style_cls) as get_style:
wecker.run_alarm(style_name="whatever", test_mode=True)
get_style.assert_called_once_with("whatever")
args = fake_style_cls.call_args.args
assert args[0] is wecker.set_led
assert isinstance(args[1], str) # resolved music file
fake.start.assert_called_once()
fake.update.assert_called()
def test_run_alarm_default_music_file(mock_gpio, mock_pygame, monkeypatch):
monkeypatch.delenv("MUSIC_FILE", raising=False)
fake = MagicMock()
fake.update.return_value = True
fake_style_cls = MagicMock(return_value=fake)
with patch("wecker.get_style", return_value=fake_style_cls):
wecker.run_alarm(test_mode=True)
assert fake_style_cls.call_args.args[1] == wecker.DEFAULT_MUSIC_FILE
def test_run_alarm_env_music_file(mock_gpio, mock_pygame, monkeypatch):
monkeypatch.setenv("MUSIC_FILE", "/env/track.mp3")
fake = MagicMock()
fake.update.return_value = True
fake_style_cls = MagicMock(return_value=fake)
with patch("wecker.get_style", return_value=fake_style_cls):
wecker.run_alarm(test_mode=True)
assert fake_style_cls.call_args.args[1] == "/env/track.mp3"
def test_run_alarm_music_file_arg_overrides_env(mock_gpio, mock_pygame, monkeypatch):
monkeypatch.setenv("MUSIC_FILE", "/env/track.mp3")
fake = MagicMock()
fake.update.return_value = True
fake_style_cls = MagicMock(return_value=fake)
with patch("wecker.get_style", return_value=fake_style_cls):
wecker.run_alarm(music_file="/arg/track.mp3", test_mode=True)
assert fake_style_cls.call_args.args[1] == "/arg/track.mp3"
def test_run_alarm_blink_plays_music(mock_gpio, mock_pygame):
"""The default (blink) style plays music on an endless loop."""
wecker.run_alarm(test_mode=True)
assert mock_pygame.mixer.music.play.called
assert mock_pygame.mixer.music.play.call_args.args[0] == -1 # endless loop
def test_run_alarm_simple_beeps_and_plays_no_music(mock_gpio, mock_pygame):
"""The simple style beeps and never touches the music stream."""
wecker.run_alarm(style_name="simple", test_mode=True)
assert mock_pygame.mixer.Sound.return_value.play.called # beep played
assert not mock_pygame.mixer.music.play.called # no music