|
@@ -0,0 +1,94 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import pytest
|
|
|
+
|
|
|
+from switchbot_mqtt._utils import (
|
|
|
+ _mac_address_valid,
|
|
|
+ _MQTTTopicPlaceholder,
|
|
|
+ _parse_mqtt_topic,
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.parametrize(
|
|
|
+ ("mac_address", "valid"),
|
|
|
+ [
|
|
|
+ ("aa:bb:cc:dd:ee:ff", True),
|
|
|
+ ("AA:BB:CC:DD:EE:FF", True),
|
|
|
+ ("AA:12:34:45:67:89", True),
|
|
|
+ ("aabbccddeeff", False),
|
|
|
+ ("aa:bb:cc:dd:ee:gg", False),
|
|
|
+ ],
|
|
|
+)
|
|
|
+def test__mac_address_valid(mac_address, valid):
|
|
|
+
|
|
|
+ assert _mac_address_valid(mac_address) == valid
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.parametrize(
|
|
|
+ ("expected_levels", "topic", "expected_attrs"),
|
|
|
+ [
|
|
|
+ (
|
|
|
+ ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
|
|
|
+ "switchbot/aa:bb:cc:dd:ee:ff/set",
|
|
|
+ {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:ff"},
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
|
|
|
+ "switchbot//set",
|
|
|
+ {_MQTTTopicPlaceholder.MAC_ADDRESS: ""},
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ ["prefix", _MQTTTopicPlaceholder.MAC_ADDRESS],
|
|
|
+ "prefix/aa:bb:cc:dd:ee:ff",
|
|
|
+ {_MQTTTopicPlaceholder.MAC_ADDRESS: "aa:bb:cc:dd:ee:ff"},
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ [_MQTTTopicPlaceholder.MAC_ADDRESS],
|
|
|
+ "00:11:22:33:44:55",
|
|
|
+ {_MQTTTopicPlaceholder.MAC_ADDRESS: "00:11:22:33:44:55"},
|
|
|
+ ),
|
|
|
+ ],
|
|
|
+)
|
|
|
+def test__parse_mqtt_topic(expected_levels, topic, expected_attrs):
|
|
|
+ assert (
|
|
|
+ _parse_mqtt_topic(topic=topic, expected_levels=expected_levels)
|
|
|
+ == expected_attrs
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.parametrize(
|
|
|
+ ("expected_levels", "topic"),
|
|
|
+ [
|
|
|
+ (
|
|
|
+ ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
|
|
|
+ "switchbot/aa:bb:cc:dd:ee:ff",
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
|
|
|
+ "switchbot/aa:bb:cc:dd:ee:ff/change",
|
|
|
+ ),
|
|
|
+ (
|
|
|
+ ["switchbot", _MQTTTopicPlaceholder.MAC_ADDRESS, "set"],
|
|
|
+ "switchbot/aa:bb:cc:dd:ee:ff/set/suffix",
|
|
|
+ ),
|
|
|
+ ],
|
|
|
+)
|
|
|
+def test__parse_mqtt_topic_fail(expected_levels, topic):
|
|
|
+ with pytest.raises(ValueError):
|
|
|
+ _parse_mqtt_topic(topic=topic, expected_levels=expected_levels)
|