Skip to content

Commit 039e89e

Browse files
authored
Test coverage transaction 100%. (#2756)
1 parent 3252244 commit 039e89e

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

test/transaction/test_transaction.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test transaction."""
22
import asyncio
3+
import copy
34
from unittest import mock
45

56
import pytest
@@ -182,7 +183,7 @@ async def test_transaction_data_2(self, use_clc, test):
182183
transact.response_future.set_result((1, pdu))
183184
transact.callback_data(packet)
184185

185-
@pytest.mark.parametrize("scenario", range(8))
186+
@pytest.mark.parametrize("scenario", range(10))
186187
async def test_transaction_execute(self, use_clc, scenario):
187188
"""Test tracers in disconnect."""
188189
transact = TransactionManager(
@@ -240,13 +241,33 @@ async def test_transaction_execute(self, use_clc, scenario):
240241
await asyncio.sleep(0.1)
241242
with pytest.raises(ModbusIOException):
242243
await resp
243-
else: # if scenario == 7: # response
244+
elif scenario == 7: # response
244245
transact.comm_params.timeout_connect = 0.2
245246
resp = asyncio.create_task(transact.execute(False, request))
246247
await asyncio.sleep(0.1)
247248
transact.response_future.set_result(response)
248249
await asyncio.sleep(0.1)
249250
assert response == await resp
251+
elif scenario == 8: # response wrong dev_id
252+
transact.comm_params.timeout_connect = 0.2
253+
resp = asyncio.create_task(transact.execute(False, request))
254+
await asyncio.sleep(0.1)
255+
new_resp = copy.deepcopy(response)
256+
new_resp.dev_id = 17
257+
transact.response_future.set_result(new_resp)
258+
await asyncio.sleep(0.1)
259+
with pytest.raises(ModbusIOException):
260+
resp.result()
261+
else: # if scenario == 9: # response wrong tid
262+
transact.comm_params.timeout_connect = 0.2
263+
resp = asyncio.create_task(transact.execute(False, request))
264+
await asyncio.sleep(0.1)
265+
new_resp = copy.deepcopy(response)
266+
new_resp.transaction_id = 17
267+
transact.response_future.set_result(new_resp)
268+
await asyncio.sleep(0.1)
269+
with pytest.raises(ModbusIOException):
270+
resp.result()
250271

251272
async def test_transaction_receiver(self, use_clc):
252273
"""Test tracers in disconnect."""
@@ -423,7 +444,7 @@ def test_sync_transaction_instance(self, use_clc):
423444
)
424445

425446

426-
@pytest.mark.parametrize("scenario", range(7))
447+
@pytest.mark.parametrize("scenario", range(10))
427448
async def test_sync_transaction_execute(self, use_clc, scenario):
428449
"""Test tracers in disconnect."""
429450
client = ModbusBaseSyncClient(
@@ -479,14 +500,40 @@ async def test_sync_transaction_execute(self, use_clc, scenario):
479500
transact.comm_params.timeout_connect = 0.1
480501
with pytest.raises(ModbusIOException):
481502
transact.sync_execute(False, request)
482-
else: # if scenario == 6 # response
503+
elif scenario == 6: # response
483504
transact.transport = 1
484505
resp_bytes = transact.framer.buildFrame(response)
485506
transact.sync_client.recv = mock.Mock(return_value=resp_bytes)
486507
transact.sync_client.send = mock.Mock()
487508
transact.comm_params.timeout_connect = 0.2
488509
resp = transact.sync_execute(False, request)
489510
assert response.bits == resp.bits
511+
elif scenario == 7: # response wrong dev_id
512+
transact.transport = 1
513+
pdu = copy.deepcopy(response)
514+
pdu.dev_id = 17
515+
transact.sync_get_response = mock.Mock(return_value=pdu)
516+
transact.pdu_send = mock.Mock()
517+
transact.comm_params.timeout_connect = 0.2
518+
with pytest.raises(ModbusIOException):
519+
transact.sync_execute(False, request)
520+
elif scenario == 8: # response wrong tid
521+
transact.transport = 1
522+
pdu = copy.deepcopy(response)
523+
pdu.transaction_id = 17
524+
transact.sync_get_response = mock.Mock(return_value=pdu)
525+
transact.pdu_send = mock.Mock()
526+
transact.comm_params.timeout_connect = 0.2
527+
with pytest.raises(ModbusIOException):
528+
transact.sync_execute(False, request)
529+
else : # if scenario == 9 # pdu_send from client
530+
transact.transport = 1
531+
transact.is_server = True
532+
resp_bytes = transact.framer.buildFrame(response)
533+
transact.sync_client.recv = mock.Mock(return_value=resp_bytes)
534+
transact.sync_client.send = mock.Mock()
535+
transact.comm_params.timeout_connect = 0.2
536+
transact.pdu_send(response)
490537

491538
def test_sync_transaction_receiver(self, use_clc):
492539
"""Test tracers in disconnect."""

0 commit comments

Comments
 (0)