Skip to content

Commit

Permalink
Restructure tests and remove remnants of old unittest (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
Orhideous committed Feb 22, 2024
1 parent 0884842 commit 78e4ecc
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 132 deletions.
46 changes: 0 additions & 46 deletions tests/abstract_test_roomba.py

This file was deleted.

47 changes: 47 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Tools and fixtures for tests."""
from dataclasses import dataclass

import pytest
from roombapy import Roomba, RoombaFactory

ROOMBA_CONFIG = {
"host": "127.0.0.1",
"username": "test",
"password": "test",
"name": "Roomba",
"continuous": True,
"delay": 120,
}


@dataclass
class Message:
"""MQTT-like message."""

topic: str
payload: bytes
qos: str = "qos"


@pytest.fixture()
def roomba() -> Roomba:
"""Mock for robot."""
return RoombaFactory.create_roomba(
address=ROOMBA_CONFIG["host"],
blid=ROOMBA_CONFIG["username"],
password=ROOMBA_CONFIG["password"],
continuous=ROOMBA_CONFIG["continuous"],
delay=ROOMBA_CONFIG["delay"],
)


@pytest.fixture()
def broken_roomba():
"""Mock for robot with broken credentials."""
return RoombaFactory.create_roomba(
address=ROOMBA_CONFIG["host"],
blid="wrong",
password=ROOMBA_CONFIG["password"],
continuous=ROOMBA_CONFIG["continuous"],
delay=ROOMBA_CONFIG["delay"],
)
19 changes: 6 additions & 13 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
from roombapy.discovery import RoombaDiscovery


class TestDiscovery:
"""Test the discovery module."""
def test_discovery_with_wrong_msg() -> None:
"""Test discovery with wrong message."""
discovery = RoombaDiscovery()
discovery.roomba_message = "test"
response = discovery.find()

def test_discovery_with_wrong_msg(self):
"""Test discovery with wrong message."""
# given
discovery = RoombaDiscovery()

# when
discovery.roomba_message = "test"
response = discovery.find()

# then
assert not response
assert not response
72 changes: 33 additions & 39 deletions tests/test_roomba.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
"""Test the Roomba class."""
from tests import abstract_test_roomba
from roombapy import Roomba

from tests.conftest import Message

class TestRoomba(abstract_test_roomba.AbstractTestRoomba):
"""Test the Roomba class."""

def test_roomba_with_data(self):
"""Test Roomba with data."""
# given
roomba = self.get_default_roomba()
def test_roomba_with_data(roomba: Roomba) -> None:
"""Test Roomba with data."""
roomba.on_message(
None,
None,
Message(
"topic",
b'{"state":{"reported":{"cleanSchedule":{"cycle":["none",'
b'"none","none","none","none","none","none"],"h":'
b'[9,11,11,11,11,11,9],"m":[0,0,0,0,0,0,0]},"language":0'
b',"cleanMissionStatus":{"cycle":"none","phase":"charge",'
b'"expireM":0,"rechrgM":0,"error":0,"notReady":0,"mssnM":108'
b',"sqft":0,"initiator":"","nMssn":209},"dock":{"known":true}'
b',"bin":{"present":true,"full":false},"batteryType":"lith",'
b'"batPct":100,"mobilityVer":"7375","bootloaderVer":"36",'
b'"soundVer":"13"}}}',
),
)
roomba.on_message(
None,
None,
Message(
"topic",
b'{"state":{"reported":{"signal":{"rssi":-38,"snr":52}}}}',
),
)

# when
roomba.on_message(
None,
None,
TestRoomba.get_message(
"topic",
b'{"state":{"reported":{"cleanSchedule":{"cycle":["none",'
b'"none","none","none","none","none","none"],"h":'
b'[9,11,11,11,11,11,9],"m":[0,0,0,0,0,0,0]},"language":0'
b',"cleanMissionStatus":{"cycle":"none","phase":"charge",'
b'"expireM":0,"rechrgM":0,"error":0,"notReady":0,"mssnM":108'
b',"sqft":0,"initiator":"","nMssn":209},"dock":{"known":true}'
b',"bin":{"present":true,"full":false},"batteryType":"lith",'
b'"batPct":100,"mobilityVer":"7375","bootloaderVer":"36",'
b'"soundVer":"13"}}}',
),
)
roomba.on_message(
None,
None,
TestRoomba.get_message(
"topic",
b'{"state":{"reported":{"signal":{"rssi":-38,"snr":52}}}}',
),
)
state = roomba.master_state

# then
state = roomba.master_state

assert state
assert state["state"]["reported"]["bin"]["present"]
assert not state["state"]["reported"]["bin"]["full"]
assert state["state"]["reported"]["batPct"] == 100
assert state
assert state["state"]["reported"]["bin"]["present"]
assert not state["state"]["reported"]["bin"]["full"]
assert state["state"]["reported"]["batPct"] == 100
60 changes: 26 additions & 34 deletions tests/test_roomba_integration.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
"""Test Roomba integration with the real device."""
"""Test Roomba integration with the mocked device."""
import asyncio
from asyncio import BaseEventLoop

import pytest
from roombapy import Roomba

from tests import abstract_test_roomba

@pytest.mark.asyncio()
async def test_roomba_connect(
roomba: Roomba, event_loop: BaseEventLoop
) -> None:
"""Connect to the Roomba."""
is_connected = await roomba_connect(roomba, event_loop)
await roomba_disconnect(roomba, event_loop)
assert is_connected

class TestRoombaIntegration(abstract_test_roomba.AbstractTestRoomba):
"""Test Roomba integration with the real device."""

@pytest.mark.asyncio()
async def test_roomba_connect(self, event_loop):
"""Test Roomba connect."""
# given
roomba = self.get_default_roomba()
@pytest.mark.asyncio()
async def test_roomba_connect_error(
broken_roomba: Roomba, event_loop: BaseEventLoop
) -> None:
"""Test Roomba connect error."""
is_connected = await roomba_connect(broken_roomba, event_loop)
assert not is_connected

# when
is_connected = await self.roomba_connect(roomba, event_loop)
await self.roomba_disconnect(roomba, event_loop)

# then
assert is_connected
async def roomba_connect(robot: Roomba, loop: BaseEventLoop) -> None:
"""Connect to the Roomba."""
await loop.run_in_executor(None, robot.connect)
await asyncio.sleep(1)
return robot.roomba_connected

@pytest.mark.asyncio()
async def test_roomba_connect_error(self, event_loop):
"""Test Roomba connect error."""
# given
roomba = self.get_default_roomba(blid="wrong")

# when
is_connected = await self.roomba_connect(roomba, event_loop)

# then
assert not is_connected

async def roomba_connect(self, roomba, loop):
"""Connect to the Roomba."""
await loop.run_in_executor(None, roomba.connect)
await asyncio.sleep(1)
return roomba.roomba_connected

async def roomba_disconnect(self, roomba, loop):
"""Disconnect from the Roomba."""
await loop.run_in_executor(None, roomba.disconnect)
async def roomba_disconnect(robot: Roomba, loop: BaseEventLoop) -> None:
"""Disconnect from the Roomba."""
await loop.run_in_executor(None, robot.disconnect)

0 comments on commit 78e4ecc

Please sign in to comment.