Skip to content

Remove async magic patch from tests #1146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions kasa/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,3 @@ async def _create_datagram_endpoint(protocol_factory, *_, **__):
side_effect=_create_datagram_endpoint,
):
yield


# allow mocks to be awaited
# https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression/51399767#51399767


async def async_magic():
pass


MagicMock.__await__ = lambda x: async_magic().__await__()
6 changes: 3 additions & 3 deletions kasa/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pkgutil
import sys
from contextlib import AbstractContextManager
from unittest.mock import Mock, patch
from unittest.mock import AsyncMock, patch

import pytest

Expand Down Expand Up @@ -85,7 +85,7 @@ async def test_create_device_with_timeout():

async def test_create_thin_wrapper():
"""Make sure thin wrapper is created with the correct device type."""
mock = Mock()
mock = AsyncMock()
config = DeviceConfig(
host="test_host",
port_override=1234,
Expand Down Expand Up @@ -281,7 +281,7 @@ async def test_device_type_aliases():
"""Test that the device type aliases in Device work."""

def _mock_connect(config, *args, **kwargs):
mock = Mock()
mock = AsyncMock()
mock.config = config
return mock

Expand Down
14 changes: 10 additions & 4 deletions kasa/tests/test_feature.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import sys
from unittest.mock import patch
from unittest.mock import AsyncMock, patch

import pytest
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -94,7 +94,9 @@ def test_feature_value_callable(dev, dummy_feature: Feature):

async def test_feature_setter(dev, mocker, dummy_feature: Feature):
"""Verify that *set_value* calls the defined method."""
mock_set_dummy = mocker.patch.object(dummy_feature.device, "set_dummy", create=True)
mock_set_dummy = mocker.patch.object(
dummy_feature.device, "set_dummy", create=True, new_callable=AsyncMock
)
dummy_feature.attribute_setter = "set_dummy"
await dummy_feature.set_value("dummy value")
mock_set_dummy.assert_called_with("dummy value")
Expand All @@ -118,7 +120,9 @@ async def test_feature_action(mocker):
icon="mdi:dummy",
type=Feature.Type.Action,
)
mock_call_action = mocker.patch.object(feat.device, "call_action", create=True)
mock_call_action = mocker.patch.object(
feat.device, "call_action", create=True, new_callable=AsyncMock
)
assert feat.value == "<Action>"
await feat.set_value(1234)
mock_call_action.assert_called()
Expand All @@ -129,7 +133,9 @@ async def test_feature_choice_list(dummy_feature, caplog, mocker: MockerFixture)
dummy_feature.type = Feature.Type.Choice
dummy_feature.choices_getter = lambda: ["first", "second"]

mock_setter = mocker.patch.object(dummy_feature.device, "dummysetter", create=True)
mock_setter = mocker.patch.object(
dummy_feature.device, "dummysetter", create=True, new_callable=AsyncMock
)
await dummy_feature.set_value("first")
mock_setter.assert_called_with("first")
mock_setter.reset_mock()
Expand Down
6 changes: 6 additions & 0 deletions kasa/tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import struct
import sys
from typing import cast
from unittest.mock import AsyncMock

import pytest

Expand Down Expand Up @@ -175,6 +176,7 @@ def aio_mock_writer(_, __):
writer = mocker.patch("asyncio.StreamWriter")
mocker.patch.object(writer, "write", _fail_one_less_than_retry_count)
mocker.patch.object(reader, "readexactly", _mock_read)
mocker.patch.object(writer, "drain", new_callable=AsyncMock)
return reader, writer

config = DeviceConfig("127.0.0.1")
Expand Down Expand Up @@ -224,6 +226,7 @@ def aio_mock_writer(_, __):
writer = mocker.patch("asyncio.StreamWriter")
mocker.patch.object(writer, "write", _cancel_first_attempt)
mocker.patch.object(reader, "readexactly", _mock_read)
mocker.patch.object(writer, "drain", new_callable=AsyncMock)
return reader, writer

config = DeviceConfig("127.0.0.1")
Expand Down Expand Up @@ -275,6 +278,7 @@ def aio_mock_writer(_, __):
reader = mocker.patch("asyncio.StreamReader")
writer = mocker.patch("asyncio.StreamWriter")
mocker.patch.object(reader, "readexactly", _mock_read)
mocker.patch.object(writer, "drain", new_callable=AsyncMock)
return reader, writer

config = DeviceConfig("127.0.0.1")
Expand Down Expand Up @@ -324,6 +328,7 @@ def aio_mock_writer(_, __):
reader = mocker.patch("asyncio.StreamReader")
writer = mocker.patch("asyncio.StreamWriter")
mocker.patch.object(reader, "readexactly", _mock_read)
mocker.patch.object(writer, "drain", new_callable=AsyncMock)
return reader, writer

config = DeviceConfig("127.0.0.1")
Expand Down Expand Up @@ -373,6 +378,7 @@ def aio_mock_writer(_, port):
else:
assert port == custom_port
mocker.patch.object(reader, "readexactly", _mock_read)
mocker.patch.object(writer, "drain", new_callable=AsyncMock)
return reader, writer

config = DeviceConfig("127.0.0.1", port_override=custom_port)
Expand Down
Loading