1
0

test_cli.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import unittest.mock
  2. import pytest
  3. import switchbot_mqtt
  4. @pytest.mark.parametrize(
  5. (
  6. "argv",
  7. "expected_mqtt_host",
  8. "expected_mqtt_port",
  9. "expected_username",
  10. "expected_password",
  11. ),
  12. [
  13. (
  14. ["", "--mqtt-host", "mqtt-broker.local"],
  15. "mqtt-broker.local",
  16. 1883,
  17. None,
  18. None,
  19. ),
  20. (
  21. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-port", "8883"],
  22. "mqtt-broker.local",
  23. 8883,
  24. None,
  25. None,
  26. ),
  27. (
  28. ["", "--mqtt-host", "mqtt-broker.local", "--mqtt-username", "me"],
  29. "mqtt-broker.local",
  30. 1883,
  31. "me",
  32. None,
  33. ),
  34. (
  35. [
  36. "",
  37. "--mqtt-host",
  38. "mqtt-broker.local",
  39. "--mqtt-username",
  40. "me",
  41. "--mqtt-password",
  42. "secret",
  43. ],
  44. "mqtt-broker.local",
  45. 1883,
  46. "me",
  47. "secret",
  48. ),
  49. ],
  50. )
  51. def test__main(
  52. argv, expected_mqtt_host, expected_mqtt_port, expected_username, expected_password
  53. ):
  54. with unittest.mock.patch("switchbot_mqtt._run") as run_mock, unittest.mock.patch(
  55. "sys.argv", argv
  56. ):
  57. # pylint: disable=protected-access
  58. switchbot_mqtt._main()
  59. run_mock.assert_called_once_with(
  60. mqtt_host=expected_mqtt_host,
  61. mqtt_port=expected_mqtt_port,
  62. mqtt_username=expected_username,
  63. mqtt_password=expected_password,
  64. )