Skip to content

Commit

Permalink
Merge pull request #19 from 2Fake/optimize_tests
Browse files Browse the repository at this point in the history
Optimize tests
  • Loading branch information
2Fake authored Nov 30, 2020
2 parents 0eaf236 + b80bfd6 commit 687f038
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 15 deletions.
Empty file added tests/fixtures/__init__.py
Empty file.
7 changes: 5 additions & 2 deletions tests/fixtures/device.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from copy import deepcopy

import pytest
from zeroconf import Zeroconf

from devolo_plc_api.device import Device
from ..mocks.mock_zeroconf import Zeroconf

from ..mocks.mock_zeroconf import MockZeroconf


@pytest.fixture()
Expand All @@ -23,4 +25,5 @@ def mock_service_browser(mocker):

@pytest.fixture()
def mock_zeroconf(mocker):
mocker.patch("zeroconf.Zeroconf.get_service_info", Zeroconf.get_service_info)
mocker.patch("zeroconf.Zeroconf.get_service_info", MockZeroconf.get_service_info)
return Zeroconf()
3 changes: 1 addition & 2 deletions tests/fixtures/protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

@pytest.fixture()
def mock_protobuf():
protobuf = StubProtobuf()
yield protobuf
return StubProtobuf()


@pytest.fixture()
Expand Down
Empty file added tests/mocks/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion tests/mocks/mock_zeroconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
test_data = json.load(fh)


class Zeroconf:
class MockZeroconf:

def get_service_info(self, service_type, name):
service_info = ServiceInfo(service_type, name)
Expand Down
Empty file added tests/stubs/__init__.py
Empty file.
16 changes: 6 additions & 10 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest.mock import patch

import pytest
import zeroconf
from zeroconf import ServiceBrowser, ServiceStateChange

try:
from unittest.mock import AsyncMock
Expand Down Expand Up @@ -59,7 +59,7 @@ async def test__get_plcnet_info_timeout(self, mock_device):
@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_service_browser")
async def test__get_zeroconf_info(self, mocker, mock_device):
spy_cancel = mocker.spy(zeroconf.ServiceBrowser, "cancel")
spy_cancel = mocker.spy(ServiceBrowser, "cancel")
spy_sleep = mocker.spy(asyncio, "sleep")
await mock_device._get_zeroconf_info("_dvl-plcnetapi._tcp.local.")
assert spy_cancel.call_count == 1
Expand All @@ -85,17 +85,13 @@ async def test__setup_device_not_found(self, mock_device):
await mock_device._setup_device()

@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_zeroconf")
def test__state_change_added(self, mock_device):
zc = zeroconf.Zeroconf()
def test__state_change_added(self, mock_device, mock_zeroconf):
service_type = "_dvl-plcnetapi._tcp.local."
mock_device._state_change(zc, service_type, service_type, zeroconf.ServiceStateChange.Added)
mock_device._state_change(mock_zeroconf, service_type, service_type, ServiceStateChange.Added)
assert mock_device._info[service_type]['new'] == "value"

@pytest.mark.asyncio
@pytest.mark.usefixtures("mock_zeroconf")
def test__state_change_removed(self, mock_device):
zc = zeroconf.Zeroconf()
def test__state_change_removed(self, mock_device, mock_zeroconf):
service_type = "_dvl-plcnetapi._tcp.local."
mock_device._state_change(zc, service_type, service_type, zeroconf.ServiceStateChange.Removed)
mock_device._state_change(mock_zeroconf, service_type, service_type, ServiceStateChange.Removed)
assert "new" not in mock_device._info[service_type]

0 comments on commit 687f038

Please sign in to comment.