Skip to content

Commit

Permalink
Fix remaining tests for rooms, scenes, shades
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-alfaro committed Feb 1, 2024
1 parent 8680ec3 commit 7e1e7e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion aiopvapi/resources/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def activate(self) -> list[int]:
_val = await self.request.get(
self.base_path, params={ATTR_SCENE_ID: self._id}
)
# v2 returns format {'sceneIds': ids} so flattening the list to align v3
# v2 returns format {'shadeIds': ids} so flattening the list to align v3
_val = _val.get(ATTR_SHADE_IDS)
# should return an array of ID's that belong to the scene
return _val
24 changes: 20 additions & 4 deletions tests/test_room.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import asyncio
from aiohttp import ClientResponse
from unittest.mock import Mock

from aiopvapi.helpers.aiorequest import PvApiResponseStatusError
from aiopvapi.helpers.aiorequest import AioRequest, PvApiResponseStatusError
from aiopvapi.resources.room import Room
from tests.fake_server import FAKE_BASE_URL
from tests.test_apiresource import TestApiResource
from tests.test_scene_members import AsyncMock


ROOM_RAW_DATA = {
"order": 2,
Expand All @@ -23,7 +27,7 @@ def get_resource_uri(self):
return "http://{}/api/rooms/26756".format(FAKE_BASE_URL)

def get_resource(self):
_request = Mock()
_request = Mock(spec=AioRequest)
_request.hub_ip = FAKE_BASE_URL
_request.api_version = 2
_request.api_path = "api"
Expand All @@ -35,7 +39,8 @@ def test_full_path(self):
)

def test_name_property(self):
# No name_unicode, so base64 encoded is returned
# No name_unicode, although name is base64 encoded
# thus base64 decoded is returned
self.assertEqual("Dining Room", self.resource.name)

def test_delete_room_success(self):
Expand All @@ -55,8 +60,19 @@ def test_delete_room_fail(self):

async def go():
await self.start_fake_server()
room = self.get_resource()
# room = self.get_resource()

loop = asyncio.get_event_loop()
request = AioRequest(FAKE_BASE_URL, loop, api_version=2)

response = Mock(spec=ClientResponse)
response.status = 500
response.release = AsyncMock(return_value=None)
request.websession.delete = AsyncMock(return_value=response)

room = Room(ROOM_RAW_DATA, request)
room._resource_path += "1"

resp = await room.delete()
return resp

Expand Down
30 changes: 23 additions & 7 deletions tests/test_scene.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import asyncio
from aiohttp import ClientResponse
from unittest.mock import Mock

from aiopvapi.helpers.aiorequest import PvApiResponseStatusError
from aiopvapi.helpers.aiorequest import AioRequest
from aiopvapi.helpers.aiorequest import AioRequest, PvApiResponseStatusError
from aiopvapi.resources.scene import Scene

from tests.fake_server import FAKE_BASE_URL
from tests.test_apiresource import TestApiResource
from tests.test_scene_members import AsyncMock


SCENE_RAW_DATA = {
"roomId": 26756,
"name": "RGluaW5nIFZhbmVzIE9wZW4=",
"name": "RGluaW5nIFZhbmVzIE9wZW4=", # "Dining Vanes Open"
"colorId": 0,
"iconId": 0,
"id": 37217,
Expand All @@ -32,8 +35,8 @@ def get_resource(self):
return Scene(SCENE_RAW_DATA, _request)

def test_name_property(self):
# No name_unicode, so base64 encoded is returned
# "RGluaW5nIFZhbmVzIE9wZW4="
# No name_unicode, although name is base64 encoded
# thus base64 decoded is returned
self.assertEqual("Dining Vanes Open", self.resource.name)

def test_room_id_property(self):
Expand All @@ -51,18 +54,31 @@ def test_activate_200(self):
async def go():
await self.start_fake_server()
scene = self.get_resource()
scene.request.get = AsyncMock(return_value={'shadeIds': [10]})
resp = await scene.activate()
return resp

resp = self.loop.run_until_complete(go())
self.assertEqual(resp["id"], 10)
self.assertEqual(resp[0], 10)

def test_activate_404(self):
"""Test scene activation"""

async def go():
await self.start_fake_server()
scene = self.get_resource()
# scene = self.get_resource()

loop = asyncio.get_event_loop()
request = AioRequest(FAKE_BASE_URL, loop, api_version=2)

response = Mock(spec=ClientResponse)
response.status = 404
response.release = AsyncMock(return_value=None)
request.websession.get = AsyncMock(return_value=response)

scene = Scene(SCENE_RAW_DATA, request)
scene._resource_path += "1"

resp = await scene.activate()
return resp

Expand Down
15 changes: 8 additions & 7 deletions tests/test_shade.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import Mock

from aiopvapi.helpers.aiorequest import AioRequest
from aiopvapi.resources.shade import BaseShade, ShadeType
from aiopvapi.resources.shade import BaseShade, ShadeType, ShadePosition
from aiopvapi.helpers.constants import (
ATTR_POSKIND1,
ATTR_POSITION1,
Expand Down Expand Up @@ -69,21 +69,22 @@ async def go():
def test_convert_g2(self):
shade = self.get_resource()
self.assertEqual(
shade.convert_to_v2({ATTR_PRIMARY: MAX_POSITION}),
{ATTR_POSITION1: MAX_POSITION_V2, ATTR_POSKIND1: 1},
shade.percent_to_api(MAX_POSITION, ATTR_PRIMARY),
MAX_POSITION_V2
)
self.assertEqual(
shade.convert_to_v2({ATTR_TILT: MID_POSITION}),
{ATTR_POSITION1: MID_POSITION_V2, ATTR_POSKIND1: 3},
shade.percent_to_api(MID_POSITION, ATTR_TILT),
MID_POSITION_V2
)
positions = shade.structured_to_raw(ShadePosition(MAX_POSITION, None, MID_POSITION))['shade']['positions']
self.assertEqual(
shade.convert_to_v2({ATTR_PRIMARY: MAX_POSITION, ATTR_TILT: MID_POSITION}),
positions,
{
ATTR_POSKIND1: 1,
ATTR_POSITION1: MAX_POSITION_V2,
ATTR_POSKIND2: 3,
ATTR_POSITION2: MID_POSITION_V2,
},
}
)


Expand Down

0 comments on commit 7e1e7e6

Please sign in to comment.