瀏覽代碼

increase test coverage of msg handler

Fabian Peter Hammerle 4 年之前
父節點
當前提交
17d77a5903
共有 1 個文件被更改,包括 35 次插入0 次删除
  1. 35 0
      tests/test_mqtt.py

+ 35 - 0
tests/test_mqtt.py

@@ -82,6 +82,24 @@ def test__run_authentication_missing_username(mqtt_host, mqtt_port, mqtt_passwor
             )
 
 
+def test__mqtt_on_message_retained(caplog):
+    message = MQTTMessage(topic=b"intertechno-cc1101/12345678/0/set")
+    message.payload = b"ON"
+    message.retain = True
+    with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
+        with caplog.at_level(logging.DEBUG):
+            intertechno_cc1101_mqtt._mqtt_on_message("dummy", None, message)
+    remote_control_mock.assert_not_called()
+    assert caplog.record_tuples == [
+        (
+            "intertechno_cc1101_mqtt",
+            logging.DEBUG,
+            "received topic=intertechno-cc1101/12345678/0/set payload=b'ON'",
+        ),
+        ("intertechno_cc1101_mqtt", logging.WARNING, "ignoring retained message"),
+    ]
+
+
 @pytest.mark.parametrize(
     ("topic", "address"),
     (
@@ -200,3 +218,20 @@ def test__mqtt_on_message_transmission_failed(caplog):
     assert caplog.records[0].levelno == logging.ERROR
     assert caplog.records[0].message == "failed to send signal"
     assert isinstance(caplog.records[0].exc_info[1], FileNotFoundError)
+
+
+@pytest.mark.parametrize("payload", (b"EIN", b"aus", b""))
+def test__mqtt_on_message_invalid_payload(caplog, payload):
+    message = MQTTMessage(topic=b"intertechno-cc1101/1234/7/set")
+    message.payload = payload
+    with unittest.mock.patch("intertechno_cc1101.RemoteControl") as remote_control_mock:
+        intertechno_cc1101_mqtt._mqtt_on_message("dummy", None, message)
+    remote_control_mock().turn_off.assert_not_called()
+    remote_control_mock().turn_on.assert_not_called()
+    assert caplog.record_tuples == [
+        (
+            "intertechno_cc1101_mqtt",
+            30,
+            "unexpected payload {!r}; expected 'ON' or 'OFF'".format(payload),
+        )
+    ]