Browse Source

raise coverage

Fabian Peter Hammerle 3 years ago
parent
commit
e4c9f6f312
3 changed files with 24 additions and 2 deletions
  1. 1 1
      .github/workflows/python.yml
  2. 1 1
      systemctl_mqtt/__init__.py
  3. 22 0
      tests/test_dbus.py

+ 1 - 1
.github/workflows/python.yml

@@ -48,6 +48,6 @@ jobs:
     - run: pip install 'coveralls>=1.9.0,<2,!=1.11.0'
     # https://github.com/coverallsapp/github-action/issues/30
     # https://github.com/coverallsapp/github-action/issues/4#issuecomment-529399410
-    - run: coveralls
+    - run: coveralls || echo TODO
       env:
         COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

+ 1 - 1
systemctl_mqtt/__init__.py

@@ -55,7 +55,7 @@ def _schedule_shutdown(action: str) -> None:
     try:
         _get_login_manager().ScheduleShutdown(action, shutdown_epoch_usec)
     except dbus.DBusException as exc:
-        _LOGGER.error("failed to schedule shutdown: %s", exc.get_dbus_message())
+        _LOGGER.error("failed to schedule %s: %s", action, exc.get_dbus_message())
 
 
 _MQTT_TOPIC_SUFFIX_ACTION_MAPPING = {

+ 22 - 0
tests/test_dbus.py

@@ -16,6 +16,7 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 import datetime
+import logging
 import unittest.mock
 
 import dbus
@@ -57,6 +58,27 @@ def test__schedule_shutdown(action):
     assert not schedule_kwargs
 
 
+@pytest.mark.parametrize("action", ["poweroff"])
+@pytest.mark.parametrize("exception_message", ["test message"])
+def test__schedule_shutdown_fail(caplog, action, exception_message):
+    login_manager_mock = unittest.mock.MagicMock()
+    login_manager_mock.ScheduleShutdown.side_effect = dbus.DBusException(
+        exception_message
+    )
+    with unittest.mock.patch(
+        "systemctl_mqtt._get_login_manager", return_value=login_manager_mock
+    ), caplog.at_level(logging.DEBUG):
+        systemctl_mqtt._schedule_shutdown(action=action)
+    login_manager_mock.ScheduleShutdown.assert_called_once()
+    assert len(caplog.records) == 2
+    assert caplog.records[0].levelno == logging.INFO
+    assert caplog.records[0].message.startswith("scheduling {} for ".format(action))
+    assert caplog.records[1].levelno == logging.ERROR
+    assert caplog.records[1].message == "failed to schedule {}: {}".format(
+        action, exception_message
+    )
+
+
 @pytest.mark.parametrize(
     ("topic_suffix", "expected_action_arg"), [("poweroff", "poweroff")]
 )