|
@@ -0,0 +1,78 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import os
|
|
|
+import re
|
|
|
+import unittest.mock
|
|
|
+
|
|
|
+import bluepy.btle
|
|
|
+import pytest
|
|
|
+
|
|
|
+import switchbot_mqtt
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def test__update_position_le_on_permission_denied():
|
|
|
+ actor = switchbot_mqtt._CurtainMotor(
|
|
|
+ mac_address="dummy", retry_count=21, password=None
|
|
|
+ )
|
|
|
+ with unittest.mock.patch(
|
|
|
+ "switchbot.SwitchbotCurtain.update",
|
|
|
+ side_effect=bluepy.btle.BTLEManagementError(
|
|
|
+ "Failed to execute management command 'le on'",
|
|
|
+ {
|
|
|
+ "rsp": ["mgmt"],
|
|
|
+ "code": ["mgmterr"],
|
|
|
+ "estat": [20],
|
|
|
+ "emsg": ["Permission Denied"],
|
|
|
+ },
|
|
|
+ ),
|
|
|
+ ) as update_mock, unittest.mock.patch.object(
|
|
|
+ actor, "_report_position"
|
|
|
+ ) as report_position_mock, pytest.raises(
|
|
|
+ PermissionError
|
|
|
+ ) as exc_info:
|
|
|
+ actor._update_position(mqtt_client="client")
|
|
|
+ update_mock.assert_called_once_with()
|
|
|
+ report_position_mock.assert_not_called()
|
|
|
+ assert os.path.isfile(
|
|
|
+ re.search(
|
|
|
+ r"sudo setcap cap_net_admin\+ep (\S+/bluepy-helper)\b",
|
|
|
+ exc_info.exconly(),
|
|
|
+ ).group(1)
|
|
|
+ )
|
|
|
+ assert isinstance(exc_info.value.__cause__, bluepy.btle.BTLEManagementError)
|
|
|
+
|
|
|
+
|
|
|
+def test__update_position_other_error():
|
|
|
+ actor = switchbot_mqtt._CurtainMotor(
|
|
|
+ mac_address="dummy", retry_count=21, password=None
|
|
|
+ )
|
|
|
+ side_effect = bluepy.btle.BTLEManagementError("test")
|
|
|
+ with unittest.mock.patch(
|
|
|
+ "switchbot.SwitchbotCurtain.update", side_effect=side_effect
|
|
|
+ ) as update_mock, unittest.mock.patch.object(
|
|
|
+ actor, "_report_position"
|
|
|
+ ) as report_position_mock, pytest.raises(
|
|
|
+ type(side_effect)
|
|
|
+ ) as exc_info:
|
|
|
+ actor._update_position(mqtt_client="client")
|
|
|
+ update_mock.assert_called_once_with()
|
|
|
+ report_position_mock.assert_not_called()
|
|
|
+ assert exc_info.value == side_effect
|