test_actor_base.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # switchbot-mqtt - MQTT client controlling SwitchBot button & curtain automators,
  2. # compatible with home-assistant.io's MQTT Switch & Cover platform
  3. #
  4. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import typing
  19. import unittest.mock
  20. import bleak.backends.device
  21. import paho.mqtt.client
  22. import pytest
  23. import switchbot
  24. import switchbot_mqtt._actors.base
  25. # pylint: disable=protected-access
  26. def test_abstract() -> None:
  27. with pytest.raises(TypeError, match=r"\babstract class\b"):
  28. # pylint: disable=abstract-class-instantiated
  29. switchbot_mqtt._actors.base._MQTTControlledActor( # type: ignore
  30. device=unittest.mock.Mock(), retry_count=21, password=None
  31. )
  32. @pytest.mark.asyncio
  33. async def test_execute_command_abstract() -> None:
  34. class _ActorMock(switchbot_mqtt._actors.base._MQTTControlledActor):
  35. # pylint: disable=duplicate-code
  36. def __init__(
  37. self,
  38. device: bleak.backends.device.BLEDevice,
  39. retry_count: int,
  40. password: typing.Optional[str],
  41. ) -> None:
  42. super().__init__(device=device, retry_count=retry_count, password=password)
  43. async def execute_command(
  44. self,
  45. *,
  46. mqtt_message_payload: bytes,
  47. mqtt_client: paho.mqtt.client.Client,
  48. update_device_info: bool,
  49. mqtt_topic_prefix: str,
  50. ) -> None:
  51. assert 21
  52. await super().execute_command( # type: ignore
  53. mqtt_message_payload=mqtt_message_payload,
  54. mqtt_client=mqtt_client,
  55. update_device_info=update_device_info,
  56. mqtt_topic_prefix=mqtt_topic_prefix,
  57. )
  58. def _get_device(self) -> switchbot.SwitchbotDevice:
  59. assert 42
  60. return super()._get_device()
  61. with pytest.raises(TypeError) as exc_info:
  62. # pylint: disable=abstract-class-instantiated
  63. switchbot_mqtt._actors.base._MQTTControlledActor( # type: ignore
  64. device=unittest.mock.Mock(), retry_count=42, password=None
  65. )
  66. exc_info.match(
  67. r"^Can't instantiate abstract class _MQTTControlledActor"
  68. r" with abstract methods __init__, _get_device, execute_command$"
  69. )
  70. actor = _ActorMock(device=unittest.mock.Mock(), retry_count=42, password=None)
  71. with pytest.raises(NotImplementedError):
  72. await actor.execute_command(
  73. mqtt_message_payload=b"dummy",
  74. mqtt_client="dummy",
  75. update_device_info=True,
  76. mqtt_topic_prefix="whatever",
  77. )
  78. with pytest.raises(NotImplementedError):
  79. actor._get_device()