93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
"""Tests for restart-free DSMR subscription management (apply_dsmr_subscription).
|
|||
|
|
|
||
|
|
These verify that toggling DSMR ingest / changing its topic / changing its sample
|
||
|
|
interval via the config UI is reflected in the live MQTT subscription without an
|
||
|
|
app restart. A fake MQTT manager is injected so no real broker is touched.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from unittest.mock import MagicMock
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from app.services import dsmr_ingest
|
||
|
|
|
||
|
|
|
||
|
|
class _FakeMqtt:
|
||
|
|
def __init__(self) -> None:
|
||
|
|
self.subscribe_calls: list[tuple[str, object]] = []
|
||
|
|
self.unsubscribe_calls: list[str] = []
|
||
|
|
|
||
|
|
def subscribe(self, topic: str, handler) -> None:
|
||
|
|
self.subscribe_calls.append((topic, handler))
|
||
|
|
|
||
|
|
def unsubscribe(self, topic: str) -> None:
|
||
|
|
self.unsubscribe_calls.append(topic)
|
||
|
|
|
||
|
|
|
||
|
|
def _settings(*, enabled: bool = True, topic: str = "dsmr/json", interval: int = 10):
|
||
|
|
s = MagicMock()
|
||
|
|
s.dsmr_ingest_enabled = enabled
|
||
|
|
s.dsmr_mqtt_topic = topic
|
||
|
|
s.dsmr_sample_interval_s = interval
|
||
|
|
return s
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture()
|
||
|
|
def fake_mqtt(monkeypatch):
|
||
|
|
fake = _FakeMqtt()
|
||
|
|
# apply_dsmr_subscription does `from app.integrations.mqtt import mqtt_manager`
|
||
|
|
# at call time, so patching the module attribute is picked up.
|
||
|
|
monkeypatch.setattr("app.integrations.mqtt.mqtt_manager", fake)
|
||
|
|
# Reset (and auto-restore) the module-level "currently subscribed topic".
|
||
|
|
monkeypatch.setattr(dsmr_ingest, "_current_dsmr_topic", None)
|
||
|
|
return fake
|
||
|
|
|
||
|
|
|
||
|
|
def test_enabled_subscribes_to_topic(fake_mqtt):
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(enabled=True, topic="dsmr/json"))
|
||
|
|
|
||
|
|
assert len(fake_mqtt.subscribe_calls) == 1
|
||
|
|
assert fake_mqtt.subscribe_calls[0][0] == "dsmr/json"
|
||
|
|
assert fake_mqtt.unsubscribe_calls == []
|
||
|
|
assert dsmr_ingest._current_dsmr_topic == "dsmr/json"
|
||
|
|
|
||
|
|
|
||
|
|
def test_disabled_after_enabled_unsubscribes(fake_mqtt):
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(enabled=True, topic="dsmr/json"))
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(enabled=False))
|
||
|
|
|
||
|
|
assert fake_mqtt.unsubscribe_calls == ["dsmr/json"]
|
||
|
|
assert dsmr_ingest._current_dsmr_topic is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_topic_change_unsubscribes_old_subscribes_new(fake_mqtt):
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(topic="dsmr/json"))
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(topic="meter/dsmr"))
|
||
|
|
|
||
|
|
assert fake_mqtt.unsubscribe_calls == ["dsmr/json"]
|
||
|
|
assert [t for t, _ in fake_mqtt.subscribe_calls] == ["dsmr/json", "meter/dsmr"]
|
||
|
|
assert dsmr_ingest._current_dsmr_topic == "meter/dsmr"
|
||
|
|
|
||
|
|
|
||
|
|
def test_reapply_same_topic_resubscribes_fresh_handler(fake_mqtt):
|
||
|
|
# A changed sample interval must take effect — the handler is re-bound to a
|
||
|
|
# fresh settings snapshot, so re-applying the same topic re-subscribes.
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(topic="dsmr/json", interval=10))
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(topic="dsmr/json", interval=20))
|
||
|
|
|
||
|
|
assert len(fake_mqtt.subscribe_calls) == 2
|
||
|
|
assert fake_mqtt.unsubscribe_calls == [] # same topic, no churn
|
||
|
|
handler1 = fake_mqtt.subscribe_calls[0][1]
|
||
|
|
handler2 = fake_mqtt.subscribe_calls[1][1]
|
||
|
|
assert handler1 is not handler2 # fresh closure carrying the new settings
|
||
|
|
|
||
|
|
|
||
|
|
def test_disabled_when_never_enabled_is_noop(fake_mqtt):
|
||
|
|
dsmr_ingest.apply_dsmr_subscription(_settings(enabled=False))
|
||
|
|
|
||
|
|
assert fake_mqtt.subscribe_calls == []
|
||
|
|
assert fake_mqtt.unsubscribe_calls == []
|
||
|
|
assert dsmr_ingest._current_dsmr_topic is None
|