Skip to content

Commit

Permalink
Add tests for MeijuCloud (#17)
Browse files Browse the repository at this point in the history
* test(cloud): meijucloud login coverage

* test(cloud): meijucloud list_home
  • Loading branch information
rokam authored May 26, 2024
1 parent 77bf2ba commit 242b032
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pytest
pytest-asyncio
pytest-cov
ruff
aiohttp
Expand Down
89 changes: 89 additions & 0 deletions tests/cloud_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""Test cloud"""

import os
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, Mock

from midealocal.cloud import MSmartHomeCloud, MeijuCloud, MideaAirCloud, get_midea_cloud


class CloudTest(IsolatedAsyncioTestCase):
"""Cloud test case."""

responses: dict[str, bytes] = {}

def setUp(self) -> None:
"""Set tests up."""
for file in os.listdir(os.path.join(os.path.dirname(__file__), "responses")):
filename = os.path.basename(file)
with open(
file=os.path.join(os.path.dirname(__file__), "responses", file),
encoding="utf-8",
) as f:
self.responses[filename] = bytes(f.read(), encoding="utf-8")

def test_get_midea_cloud(self) -> None:
"""Test get midea cloud"""
assert isinstance(get_midea_cloud("美的美居", None, "", ""), MeijuCloud)
assert isinstance(get_midea_cloud("MSmartHome", None, "", ""), MSmartHomeCloud)
assert isinstance(get_midea_cloud("Midea Air", None, "", ""), MideaAirCloud)
assert isinstance(get_midea_cloud("NetHome Plus", None, "", ""), MideaAirCloud)
assert isinstance(get_midea_cloud("Ariston Clima", None, "", ""), MideaAirCloud)

async def test_meijucloud_login_success(self) -> None:
"""Test MeijuCloud login"""
session = Mock()
response = Mock()
response.read = AsyncMock(
side_effect=[
self.responses["meijucloud_login_id.json"],
self.responses["meijucloud_login.json"],
]
)
session.request = AsyncMock(return_value=response)
cloud = get_midea_cloud(
"美的美居", session=session, account="account", password="password"
)
assert await cloud.login()

async def test_meijucloud_login_invalid_user(self) -> None:
"""Test MeijuCloud login invalid user"""
session = Mock()
response = Mock()
response.read = AsyncMock(return_value=self.responses["invalid_response.json"])
session.request = AsyncMock(return_value=response)
cloud = get_midea_cloud(
"美的美居", session=session, account="account", password="password"
)
assert not await cloud.login()

async def test_meijucloud_get_keys(self) -> None:
"""Test MeijuCloud get_keys"""
session = Mock()
response = Mock()
response.read = AsyncMock(
return_value=self.responses["meijucloud_get_keys.json"]
)
session.request = AsyncMock(return_value=response)
cloud = get_midea_cloud(
"美的美居", session=session, account="account", password="password"
)
keys: dict = await cloud.get_keys(100)
assert keys.get(1).get("token") == "returnedappliancetoken"
assert keys.get(1).get("key") == "returnedappliancekey"

async def test_meijucloud_list_home(self) -> None:
"""Test MeijuCloud list_home"""
session = Mock()
response = Mock()
response.read = AsyncMock(
return_value=self.responses["meijucloud_list_home.json"]
)
session.request = AsyncMock(return_value=response)
cloud = get_midea_cloud(
"美的美居", session=session, account="account", password="password"
)
homes = await cloud.list_home()
assert len(homes.keys()) == 2
assert homes.get(1) == "Home 1"
assert homes.get(2) == "Home 2"
3 changes: 3 additions & 0 deletions tests/responses/invalid_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"code":10
}
10 changes: 10 additions & 0 deletions tests/responses/meijucloud_get_keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"code":0,
"data":{
"tokenlist":[{
"udpId": "dec4da86e0aeefadde14a4e553680b9b",
"token": "returnedappliancetoken",
"key": "returnedappliancekey"
}]
}
}
12 changes: 12 additions & 0 deletions tests/responses/meijucloud_list_home.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"code":0,
"data":{
"homeList":[{
"homegroupId": "1",
"name": "Home 1"
},{
"homegroupId": "2",
"name": "Home 2"
}]
}
}
9 changes: 9 additions & 0 deletions tests/responses/meijucloud_login.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"code":0,
"data":{
"mdata":{
"accessToken": "returnedAccessToken"
},
"key": "9d52c159dcdd32bac5109cf54080fca7"
}
}
6 changes: 6 additions & 0 deletions tests/responses/meijucloud_login_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"code":0,
"data":{
"loginId": "returnedLoginId"
}
}

0 comments on commit 242b032

Please sign in to comment.