Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Oct 25, 2024
1 parent a9d50d2 commit 6e6f63a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
11 changes: 7 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from unittest.mock import MagicMock, patch, sentinel
from unittest.mock import AsyncMock, MagicMock, patch, sentinel

import pytest
import serial_asyncio
Expand Down Expand Up @@ -37,10 +37,13 @@ async def mock_conn(loop, protocol_factory, **kwargs):
await api.connect()


def test_close(api):
@pytest.mark.asyncio
async def test_disconnect(api):
uart = api._uart
api.close()
assert uart.close.call_count == 1
uart.disconnect = AsyncMock()

await api.disconnect()
assert uart.disconnect.call_count == 1
assert api._uart is None


Expand Down
12 changes: 6 additions & 6 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,28 @@ async def mock_get_network_state():

@pytest.mark.asyncio
async def test_disconnect_success(app):
api = MagicMock()
api = AsyncMock()

app._api = api
await app.disconnect()

api.close.assert_called_once()
api.disconnect.assert_called_once()
assert app._api is None


@pytest.mark.asyncio
async def test_disconnect_failure(app, caplog):
api = MagicMock()
api.disconnect = MagicMock(side_effect=RuntimeError("Broken"))
api = AsyncMock()
api.reset = AsyncMock(side_effect=RuntimeError("Broken"))

app._api = api

with caplog.at_level(logging.WARNING):
await app.disconnect()

assert "disconnect" in caplog.text
assert "Failed to reset before disconnect" in caplog.text

api.close.assert_called_once()
api.disconnect.assert_called_once()
assert app._api is None


Expand Down
7 changes: 0 additions & 7 deletions tests/test_uart.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ def test_escape(gw):
assert r == data_escaped


def test_length(gw):
data = b"\x80\x10\x00\x05\xaa\x00\x0f?\xf0\xff"
length = 5
r = gw._length(data)
assert r == length


def test_checksum(gw):
data = b"\x00\x0f?\xf0"
checksum = 0xAA
Expand Down

0 comments on commit 6e6f63a

Please sign in to comment.