From c1753c6fa08a90d48cf52db28c2afff0af34b7b6 Mon Sep 17 00:00:00 2001 From: Gilles Igot Date: Sat, 30 Oct 2021 14:19:12 +0200 Subject: [PATCH] Improve test coverage for core/Heartbeat.py (#121) --- Pipfile | 1 + tests/core/test_heartbeat.py | 36 ++++++++++++++++++++++++++++++++++++ tests/requirements.txt | 1 + tests/utils/__init__.py | 0 tests/utils/utils.py | 8 ++++++++ 5 files changed, 46 insertions(+) create mode 100644 tests/core/test_heartbeat.py create mode 100644 tests/utils/__init__.py create mode 100644 tests/utils/utils.py diff --git a/Pipfile b/Pipfile index 7b1892b1..a0b20a25 100644 --- a/Pipfile +++ b/Pipfile @@ -12,6 +12,7 @@ Pillow = "*" flake8 = "==4.0.1" tox = "==3.24.4" pytest = "==6.2.5" +pytest-asyncio = "==0.16.0" pytest-cov = "==3.0.0" mypy = "==0.910" twine = "==3.4.2" diff --git a/tests/core/test_heartbeat.py b/tests/core/test_heartbeat.py new file mode 100644 index 00000000..6a0e2a57 --- /dev/null +++ b/tests/core/test_heartbeat.py @@ -0,0 +1,36 @@ +# Copyright Pincer 2021-Present +# Full MIT License can be found in `LICENSE` at the project root. + +from unittest.mock import AsyncMock, Mock, patch, PropertyMock + +import pytest + +from pincer.core.heartbeat import Heartbeat +from tests.utils.utils import assert_not_raises + + +@pytest.fixture +def web_socket_client_protocol(): + return AsyncMock() + + +class TestHeartbeat: + def test_get(self): + assert Heartbeat.get() == 0 + + @pytest.mark.asyncio + async def test_handle_hello(self, web_socket_client_protocol): + payload = Mock() + payload.data.get.return_value = 1 + with patch("pincer.core.heartbeat.Heartbeat"): + await Heartbeat.handle_hello(web_socket_client_protocol, payload) + web_socket_client_protocol.send.assert_awaited_once() + + @pytest.mark.asyncio + async def test_handle_heartbeat(self, web_socket_client_protocol): + await Heartbeat.handle_heartbeat(web_socket_client_protocol, "GARBAGE") + web_socket_client_protocol.send.assert_awaited_once() + + def test_update_sequence(self): + with assert_not_raises(): + Heartbeat.update_sequence(42) diff --git a/tests/requirements.txt b/tests/requirements.txt index 92709451..4fa81360 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1 +1,2 @@ pytest==6.2.5 +pytest-asyncio==0.16.0 diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/utils/utils.py b/tests/utils/utils.py new file mode 100644 index 00000000..8c2795a3 --- /dev/null +++ b/tests/utils/utils.py @@ -0,0 +1,8 @@ +from contextlib import contextmanager + + +@contextmanager +def assert_not_raises(): + """Dummy context manager to highlight a row of a test + that should not raises any exception""" + yield