Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlizio committed Dec 1, 2023
1 parent a5e6042 commit a70f1bb
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 107 deletions.
1 change: 1 addition & 0 deletions pyvlx/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# flake8: noqa

from .activate_scene import ActivateScene
from .api_event import ApiEvent
from .command_send import CommandSend
from .factory_default import FactoryDefault
from .get_all_nodes_information import GetAllNodesInformation
Expand Down
77 changes: 77 additions & 0 deletions test/command_send_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""Unit test for command send module."""
import unittest
from unittest.mock import AsyncMock, MagicMock, patch

from pyvlx import Parameter, PyVLXException
from pyvlx.api import CommandSend
from pyvlx.api.frames import (
CommandSendConfirmationStatus, FrameCommandRemainingTimeNotification,
FrameCommandRunStatusNotification, FrameCommandSendConfirmation,
FrameCommandSendRequest, FrameSessionFinishedNotification)


class TestCommandSend(unittest.IsolatedAsyncioTestCase):
"""Test class for CommandSend."""

def setUp(self) -> None:
"""Set up TestCommandSend."""
mocked_pyvlx = MagicMock()
self.command_send = CommandSend(pyvlx=mocked_pyvlx, node_id=23, parameter=Parameter())

async def test_handle_frame(self) -> None:
"""Test handle_frame function of CommandSend object."""
frame = MagicMock(spec=FrameCommandSendConfirmation)
session_id = 1
self.command_send.session_id = session_id
frame.session_id = session_id
self.command_send.wait_for_completion = False
frame.status = CommandSendConfirmationStatus.ACCEPTED
self.assertTrue(await self.command_send.handle_frame(frame=frame))
self.assertTrue(self.command_send.success)
self.command_send.success = False
self.command_send.wait_for_completion = True
frame.status = CommandSendConfirmationStatus.ACCEPTED
self.assertFalse(await self.command_send.handle_frame(frame=frame))
self.assertTrue(self.command_send.success)
self.command_send.success = False
self.command_send.wait_for_completion = False
frame.status = CommandSendConfirmationStatus.REJECTED
self.assertTrue(await self.command_send.handle_frame(frame=frame))
self.assertFalse(self.command_send.success)
self.command_send.success = False
self.command_send.wait_for_completion = True
frame.status = CommandSendConfirmationStatus.REJECTED
self.assertFalse(await self.command_send.handle_frame(frame=frame))
self.assertFalse(self.command_send.success)

frame = MagicMock(spec=FrameCommandRemainingTimeNotification)
frame.session_id = session_id
self.assertFalse(await self.command_send.handle_frame(frame=frame))

frame = MagicMock(spec=FrameCommandRunStatusNotification)
frame.session_id = session_id
self.assertFalse(await self.command_send.handle_frame(frame=frame))

frame = MagicMock(spec=FrameSessionFinishedNotification)
frame.session_id = session_id
self.assertTrue(await self.command_send.handle_frame(frame=frame))

@patch("pyvlx.api.ApiEvent.do_api_call", new_callable=AsyncMock)
async def test_send(self, do_api_call: AsyncMock) -> None:
"""Test send function of CommandSend object."""
self.command_send.success = True
await self.command_send.send()
assert do_api_call.called

self.command_send.success = False
with self.assertRaises(PyVLXException):
await self.command_send.send()

@patch("pyvlx.api.command_send.get_new_session_id", callable=MagicMock)
def test_request_frame(self, new_session_id_request: MagicMock) -> None:
"""Test request_frame function of CommandSend object."""
self.command_send.session_id = 1
new_session_id_request.return_value = 5
assert isinstance(self.command_send.request_frame(), FrameCommandSendRequest)
assert new_session_id_request.called
assert self.command_send.session_id == 5
14 changes: 7 additions & 7 deletions test/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@
class TestConfig(unittest.TestCase):
"""Test class for configuration."""

def test_config_file(self):
def test_config_file(self) -> None:
"""Test host/password configuration via config.yaml."""
pyvlx = PyVLX("test/data/test_config.yaml")
self.assertEqual(pyvlx.config.password, "velux123")
self.assertEqual(pyvlx.config.host, "192.168.2.127")
self.assertEqual(pyvlx.config.port, 51200)

def test_config_file_with_port(self):
def test_config_file_with_port(self) -> None:
"""Test host/password configuration via config.yaml."""
pyvlx = PyVLX("test/data/test_config_with_port.yaml")
self.assertEqual(pyvlx.config.port, 1234)

def test_config_explicit(self):
def test_config_explicit(self) -> None:
"""Test host/password configuration via parameter."""
pyvlx = PyVLX(host="192.168.2.127", password="velux123")
self.assertEqual(pyvlx.config.password, "velux123")
self.assertEqual(pyvlx.config.host, "192.168.2.127")
self.assertEqual(pyvlx.config.port, 51200)

def test_config_wrong1(self):
def test_config_wrong1(self) -> None:
"""Test configuration with missing password."""
with self.assertRaises(PyVLXException):
PyVLX("test/data/test_config_wrong1.yaml")

def test_config_wrong2(self):
def test_config_wrong2(self) -> None:
"""Test configuration with missing host."""
with self.assertRaises(PyVLXException):
PyVLX("test/data/test_config_wrong2.yaml")

def test_config_wrong3(self):
def test_config_wrong3(self) -> None:
"""Test configuration with missing config node."""
with self.assertRaises(PyVLXException):
PyVLX("test/data/test_config_wrong3.yaml")

def test_config_non_existent(self):
def test_config_non_existent(self) -> None:
"""Test non existing configuration path."""
with self.assertRaises(PyVLXException):
PyVLX("test/data/test_config_non_existent.yaml")
8 changes: 2 additions & 6 deletions test/get_limitation_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Unit test for limitation."""
import asyncio
import unittest
from unittest.mock import MagicMock

import pytest
from pytest import FixtureRequest
Expand Down Expand Up @@ -28,11 +29,10 @@ class TestGetLimitation(unittest.TestCase):

def setUp(self) -> None:
"""Set up TestGetLimitation."""
self.pyvlx = PyVLX()
self.pyvlx = MagicMock(spec=PyVLX)

def test_get_name(self) -> None:
"""Test get_name()."""
self.pyvlx = PyVLX()
limit = GetLimitation(self.pyvlx, 1)
self.assertEqual(limit.node_id, 1)
self.assertEqual(limit.limitation_type, LimitationType.MIN_LIMITATION)
Expand All @@ -42,21 +42,18 @@ def test_get_name(self) -> None:

def test_max_value(self) -> None:
"""Test limit.max_value."""
self.pyvlx = PyVLX()
limit = GetLimitation(self.pyvlx, 1)
limit.max_value_raw = b'\xf7'
self.assertEqual(limit.max_value, 124)

def test_min_value(self) -> None:
"""Test limit.min_value."""
self.pyvlx = PyVLX()
limit = GetLimitation(self.pyvlx, 1)
limit.min_value_raw = b'\xba'
self.assertEqual(limit.min_value, 93)

def test_handle_frame(self) -> None:
"""Test handle frame."""
self.pyvlx = PyVLX()
limit = GetLimitation(self.pyvlx, 1)

frame = FrameGetLimitationStatus()
Expand Down Expand Up @@ -91,7 +88,6 @@ def test_handle_frame(self) -> None:

def test_request_frame(self) -> None:
"""Test initiating frame."""
self.pyvlx = PyVLX()
limit = GetLimitation(self.pyvlx, 1)
req_frame = limit.request_frame()
self.assertIsInstance(req_frame, FrameGetLimitationStatus)
Expand Down
18 changes: 10 additions & 8 deletions test/lightening_device_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Unit test for lights."""
import unittest
from unittest.mock import MagicMock

from pyvlx import Light, PyVLX

Expand All @@ -8,11 +9,14 @@
class TestLighteningDevice(unittest.TestCase):
"""Test class for lights."""

def setUp(self) -> None:
"""Set up TestGetLimitation."""
self.pyvlx = MagicMock(spec=PyVLX)

def test_light_str(self) -> None:
"""Test string representation of Light object."""
pyvlx = PyVLX()
light = Light(
pyvlx=pyvlx,
pyvlx=self.pyvlx,
node_id=23,
name="Test Light",
serial_number="aa:bb:aa:bb:aa:bb:aa:23",
Expand All @@ -24,22 +28,20 @@ def test_light_str(self) -> None:

def test_eq(self) -> None:
"""Testing eq method with positive results."""
pyvlx = PyVLX()
node1 = Light(
pyvlx=pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
pyvlx=self.pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
)
node2 = Light(
pyvlx=pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
pyvlx=self.pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
)
self.assertEqual(node1, node2)

def test_nq(self) -> None:
"""Testing eq method with negative results."""
pyvlx = PyVLX()
node1 = Light(
pyvlx=pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
pyvlx=self.pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
)
node2 = Light(
pyvlx=pyvlx, node_id=24, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
pyvlx=self.pyvlx, node_id=24, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
)
self.assertNotEqual(node1, node2)
Loading

0 comments on commit a70f1bb

Please sign in to comment.