Skip to content

Commit 6ad0366

Browse files
committed
Replace swap_attribute with patch.object
1 parent f69c407 commit 6ad0366

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

tests/application/test_connect.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import asyncio
2+
from unittest.mock import patch
23

34
import pytest
45

56
import zigpy_znp.config as conf
67
from zigpy_znp.uart import connect as uart_connect
78
from zigpy_znp.zigbee.application import ControllerApplication
89

9-
from ..conftest import FORMED_DEVICES, FormedLaunchpadCC26X2R1, swap_attribute
10+
from ..conftest import FORMED_DEVICES, FormedLaunchpadCC26X2R1
1011

1112

1213
async def test_no_double_connect(make_znp_server, mocker):
@@ -118,7 +119,7 @@ async def test_reconnect(device, event_loop, make_application):
118119
assert app._znp is not None
119120

120121
# Don't reply to anything for a bit
121-
with swap_attribute(znp_server, "frame_received", lambda _: None):
122+
with patch.object(znp_server, "frame_received", lambda _: None):
122123
# Now that we're connected, have the server close the connection
123124
znp_server._uart._transport.close()
124125

@@ -197,7 +198,7 @@ async def test_reconnect_lockup(device, event_loop, make_application, mocker):
197198
await app.startup(auto_form=False)
198199

199200
# Stop responding
200-
with swap_attribute(znp_server, "frame_received", lambda _: None):
201+
with patch.object(znp_server, "frame_received", lambda _: None):
201202
assert app._znp is not None
202203
assert app._reconnect_task.done()
203204

@@ -250,7 +251,7 @@ async def patched_load_network_info(old_load=app.load_network_info, **kwargs):
250251
finally:
251252
did_load_info.set_result(True)
252253

253-
with swap_attribute(app, "load_network_info", patched_load_network_info):
254+
with patch.object(app, "load_network_info", patched_load_network_info):
254255
# "Drop" the connection like PySerial
255256
app._znp._uart.connection_lost(exc=None)
256257

tests/conftest.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import inspect
44
import logging
55
import pathlib
6-
import contextlib
7-
from unittest.mock import Mock, PropertyMock
6+
from unittest.mock import Mock, PropertyMock, patch
87

98
import pytest
109
import zigpy.types
@@ -217,17 +216,6 @@ def merge_dicts(a, b):
217216
return c
218217

219218

220-
@contextlib.contextmanager
221-
def swap_attribute(obj, name, value):
222-
old_value = getattr(obj, name)
223-
setattr(obj, name, value)
224-
225-
try:
226-
yield old_value
227-
finally:
228-
setattr(obj, name, old_value)
229-
230-
231219
@pytest.fixture
232220
def make_application(make_znp_server):
233221
async def inner(
@@ -363,7 +351,7 @@ def send(self, response):
363351

364352
def close(self):
365353
# We don't clear listeners on shutdown
366-
with swap_attribute(self, "_listeners", {}):
354+
with patch.object(self, "_listeners", {}):
367355
return super().close()
368356

369357

0 commit comments

Comments
 (0)