11import asyncio
22import logging
33
4- from asynctest import CoroutineMock , mock
54import pytest
65import serial
76import zigpy .exceptions
109import zigpy_xbee .config
1110from zigpy_xbee .zigbee .application import ControllerApplication
1211
12+ import tests .async_mock as mock
13+
1314DEVICE_CONFIG = zigpy_xbee .config .SCHEMA_DEVICE (
1415 {zigpy_xbee .config .CONF_DEVICE_PATH : "/dev/null" }
1516)
@@ -22,10 +23,9 @@ def api():
2223 return api
2324
2425
25- @pytest .mark .asyncio
2626async def test_connect (monkeypatch ):
2727 api = xbee_api .XBee (DEVICE_CONFIG )
28- monkeypatch .setattr (uart , "connect" , CoroutineMock ())
28+ monkeypatch .setattr (uart , "connect" , mock . AsyncMock ())
2929 await api .connect ()
3030
3131
@@ -52,7 +52,6 @@ def test_commands():
5252 assert reply is None or isinstance (reply , int )
5353
5454
55- @pytest .mark .asyncio
5655async def test_command (api ):
5756 def mock_api_frame (name , * args ):
5857 c = xbee_api .COMMAND_REQUESTS [name ]
@@ -90,7 +89,6 @@ def mock_api_frame(name, *args):
9089 api ._uart .send .reset_mock ()
9190
9291
93- @pytest .mark .asyncio
9492async def test_command_not_connected (api ):
9593 api ._uart = None
9694
@@ -135,20 +133,17 @@ def mock_command(name, *args):
135133 api ._command .reset_mock ()
136134
137135
138- @pytest .mark .asyncio
139136async def test_at_command (api , monkeypatch ):
140137 await _test_at_or_queued_at_command (api , api ._at_command , monkeypatch )
141138
142139
143- @pytest .mark .asyncio
144140async def test_at_command_no_response (api , monkeypatch ):
145141 with pytest .raises (asyncio .TimeoutError ):
146142 await _test_at_or_queued_at_command (
147143 api , api ._at_command , monkeypatch , do_reply = False
148144 )
149145
150146
151- @pytest .mark .asyncio
152147async def test_queued_at_command (api , monkeypatch ):
153148 await _test_at_or_queued_at_command (api , api ._queued_at , monkeypatch )
154149
@@ -191,12 +186,10 @@ def mock_command(name, *args):
191186 api ._command .reset_mock ()
192187
193188
194- @pytest .mark .asyncio
195189async def test_remote_at_cmd (api , monkeypatch ):
196190 await _test_remote_at_command (api , monkeypatch )
197191
198192
199- @pytest .mark .asyncio
200193async def test_remote_at_cmd_no_rsp (api , monkeypatch ):
201194 monkeypatch .setattr (xbee_api , "REMOTE_AT_COMMAND_TIMEOUT" , 0.1 )
202195 with pytest .raises (asyncio .TimeoutError ):
@@ -417,7 +410,6 @@ def test_handle_tx_status_duplicate(api):
417410 assert send_fut .set_exception .call_count == 0
418411
419412
420- @pytest .mark .asyncio
421413async def test_command_mode_at_cmd (api ):
422414 command = "+++"
423415
@@ -430,7 +422,6 @@ def cmd_mode_send(cmd):
430422 assert result
431423
432424
433- @pytest .mark .asyncio
434425async def test_command_mode_at_cmd_timeout (api ):
435426 command = "+++"
436427
@@ -462,21 +453,15 @@ def test_handle_command_mode_rsp(api):
462453 assert api ._cmd_mode_future .result () == data
463454
464455
465- @pytest .mark .asyncio
466456async def test_enter_at_command_mode (api ):
467- api .command_mode_at_cmd = mock .MagicMock (
468- side_effect = asyncio .coroutine (lambda x : mock .sentinel .at_response )
469- )
457+ api .command_mode_at_cmd = mock .AsyncMock (return_value = mock .sentinel .at_response )
470458
471459 res = await api .enter_at_command_mode ()
472460 assert res == mock .sentinel .at_response
473461
474462
475- @pytest .mark .asyncio
476463async def test_api_mode_at_commands (api ):
477- api .command_mode_at_cmd = mock .MagicMock (
478- side_effect = asyncio .coroutine (lambda x : mock .sentinel .api_mode )
479- )
464+ api .command_mode_at_cmd = mock .AsyncMock (return_value = mock .sentinel .api_mode )
480465
481466 res = await api .api_mode_at_commands (57600 )
482467 assert res is True
@@ -491,20 +476,15 @@ async def mock_at_cmd(cmd):
491476 assert res is None
492477
493478
494- @pytest .mark .asyncio
495479async def test_init_api_mode (api , monkeypatch ):
496480 monkeypatch .setattr (api ._uart , "baudrate" , 57600 )
497- api .enter_at_command_mode = mock .MagicMock (
498- side_effect = asyncio .coroutine (mock .MagicMock (return_value = True ))
499- )
481+ api .enter_at_command_mode = mock .AsyncMock (return_value = True )
500482
501483 res = await api .init_api_mode ()
502484 assert res is None
503485 assert api .enter_at_command_mode .call_count == 1
504486
505- api .enter_at_command_mode = mock .MagicMock (
506- side_effect = asyncio .coroutine (mock .MagicMock (return_value = False ))
507- )
487+ api .enter_at_command_mode = mock .AsyncMock (return_value = False )
508488
509489 res = await api .init_api_mode ()
510490 assert res is False
@@ -517,9 +497,7 @@ async def enter_at_mode():
517497
518498 api ._uart .baudrate = 57600
519499 api .enter_at_command_mode = mock .MagicMock (side_effect = enter_at_mode )
520- api .api_mode_at_commands = mock .MagicMock (
521- side_effect = asyncio .coroutine (mock .MagicMock (return_value = True ))
522- )
500+ api .api_mode_at_commands = mock .AsyncMock (return_value = True )
523501
524502 res = await api .init_api_mode ()
525503 assert res is True
@@ -542,21 +520,16 @@ def test_handle_many_to_one_rri(api):
542520 api ._handle_many_to_one_rri (ieee , nwk , 0 )
543521
544522
545- @pytest .mark .asyncio
546523async def test_reconnect_multiple_disconnects (monkeypatch , caplog ):
547524 api = xbee_api .XBee (DEVICE_CONFIG )
548- connect_mock = CoroutineMock ()
549- connect_mock .return_value = asyncio .Future ()
550- connect_mock .return_value .set_result (True )
525+ connect_mock = mock .AsyncMock (return_value = True )
551526 monkeypatch .setattr (uart , "connect" , connect_mock )
552527
553528 await api .connect ()
554529
555530 caplog .set_level (logging .DEBUG )
556- connected = asyncio .Future ()
557- connected .set_result (mock .sentinel .uart_reconnect )
558531 connect_mock .reset_mock ()
559- connect_mock .side_effect = [asyncio . Future (), connected ]
532+ connect_mock .side_effect = [OSError , mock . sentinel . uart_reconnect ]
560533 api .connection_lost ("connection lost" )
561534 await asyncio .sleep (0.3 )
562535 api .connection_lost ("connection lost 2" )
@@ -567,21 +540,20 @@ async def test_reconnect_multiple_disconnects(monkeypatch, caplog):
567540 assert connect_mock .call_count == 2
568541
569542
570- @pytest .mark .asyncio
571543async def test_reconnect_multiple_attempts (monkeypatch , caplog ):
572544 api = xbee_api .XBee (DEVICE_CONFIG )
573- connect_mock = CoroutineMock ()
574- connect_mock .return_value = asyncio .Future ()
575- connect_mock .return_value .set_result (True )
545+ connect_mock = mock .AsyncMock (return_value = True )
576546 monkeypatch .setattr (uart , "connect" , connect_mock )
577547
578548 await api .connect ()
579549
580550 caplog .set_level (logging .DEBUG )
581- connected = asyncio .Future ()
582- connected .set_result (mock .sentinel .uart_reconnect )
583551 connect_mock .reset_mock ()
584- connect_mock .side_effect = [asyncio .TimeoutError , OSError , connected ]
552+ connect_mock .side_effect = [
553+ asyncio .TimeoutError ,
554+ OSError ,
555+ mock .sentinel .uart_reconnect ,
556+ ]
585557
586558 with mock .patch ("asyncio.sleep" ):
587559 api .connection_lost ("connection lost" )
@@ -591,8 +563,7 @@ async def test_reconnect_multiple_attempts(monkeypatch, caplog):
591563 assert connect_mock .call_count == 3
592564
593565
594- @pytest .mark .asyncio
595- @mock .patch .object (xbee_api .XBee , "_at_command" , new_callable = CoroutineMock )
566+ @mock .patch .object (xbee_api .XBee , "_at_command" , new_callable = mock .AsyncMock )
596567@mock .patch .object (uart , "connect" )
597568async def test_probe_success (mock_connect , mock_at_cmd ):
598569 """Test device probing."""
@@ -606,7 +577,6 @@ async def test_probe_success(mock_connect, mock_at_cmd):
606577 assert mock_connect .return_value .close .call_count == 1
607578
608579
609- @pytest .mark .asyncio
610580@mock .patch .object (xbee_api .XBee , "init_api_mode" , return_value = True )
611581@mock .patch .object (xbee_api .XBee , "_at_command" , side_effect = asyncio .TimeoutError )
612582@mock .patch .object (uart , "connect" )
@@ -623,7 +593,6 @@ async def test_probe_success_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
623593 assert mock_connect .return_value .close .call_count == 1
624594
625595
626- @pytest .mark .asyncio
627596@mock .patch .object (xbee_api .XBee , "init_api_mode" )
628597@mock .patch .object (xbee_api .XBee , "_at_command" , side_effect = asyncio .TimeoutError )
629598@mock .patch .object (uart , "connect" )
@@ -648,7 +617,6 @@ async def test_probe_fail(mock_connect, mock_at_cmd, mock_api_mode, exception):
648617 assert mock_connect .return_value .close .call_count == 1
649618
650619
651- @pytest .mark .asyncio
652620@mock .patch .object (xbee_api .XBee , "init_api_mode" , return_value = False )
653621@mock .patch .object (xbee_api .XBee , "_at_command" , side_effect = asyncio .TimeoutError )
654622@mock .patch .object (uart , "connect" )
@@ -668,7 +636,6 @@ async def test_probe_fail_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
668636 assert mock_connect .return_value .close .call_count == 1
669637
670638
671- @pytest .mark .asyncio
672639@mock .patch .object (xbee_api .XBee , "connect" )
673640async def test_xbee_new (conn_mck ):
674641 """Test new class method."""
0 commit comments