Skip to content

Commit

Permalink
Add further tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlizio committed Dec 1, 2023
1 parent 4d1d0b7 commit 6771194
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 22 deletions.
8 changes: 4 additions & 4 deletions pyvlx/opening_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(
pyvlx: "PyVLX",
node_id: int,
name: str,
serial_number: Optional[str],
serial_number: Optional[str] = None,
position_parameter: Parameter = Parameter(),
):
"""Initialize opening device.
Expand Down Expand Up @@ -296,7 +296,7 @@ async def set_position_and_orientation(
* position: Position object containing the current position.
* velocity: Velocity to be used during transition.
* target_position: Position object holding the target position
which allows to ajust the position while the blind is in movement
which allows to adjust the position while the blind is in movement
without stopping the blind (if orientation position has been changed.)
* wait_for_completion: If set, function will return
after device has reached target position.
Expand Down Expand Up @@ -352,7 +352,7 @@ async def set_position(
* position: Position object containing the current position.
* velocity: Velocity to be used during transition.
* target_position: Position object holding the target position
which allows to ajust the position while the blind is in movement
which allows to adjust the position while the blind is in movement
without stopping the blind (if orientation position has been changed.)
* wait_for_completion: If set, function will return
after device has reached target position.
Expand Down Expand Up @@ -515,7 +515,7 @@ async def set_position(
Parameters:
* position: Position object containing the current position.
* target_position: Position object holding the target position
which allows to ajust the position while the blind is in movement
which allows to adjust the position while the blind is in movement
* wait_for_completion: If set, function will return
after device has reached target position.
"""
Expand Down
75 changes: 64 additions & 11 deletions test/opening_device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,79 @@
from unittest.mock import AsyncMock, MagicMock, patch

from pyvlx import (
Awning, Blade, Blind, OpeningDevice, Parameter, Position, RollerShutter,
Window)
Awning, Blade, Blind, CurrentPosition, OpeningDevice, Parameter, Position,
PyVLX, RollerShutter, Window)
from pyvlx.const import Velocity


# pylint: disable=too-many-public-methods,invalid-name
class TestOpeningDevice(IsolatedAsyncioTestCase):
"""Test class for roller shutter."""

mocked_pyvlx = MagicMock(spec=PyVLX)

@patch("pyvlx.api.CommandSend.send", new_callable=AsyncMock)
@patch("pyvlx.Node.after_update", new_callable=AsyncMock)
async def test_set_position(self, commandSend: AsyncMock, afterUpdate: AsyncMock) -> None:
"""Test set_position of OpeningDevice object."""
test_device = OpeningDevice(pyvlx="PyVLX", node_id=23, name="Test device", serial_number=None)
await test_device.set_position(position=Position(position_percent=100))
opening_device = OpeningDevice(pyvlx=self.mocked_pyvlx, node_id=23, name="Test device")
await opening_device.set_position(position=Position(position_percent=100))
assert commandSend.called
assert afterUpdate.called

@patch("pyvlx.opening_device.OpeningDevice.set_position", new_callable=AsyncMock)
async def test_open(self, set_position: AsyncMock) -> None:
"""Test open function of OpeningDevice object."""
opening_device = OpeningDevice(pyvlx=self.mocked_pyvlx, node_id=23, name="Test device")
velocity = Velocity.DEFAULT
wait_for_completion = False
await opening_device.open(velocity=velocity, wait_for_completion=wait_for_completion)
set_position.assert_awaited_once_with(
position=Position(position_percent=opening_device.open_position_target),
velocity=velocity,
wait_for_completion=wait_for_completion)

@patch("pyvlx.opening_device.OpeningDevice.set_position", new_callable=AsyncMock)
async def test_close(self, set_position: AsyncMock) -> None:
"""Test close function of OpeningDevice object."""
opening_device = OpeningDevice(pyvlx=self.mocked_pyvlx, node_id=23, name="Test device")
velocity = Velocity.DEFAULT
wait_for_completion = False
await opening_device.close(velocity=velocity, wait_for_completion=wait_for_completion)
set_position.assert_awaited_once_with(
position=Position(position_percent=opening_device.close_position_target),
velocity=velocity,
wait_for_completion=wait_for_completion)

@patch("pyvlx.opening_device.OpeningDevice.set_position", new_callable=AsyncMock)
async def test_stop(self, set_position: AsyncMock) -> None:
"""Test stop function of OpeningDevice object."""
opening_device = OpeningDevice(pyvlx=self.mocked_pyvlx, node_id=23, name="Test device")
wait_for_completion = False
await opening_device.stop(wait_for_completion=wait_for_completion)
set_position.assert_awaited_once_with(
position=CurrentPosition(),
wait_for_completion=wait_for_completion)

def test_is_moving(self) -> None:
"""Test is moving boolean of OpeningDevice object."""
opening_device = OpeningDevice(pyvlx=self.mocked_pyvlx, node_id=23, name="Test Device")
opening_device.is_opening = True
opening_device.is_closing = False
self.assertTrue(opening_device.is_moving())
opening_device.is_opening = False
opening_device.is_closing = True
self.assertTrue(opening_device.is_moving())
opening_device.is_opening = True
opening_device.is_closing = True
self.assertTrue(opening_device.is_moving())
opening_device.is_opening = False
opening_device.is_closing = False
self.assertFalse(opening_device.is_moving())

def test_window_str(self) -> None:
"""Test string representation of Window object."""
pyvlx = MagicMock()
pyvlx = self.mocked_pyvlx
window = Window(
pyvlx=pyvlx,
node_id=23,
Expand All @@ -37,7 +90,7 @@ def test_window_str(self) -> None:

def test_blind_str(self) -> None:
"""Test string representation of Blind object."""
pyvlx = MagicMock()
pyvlx = self.mocked_pyvlx
blind = Blind(
pyvlx=pyvlx,
node_id=23,
Expand All @@ -51,7 +104,7 @@ def test_blind_str(self) -> None:

def test_roller_shutter_str(self) -> None:
"""Test string representation of RolllerShutter object."""
pyvlx = MagicMock()
pyvlx = self.mocked_pyvlx
roller_shutter = RollerShutter(
pyvlx=pyvlx,
node_id=23,
Expand All @@ -66,7 +119,7 @@ def test_roller_shutter_str(self) -> None:

def test_blade_str(self) -> None:
"""Test string representation of Blade object."""
pyvlx = MagicMock()
pyvlx = self.mocked_pyvlx
blade = Blade(
pyvlx=pyvlx,
node_id=23,
Expand All @@ -80,7 +133,7 @@ def test_blade_str(self) -> None:

def test_awning_str(self) -> None:
"""Test string representation of Awning object."""
pyvlx = MagicMock()
pyvlx = self.mocked_pyvlx
awning = Awning(
pyvlx=pyvlx,
node_id=23,
Expand All @@ -94,7 +147,7 @@ def test_awning_str(self) -> None:

def test_eq(self) -> None:
"""Testing eq method with positive results."""
pyvlx = MagicMock()
pyvlx = self.mocked_pyvlx
node1 = Blind(
pyvlx=pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
)
Expand All @@ -105,7 +158,7 @@ def test_eq(self) -> None:

def test_nq(self) -> None:
"""Testing eq method with negative results."""
pyvlx = MagicMock()
pyvlx = self.mocked_pyvlx
node1 = Blind(
pyvlx=pyvlx, node_id=23, name="xxx", serial_number="aa:bb:aa:bb:aa:bb:aa:23"
)
Expand Down
14 changes: 7 additions & 7 deletions test/scenes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
from unittest.mock import MagicMock

from pyvlx import Scene, Scenes
from pyvlx import PyVLX, Scene, Scenes

# pylint: disable=too-many-public-methods,invalid-name

Expand All @@ -12,7 +12,7 @@ class TestScenes(unittest.TestCase):

def test_get_item(self) -> None:
"""Test get_item."""
pyvlx = MagicMock()
pyvlx = MagicMock(spec=PyVLX)
scenes = Scenes(pyvlx)
scene1 = Scene(pyvlx, 0, "Scene_1")
scenes.add(scene1)
Expand All @@ -29,7 +29,7 @@ def test_get_item(self) -> None:

def test_get_item_failed(self) -> None:
"""Test get_item with non existing object."""
pyvlx = MagicMock()
pyvlx = MagicMock(spec=PyVLX)
scenes = Scenes(pyvlx)
scene1 = Scene(pyvlx, 0, "Scene_1")
scenes.add(scene1)
Expand All @@ -40,7 +40,7 @@ def test_get_item_failed(self) -> None:

def test_iter(self) -> None:
"""Test iterator."""
pyvlx = MagicMock()
pyvlx = MagicMock(spec=PyVLX)
scenes = Scenes(pyvlx)
scene1 = Scene(pyvlx, 0, "Scene_1")
scenes.add(scene1)
Expand All @@ -52,7 +52,7 @@ def test_iter(self) -> None:

def test_len(self) -> None:
"""Test len."""
pyvlx = MagicMock()
pyvlx = MagicMock(spec=PyVLX)
scenes = Scenes(pyvlx)
self.assertEqual(len(scenes), 0)
scenes.add(Scene(pyvlx, 0, "Scene_1"))
Expand All @@ -63,7 +63,7 @@ def test_len(self) -> None:

def test_add_same_object(self) -> None:
"""Test adding object with same scene_id."""
pyvlx = MagicMock()
pyvlx = MagicMock(spec=PyVLX)
scenes = Scenes(pyvlx)
self.assertEqual(len(scenes), 0)
scenes.add(Scene(pyvlx, 0, "Scene_1"))
Expand All @@ -84,7 +84,7 @@ def test_add_item_failed(self) -> None:

def test_clear(self) -> None:
"""Test clear() method."""
pyvlx = MagicMock()
pyvlx = MagicMock(spec=PyVLX)
scenes = Scenes(pyvlx)
self.assertEqual(len(scenes), 0)
scenes.add(Scene(pyvlx, 0, "Scene_1"))
Expand Down

0 comments on commit 6771194

Please sign in to comment.