Skip to content

Commit

Permalink
Improve test coverage for core/Heartbeat.py (Pincer-org#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesigot authored and Gilles Igot committed Oct 30, 2021
1 parent c79b305 commit c1753c6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
36 changes: 36 additions & 0 deletions tests/core/test_heartbeat.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==6.2.5
pytest-asyncio==0.16.0
Empty file added tests/utils/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/utils/utils.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c1753c6

Please sign in to comment.