1
1
import asyncio
2
2
import logging
3
3
4
- from asynctest import CoroutineMock , mock
5
4
import pytest
6
5
import serial
7
6
import zigpy .exceptions
10
9
import zigpy_xbee .config
11
10
from zigpy_xbee .zigbee .application import ControllerApplication
12
11
12
+ import tests .async_mock as mock
13
+
13
14
DEVICE_CONFIG = zigpy_xbee .config .SCHEMA_DEVICE (
14
15
{zigpy_xbee .config .CONF_DEVICE_PATH : "/dev/null" }
15
16
)
@@ -22,10 +23,9 @@ def api():
22
23
return api
23
24
24
25
25
- @pytest .mark .asyncio
26
26
async def test_connect (monkeypatch ):
27
27
api = xbee_api .XBee (DEVICE_CONFIG )
28
- monkeypatch .setattr (uart , "connect" , CoroutineMock ())
28
+ monkeypatch .setattr (uart , "connect" , mock . AsyncMock ())
29
29
await api .connect ()
30
30
31
31
@@ -52,7 +52,6 @@ def test_commands():
52
52
assert reply is None or isinstance (reply , int )
53
53
54
54
55
- @pytest .mark .asyncio
56
55
async def test_command (api ):
57
56
def mock_api_frame (name , * args ):
58
57
c = xbee_api .COMMAND_REQUESTS [name ]
@@ -90,7 +89,6 @@ def mock_api_frame(name, *args):
90
89
api ._uart .send .reset_mock ()
91
90
92
91
93
- @pytest .mark .asyncio
94
92
async def test_command_not_connected (api ):
95
93
api ._uart = None
96
94
@@ -135,20 +133,17 @@ def mock_command(name, *args):
135
133
api ._command .reset_mock ()
136
134
137
135
138
- @pytest .mark .asyncio
139
136
async def test_at_command (api , monkeypatch ):
140
137
await _test_at_or_queued_at_command (api , api ._at_command , monkeypatch )
141
138
142
139
143
- @pytest .mark .asyncio
144
140
async def test_at_command_no_response (api , monkeypatch ):
145
141
with pytest .raises (asyncio .TimeoutError ):
146
142
await _test_at_or_queued_at_command (
147
143
api , api ._at_command , monkeypatch , do_reply = False
148
144
)
149
145
150
146
151
- @pytest .mark .asyncio
152
147
async def test_queued_at_command (api , monkeypatch ):
153
148
await _test_at_or_queued_at_command (api , api ._queued_at , monkeypatch )
154
149
@@ -191,12 +186,10 @@ def mock_command(name, *args):
191
186
api ._command .reset_mock ()
192
187
193
188
194
- @pytest .mark .asyncio
195
189
async def test_remote_at_cmd (api , monkeypatch ):
196
190
await _test_remote_at_command (api , monkeypatch )
197
191
198
192
199
- @pytest .mark .asyncio
200
193
async def test_remote_at_cmd_no_rsp (api , monkeypatch ):
201
194
monkeypatch .setattr (xbee_api , "REMOTE_AT_COMMAND_TIMEOUT" , 0.1 )
202
195
with pytest .raises (asyncio .TimeoutError ):
@@ -417,7 +410,6 @@ def test_handle_tx_status_duplicate(api):
417
410
assert send_fut .set_exception .call_count == 0
418
411
419
412
420
- @pytest .mark .asyncio
421
413
async def test_command_mode_at_cmd (api ):
422
414
command = "+++"
423
415
@@ -430,7 +422,6 @@ def cmd_mode_send(cmd):
430
422
assert result
431
423
432
424
433
- @pytest .mark .asyncio
434
425
async def test_command_mode_at_cmd_timeout (api ):
435
426
command = "+++"
436
427
@@ -462,21 +453,15 @@ def test_handle_command_mode_rsp(api):
462
453
assert api ._cmd_mode_future .result () == data
463
454
464
455
465
- @pytest .mark .asyncio
466
456
async 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 )
470
458
471
459
res = await api .enter_at_command_mode ()
472
460
assert res == mock .sentinel .at_response
473
461
474
462
475
- @pytest .mark .asyncio
476
463
async 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 )
480
465
481
466
res = await api .api_mode_at_commands (57600 )
482
467
assert res is True
@@ -491,20 +476,15 @@ async def mock_at_cmd(cmd):
491
476
assert res is None
492
477
493
478
494
- @pytest .mark .asyncio
495
479
async def test_init_api_mode (api , monkeypatch ):
496
480
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 )
500
482
501
483
res = await api .init_api_mode ()
502
484
assert res is None
503
485
assert api .enter_at_command_mode .call_count == 1
504
486
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 )
508
488
509
489
res = await api .init_api_mode ()
510
490
assert res is False
@@ -517,9 +497,7 @@ async def enter_at_mode():
517
497
518
498
api ._uart .baudrate = 57600
519
499
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 )
523
501
524
502
res = await api .init_api_mode ()
525
503
assert res is True
@@ -542,21 +520,16 @@ def test_handle_many_to_one_rri(api):
542
520
api ._handle_many_to_one_rri (ieee , nwk , 0 )
543
521
544
522
545
- @pytest .mark .asyncio
546
523
async def test_reconnect_multiple_disconnects (monkeypatch , caplog ):
547
524
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 )
551
526
monkeypatch .setattr (uart , "connect" , connect_mock )
552
527
553
528
await api .connect ()
554
529
555
530
caplog .set_level (logging .DEBUG )
556
- connected = asyncio .Future ()
557
- connected .set_result (mock .sentinel .uart_reconnect )
558
531
connect_mock .reset_mock ()
559
- connect_mock .side_effect = [asyncio . Future (), connected ]
532
+ connect_mock .side_effect = [OSError , mock . sentinel . uart_reconnect ]
560
533
api .connection_lost ("connection lost" )
561
534
await asyncio .sleep (0.3 )
562
535
api .connection_lost ("connection lost 2" )
@@ -567,21 +540,20 @@ async def test_reconnect_multiple_disconnects(monkeypatch, caplog):
567
540
assert connect_mock .call_count == 2
568
541
569
542
570
- @pytest .mark .asyncio
571
543
async def test_reconnect_multiple_attempts (monkeypatch , caplog ):
572
544
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 )
576
546
monkeypatch .setattr (uart , "connect" , connect_mock )
577
547
578
548
await api .connect ()
579
549
580
550
caplog .set_level (logging .DEBUG )
581
- connected = asyncio .Future ()
582
- connected .set_result (mock .sentinel .uart_reconnect )
583
551
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
+ ]
585
557
586
558
with mock .patch ("asyncio.sleep" ):
587
559
api .connection_lost ("connection lost" )
@@ -591,8 +563,7 @@ async def test_reconnect_multiple_attempts(monkeypatch, caplog):
591
563
assert connect_mock .call_count == 3
592
564
593
565
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 )
596
567
@mock .patch .object (uart , "connect" )
597
568
async def test_probe_success (mock_connect , mock_at_cmd ):
598
569
"""Test device probing."""
@@ -606,7 +577,6 @@ async def test_probe_success(mock_connect, mock_at_cmd):
606
577
assert mock_connect .return_value .close .call_count == 1
607
578
608
579
609
- @pytest .mark .asyncio
610
580
@mock .patch .object (xbee_api .XBee , "init_api_mode" , return_value = True )
611
581
@mock .patch .object (xbee_api .XBee , "_at_command" , side_effect = asyncio .TimeoutError )
612
582
@mock .patch .object (uart , "connect" )
@@ -623,7 +593,6 @@ async def test_probe_success_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
623
593
assert mock_connect .return_value .close .call_count == 1
624
594
625
595
626
- @pytest .mark .asyncio
627
596
@mock .patch .object (xbee_api .XBee , "init_api_mode" )
628
597
@mock .patch .object (xbee_api .XBee , "_at_command" , side_effect = asyncio .TimeoutError )
629
598
@mock .patch .object (uart , "connect" )
@@ -648,7 +617,6 @@ async def test_probe_fail(mock_connect, mock_at_cmd, mock_api_mode, exception):
648
617
assert mock_connect .return_value .close .call_count == 1
649
618
650
619
651
- @pytest .mark .asyncio
652
620
@mock .patch .object (xbee_api .XBee , "init_api_mode" , return_value = False )
653
621
@mock .patch .object (xbee_api .XBee , "_at_command" , side_effect = asyncio .TimeoutError )
654
622
@mock .patch .object (uart , "connect" )
@@ -668,7 +636,6 @@ async def test_probe_fail_api_mode(mock_connect, mock_at_cmd, mock_api_mode):
668
636
assert mock_connect .return_value .close .call_count == 1
669
637
670
638
671
- @pytest .mark .asyncio
672
639
@mock .patch .object (xbee_api .XBee , "connect" )
673
640
async def test_xbee_new (conn_mck ):
674
641
"""Test new class method."""
0 commit comments