Skip to content

Commit

Permalink
Merge pull request #14 from 2Fake/development
Browse files Browse the repository at this point in the history
Fix #12
  • Loading branch information
Shutgun authored Oct 7, 2020
2 parents bcae1ea + 73f37a6 commit dd55b52
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
"Operating System :: OS Independent",
],
install_requires=[
"httpx>=0.14,<0.15",
"httpx>=0.14,<0.17",
"protobuf",
"zeroconf>=0.27.0",
],
setup_requires=[
"pytest-runner"
],
tests_require=[
"asynctest",
"asynctest;python_version<'3.8'",
"pytest",
"pytest-asyncio",
"pytest-cov",
Expand Down
24 changes: 14 additions & 10 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

import pytest
import zeroconf
from asynctest import CoroutineMock

try:
from unittest.mock import AsyncMock
except ImportError:
from asynctest import CoroutineMock as AsyncMock

from devolo_plc_api.device_api.deviceapi import DeviceApi
from devolo_plc_api.plcnet_api.plcnetapi import PlcNetApi
from devolo_plc_api.exceptions.device import DeviceNotFound
from devolo_plc_api.plcnet_api.plcnetapi import PlcNetApi


class TestDevice:

@pytest.mark.asyncio
async def test__gather_apis(self, mocker, mock_device):
with patch("devolo_plc_api.device.Device._get_device_info", new=CoroutineMock()), \
patch("devolo_plc_api.device.Device._get_plcnet_info", new=CoroutineMock()):
with patch("devolo_plc_api.device.Device._get_device_info", new=AsyncMock()), \
patch("devolo_plc_api.device.Device._get_plcnet_info", new=AsyncMock()):
spy_device_info = mocker.spy(mock_device, "_get_device_info")
spy_plcnet_info = mocker.spy(mock_device, "_get_plcnet_info")
await mock_device._gather_apis()
Expand All @@ -27,7 +31,7 @@ async def test__gather_apis(self, mocker, mock_device):
@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_device_api")
async def test__get_device_info(self, mock_device):
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=CoroutineMock()):
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=AsyncMock()):
device_info = self.device_info['_dvl-deviceapi._tcp.local.']
await mock_device._get_device_info()

Expand All @@ -41,15 +45,15 @@ async def test__get_device_info(self, mock_device):
@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_device_api")
async def test__get_device_info_timeout(self, mock_device):
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=CoroutineMock()), \
patch("asyncio.wait_for", new=CoroutineMock(side_effect=asyncio.TimeoutError())), \
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=AsyncMock()), \
patch("asyncio.wait_for", new=AsyncMock(side_effect=asyncio.TimeoutError())), \
pytest.raises(DeviceNotFound):
await mock_device._get_device_info()

@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_plcnet_api")
async def test__get_plcnet_info(self, mock_device):
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=CoroutineMock()):
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=AsyncMock()):
device_info = self.device_info['_dvl-plcnetapi._tcp.local.']
await mock_device._get_plcnet_info()

Expand All @@ -60,8 +64,8 @@ async def test__get_plcnet_info(self, mock_device):
@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_device_api")
async def test__get_plcnet_info_timeout(self, mock_device):
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=CoroutineMock()), \
patch("asyncio.wait_for", new=CoroutineMock(side_effect=asyncio.TimeoutError())), \
with patch("devolo_plc_api.device.Device._get_zeroconf_info", new=AsyncMock()), \
patch("asyncio.wait_for", new=AsyncMock(side_effect=asyncio.TimeoutError())), \
pytest.raises(DeviceNotFound):
await mock_device._get_plcnet_info()

Expand Down
46 changes: 25 additions & 21 deletions tests/test_deviceapi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from unittest.mock import patch

import pytest
from asynctest import CoroutineMock
from google.protobuf.json_format import MessageToDict
from httpx import AsyncClient, Client, Response

try:
from unittest.mock import AsyncMock
except ImportError:
from asynctest import CoroutineMock as AsyncMock

from devolo_plc_api.device_api.deviceapi import DeviceApi
from devolo_plc_api.device_api.devolo_idl_proto_deviceapi_ledsettings_pb2 import LedSettingsGet, LedSettingsSetResponse
from devolo_plc_api.device_api.devolo_idl_proto_deviceapi_wifinetwork_pb2 import (
Expand All @@ -31,8 +35,8 @@ def test_unsupported_feature(self, request):
async def test_async_get_led_setting(self, request):
led_setting_get = LedSettingsGet()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=led_setting_get.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=led_setting_get.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -68,8 +72,8 @@ def test_get_led_setting(self, request):
async def test_async_set_led_setting(self, request):
led_setting_set = LedSettingsSetResponse()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=led_setting_set.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=led_setting_set.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -99,8 +103,8 @@ def test_set_led_setting(self, request):
async def test_async_check_firmware_available(self, request):
firmware_available = UpdateFirmwareCheck()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=firmware_available.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=firmware_available.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -136,8 +140,8 @@ def test_check_firmware_available(self, request):
async def test_async_start_firmware_update(self, request):
firmware_update = UpdateFirmwareStart()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=firmware_update.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=firmware_update.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -167,8 +171,8 @@ def test_start_firmware_update(self, request):
async def test_async_get_wifi_connected_station(self, request):
wifi_connected_stations_get = WifiConnectedStationsGet()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=wifi_connected_stations_get.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=wifi_connected_stations_get.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -204,8 +208,8 @@ def test_get_wifi_connected_station(self, request):
async def test_async_get_wifi_guest_access(self, request):
wifi_guest_access_get = WifiGuestAccessGet()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=wifi_guest_access_get.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=wifi_guest_access_get.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -241,8 +245,8 @@ def test_get_wifi_guest_access(self, request):
async def test_async_set_wifi_guest_access(self, request):
wifi_guest_access_set = WifiGuestAccessSetResponse()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=wifi_guest_access_set.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=wifi_guest_access_set.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -272,8 +276,8 @@ def test_set_wifi_guest_access(self, request):
async def test_async_get_wifi_neighbor_access_points(self, request):
wifi_neighbor_accesspoints_get = WifiNeighborAPsGet()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=wifi_neighbor_accesspoints_get.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=wifi_neighbor_accesspoints_get.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -309,8 +313,8 @@ def test_get_wifi_neighbor_access_points(self, request):
async def test_async_get_wifi_repeated_access_points(self, request):
wifi_repeated_accesspoints_get = WifiRepeatedAPsGet()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=wifi_repeated_accesspoints_get.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=wifi_repeated_accesspoints_get.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -346,8 +350,8 @@ def test_get_wifi_repeated_access_points(self, request):
async def test_async_start_wps(self, request):
wps = WifiWpsPbcStart()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=wps.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=wps.SerializeToString())):
device_api = DeviceApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down
22 changes: 13 additions & 9 deletions tests/test_plcnetapi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from unittest.mock import patch

import pytest
from asynctest import CoroutineMock
from google.protobuf.json_format import MessageToDict
from httpx import AsyncClient, Client, Response

try:
from unittest.mock import AsyncMock
except ImportError:
from asynctest import CoroutineMock as AsyncMock

from devolo_plc_api.plcnet_api.devolo_idl_proto_plcnetapi_getnetworkoverview_pb2 import GetNetworkOverview
from devolo_plc_api.plcnet_api.devolo_idl_proto_plcnetapi_identifydevice_pb2 import IdentifyDeviceResponse
from devolo_plc_api.plcnet_api.devolo_idl_proto_plcnetapi_setuserdevicename_pb2 import SetUserDeviceNameResponse
Expand All @@ -17,8 +21,8 @@ class TestDeviceApi:
async def test_async_get_network_overview(self, request):
network_overview = GetNetworkOverview()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=network_overview.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_get", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=network_overview.SerializeToString())):
plcnet_api = PlcNetApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
AsyncClient(),
Expand Down Expand Up @@ -52,8 +56,8 @@ def test_get_network_overview(self, request):
async def test_async_identify_device_start(self, request):
identify_device = IdentifyDeviceResponse()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=identify_device.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=identify_device.SerializeToString())):
plcnet_api = PlcNetApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
Client(),
Expand Down Expand Up @@ -81,8 +85,8 @@ def test_identify_device_start(self, request):
async def test_async_identify_device_stop(self, request):
identify_device = IdentifyDeviceResponse()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=identify_device.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=identify_device.SerializeToString())):
plcnet_api = PlcNetApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
Client(),
Expand Down Expand Up @@ -110,8 +114,8 @@ def test_identify_device_stop(self, request):
async def test_async_set_user_device_name(self, request):
user_device_name_set = SetUserDeviceNameResponse()

with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=CoroutineMock(return_value=Response)), \
patch("httpx.Response.aread", new=CoroutineMock(return_value=user_device_name_set.SerializeToString())):
with patch("devolo_plc_api.clients.protobuf.Protobuf._async_post", new=AsyncMock(return_value=Response)), \
patch("httpx.Response.aread", new=AsyncMock(return_value=user_device_name_set.SerializeToString())):
plcnet_api = PlcNetApi(request.cls.ip,
request.cls.device_info['_dvl-deviceapi._tcp.local.']['Port'],
Client(),
Expand Down

0 comments on commit dd55b52

Please sign in to comment.