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