From 35503cb19315c82fb46542289c214a6f2e191679 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Mon, 21 Oct 2024 13:16:49 +0200 Subject: [PATCH] All pdu (incl. function code) tests to pdu directory. (#2397) --- pymodbus/pdu/bit_read_message.py | 4 +- pymodbus/pdu/bit_write_message.py | 4 +- pymodbus/pdu/diag_message.py | 78 +++++++++---------- pymodbus/pdu/file_message.py | 6 +- pymodbus/pdu/mei_message.py | 2 +- pymodbus/pdu/other_message.py | 8 +- pymodbus/pdu/register_read_message.py | 6 +- pymodbus/pdu/register_write_message.py | 6 +- pymodbus/server/async_io.py | 4 +- .../test_all_messages.py | 0 .../test_bit_read_messages.py | 15 ++-- .../test_bit_write_messages.py | 21 ++--- .../test_diag_messages.py | 16 ++-- .../{sub_current => pdu}/test_file_message.py | 16 ++-- .../test_mei_messages.py | 12 +-- .../test_other_messages.py | 14 ++-- .../test_register_read_messages.py | 19 ++--- .../test_register_write_messages.py | 35 +++++---- test/sub_client/test_client.py | 2 +- test/sub_server/test_server_asyncio.py | 2 +- 20 files changed, 137 insertions(+), 133 deletions(-) rename test/{sub_function_codes => pdu}/test_all_messages.py (100%) rename test/{sub_function_codes => pdu}/test_bit_read_messages.py (91%) rename test/{sub_function_codes => pdu}/test_bit_write_messages.py (89%) rename test/{sub_function_codes => pdu}/test_diag_messages.py (94%) rename test/{sub_current => pdu}/test_file_message.py (96%) rename test/{sub_function_codes => pdu}/test_mei_messages.py (93%) rename test/{sub_function_codes => pdu}/test_other_messages.py (91%) rename test/{sub_function_codes => pdu}/test_register_read_messages.py (92%) rename test/{sub_function_codes => pdu}/test_register_write_messages.py (87%) diff --git a/pymodbus/pdu/bit_read_message.py b/pymodbus/pdu/bit_read_message.py index cc684bb82..cb879e87d 100644 --- a/pymodbus/pdu/bit_read_message.py +++ b/pymodbus/pdu/bit_read_message.py @@ -141,7 +141,7 @@ def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode """ ReadBitsRequestBase.__init__(self, address, count, slave, transaction, skip_encode) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a read coils request against a datastore. Before running the request, we make sure that the request is in @@ -209,7 +209,7 @@ def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode """ ReadBitsRequestBase.__init__(self, address, count, slave, transaction, skip_encode) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a read discrete input request against a datastore. Before running the request, we make sure that the request is in diff --git a/pymodbus/pdu/bit_write_message.py b/pymodbus/pdu/bit_write_message.py index e7631f8a5..a8270afb4 100644 --- a/pymodbus/pdu/bit_write_message.py +++ b/pymodbus/pdu/bit_write_message.py @@ -73,7 +73,7 @@ def decode(self, data): self.address, value = struct.unpack(">HH", data) self.value = value == ModbusStatus.ON - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a write coil request against a datastore. :param context: The datastore to request from @@ -199,7 +199,7 @@ def decode(self, data): values = unpack_bitstring(data[5:]) self.values = values[:count] - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a write coils request against a datastore. :param context: The datastore to request from diff --git a/pymodbus/pdu/diag_message.py b/pymodbus/pdu/diag_message.py index 176bc17f2..686c8bd52 100644 --- a/pymodbus/pdu/diag_message.py +++ b/pymodbus/pdu/diag_message.py @@ -86,7 +86,7 @@ class DiagnosticStatusResponse(ModbusPDU): It works by performing all of the encoding and decoding of variable data and lets the higher classes define what extra data to append - and how to execute a request + and how to update_datastore a request """ function_code = 0x08 @@ -145,7 +145,7 @@ class DiagnosticStatusSimpleRequest(DiagnosticStatusRequest): 2 bytes of data. If a function inherits this, they only need to implement - the execute method + the update_datastore method """ def __init__(self, data=0x0000, slave=1, transaction=0, skip_encode=False): @@ -159,9 +159,9 @@ def __init__(self, data=0x0000, slave=1, transaction=0, skip_encode=False): DiagnosticStatusRequest.__init__(self, slave=slave, transaction=transaction, skip_encode=skip_encode) self.message = data - async def execute(self, *args): # pragma: no cover + async def update_datastore(self, *args): # pragma: no cover """Raise if not implemented.""" - raise NotImplementedException("Diagnostic Message Has No Execute Method") + raise NotImplementedException("Diagnostic Message Has No update_datastore Method") class DiagnosticStatusSimpleResponse(DiagnosticStatusResponse): @@ -205,8 +205,8 @@ def __init__(self, message=b"\x00\x00", slave=1, transaction=0, skip_encode=Fals raise ModbusException(f"message({type(message)}) must be bytes") self.message = message - async def execute(self, *_args): # pragma: no cover - """Execute the loopback request (builds the response). + async def update_datastore(self, *_args): # pragma: no cover + """update_datastore the loopback request (builds the response). :returns: The populated loopback response message """ @@ -245,7 +245,7 @@ class RestartCommunicationsOptionRequest(DiagnosticStatusRequest): currently in Listen Only Mode, no response is returned. This function is the only one that brings the port out of Listen Only Mode. If the port is not currently in Listen Only Mode, a normal response is returned. This - occurs before the restart is executed. + occurs before the restart is update_datastored. """ sub_function_code = 0x0001 @@ -261,7 +261,7 @@ def __init__(self, toggle=False, slave=1, transaction=0, skip_encode=False): else: self.message = [ModbusStatus.OFF] # pragma: no cover - async def execute(self, *_args): # pragma: no cover + async def update_datastore(self, *_args): # pragma: no cover """Clear event log and restart. :returns: The initialized response message @@ -278,7 +278,7 @@ class RestartCommunicationsOptionResponse(DiagnosticStatusResponse): currently in Listen Only Mode, no response is returned. This function is the only one that brings the port out of Listen Only Mode. If the port is not currently in Listen Only Mode, a normal response is returned. This - occurs before the restart is executed. + occurs before the restart is update_datastored. """ sub_function_code = 0x0001 @@ -303,8 +303,8 @@ class ReturnDiagnosticRegisterRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0002 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -337,8 +337,8 @@ class ChangeAsciiInputDelimiterRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0003 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -372,8 +372,8 @@ class ForceListenOnlyModeRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0004 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -410,8 +410,8 @@ class ClearCountersRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x000A - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -441,8 +441,8 @@ class ReturnBusMessageCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x000B - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -474,8 +474,8 @@ class ReturnBusCommunicationErrorCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x000C - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -507,8 +507,8 @@ class ReturnBusExceptionErrorCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x000D - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -540,8 +540,8 @@ class ReturnSlaveMessageCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x000E - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -573,8 +573,8 @@ class ReturnSlaveNoResponseCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x000F - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -607,8 +607,8 @@ class ReturnSlaveNAKCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0010 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -641,8 +641,8 @@ class ReturnSlaveBusyCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0011 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -676,8 +676,8 @@ class ReturnSlaveBusCharacterOverrunCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0012 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -709,8 +709,8 @@ class ReturnIopOverrunCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0013 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -742,8 +742,8 @@ class ClearOverrunCountRequest(DiagnosticStatusSimpleRequest): sub_function_code = 0x0014 - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ @@ -794,8 +794,8 @@ def get_response_pdu_size(self): data = 0 return 1 + 2 + 2 + 2 + data - async def execute(self, *args): # pragma: no cover - """Execute the diagnostic request on the given device. + async def update_datastore(self, *args): # pragma: no cover + """update_datastore the diagnostic request on the given device. :returns: The initialized response message """ diff --git a/pymodbus/pdu/file_message.py b/pymodbus/pdu/file_message.py index e63ada5d5..96804b8ad 100644 --- a/pymodbus/pdu/file_message.py +++ b/pymodbus/pdu/file_message.py @@ -130,7 +130,7 @@ def decode(self, data): if decoded[0] == 0x06: # pragma: no cover self.records.append(record) - def execute(self, _context): # pragma: no cover + def update_datastore(self, _context): # pragma: no cover """Run a read exception status request against the store. :returns: The populated response @@ -257,7 +257,7 @@ def decode(self, data): if decoded[0] == 0x06: # pragma: no cover self.records.append(record) - def execute(self, _context): # pragma: no cover + def update_datastore(self, _context): # pragma: no cover """Run the write file record request against the context. :returns: The populated response @@ -362,7 +362,7 @@ def decode(self, data): """ self.address = struct.unpack(">H", data)[0] - def execute(self, _context): # pragma: no cover + def update_datastore(self, _context): # pragma: no cover """Run a read exception status request against the store. :returns: The populated response diff --git a/pymodbus/pdu/mei_message.py b/pymodbus/pdu/mei_message.py index be3dc14a5..75f4b53d2 100644 --- a/pymodbus/pdu/mei_message.py +++ b/pymodbus/pdu/mei_message.py @@ -80,7 +80,7 @@ def decode(self, data): params = struct.unpack(">BBB", data) self.sub_function_code, self.read_code, self.object_id = params - async def execute(self, _context): # pragma: no cover + async def update_datastore(self, _context): # pragma: no cover """Run a read exception status request against the store. :returns: The populated response diff --git a/pymodbus/pdu/other_message.py b/pymodbus/pdu/other_message.py index e3a439116..fd5b5d6bd 100644 --- a/pymodbus/pdu/other_message.py +++ b/pymodbus/pdu/other_message.py @@ -44,7 +44,7 @@ def decode(self, data): :param data: The incoming data """ - async def execute(self, _context=None): # pragma: no cover + async def update_datastore(self, _context=None): # pragma: no cover """Run a read exception status request against the store. :returns: The populated response @@ -143,7 +143,7 @@ def decode(self, data): :param data: The incoming data """ - async def execute(self, _context=None): # pragma: no cover + async def update_datastore(self, _context=None): # pragma: no cover """Run a read exception status request against the store. :returns: The populated response @@ -248,7 +248,7 @@ def decode(self, data): :param data: The incoming data """ - async def execute(self, _context=None): # pragma: no cover + async def update_datastore(self, _context=None): # pragma: no cover """Run a read exception status request against the store. :returns: The populated response @@ -373,7 +373,7 @@ def decode(self, data): :param data: The incoming data """ - async def execute(self, context=None): # pragma: no cover + async def update_datastore(self, context=None): # pragma: no cover """Run a report slave id request against the store. :returns: The populated response diff --git a/pymodbus/pdu/register_read_message.py b/pymodbus/pdu/register_read_message.py index 39c45562a..f60ad4b5b 100644 --- a/pymodbus/pdu/register_read_message.py +++ b/pymodbus/pdu/register_read_message.py @@ -133,7 +133,7 @@ def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode """ super().__init__(address, count, slave, transaction, skip_encode) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a read holding request against a datastore. :param context: The datastore to request from @@ -195,7 +195,7 @@ def __init__(self, address=None, count=None, slave=1, transaction=0, skip_encode """ super().__init__(address, count, slave, transaction, skip_encode) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a read input request against a datastore. :param context: The datastore to request from @@ -307,7 +307,7 @@ def decode(self, data): register = struct.unpack(">H", data[i : i + 2])[0] self.write_registers.append(register) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a write single register request against a datastore. :param context: The datastore to request from diff --git a/pymodbus/pdu/register_write_message.py b/pymodbus/pdu/register_write_message.py index e40a7b502..328e9a263 100644 --- a/pymodbus/pdu/register_write_message.py +++ b/pymodbus/pdu/register_write_message.py @@ -49,7 +49,7 @@ def decode(self, data): """ self.address, self.value = struct.unpack(">HH", data) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a write single register request against a datastore. :param context: The datastore to request from @@ -195,7 +195,7 @@ def decode(self, data): for idx in range(5, (self.count * 2) + 5, 2): self.values.append(struct.unpack(">H", data[idx : idx + 2])[0]) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a write single register request against a datastore. :param context: The datastore to request from @@ -316,7 +316,7 @@ def decode(self, data): """ self.address, self.and_mask, self.or_mask = struct.unpack(">HHH", data) - async def execute(self, context): # pragma: no cover + async def update_datastore(self, context): # pragma: no cover """Run a mask write register request against the store. :param context: The datastore to request from diff --git a/pymodbus/server/async_io.py b/pymodbus/server/async_io.py index 1f4b31f7e..d06154603 100644 --- a/pymodbus/server/async_io.py +++ b/pymodbus/server/async_io.py @@ -188,10 +188,10 @@ async def _async_execute(self, request, *addr): # if broadcasting then execute on all slave contexts, # note response will be ignored for slave_id in self.server.context.slaves(): - response = await request.execute(self.server.context[slave_id]) + response = await request.update_datastore(self.server.context[slave_id]) else: context = self.server.context[request.slave_id] - response = await request.execute(context) + response = await request.update_datastore(context) except NoSuchSlaveException: Log.error("requested slave does not exist: {}", request.slave_id) diff --git a/test/sub_function_codes/test_all_messages.py b/test/pdu/test_all_messages.py similarity index 100% rename from test/sub_function_codes/test_all_messages.py rename to test/pdu/test_all_messages.py diff --git a/test/sub_function_codes/test_bit_read_messages.py b/test/pdu/test_bit_read_messages.py similarity index 91% rename from test/sub_function_codes/test_bit_read_messages.py rename to test/pdu/test_bit_read_messages.py index 85f807e37..b93c7b193 100644 --- a/test/sub_function_codes/test_bit_read_messages.py +++ b/test/pdu/test_bit_read_messages.py @@ -15,7 +15,8 @@ ReadCoilsRequest, ReadDiscreteInputsRequest, ) -from test.conftest import MockContext + +from ..conftest import MockContext res = [True] * 21 @@ -85,7 +86,7 @@ def test_bit_read_base_requests(self): for request, expected in iter(messages.items()): assert request.encode() == expected - async def test_bit_read_message_execute_value_errors(self): + async def test_bit_read_message_update_datastore_value_errors(self): """Test bit read request encoding.""" context = MockContext() requests = [ @@ -93,10 +94,10 @@ async def test_bit_read_message_execute_value_errors(self): ReadDiscreteInputsRequest(1, 0x800, 0, 0, False), ] for request in requests: - result = await request.execute(context) + result = await request.update_datastore(context) assert ModbusExceptions.IllegalValue == result.exception_code - async def test_bit_read_message_execute_address_errors(self): + async def test_bit_read_message_update_datastore_address_errors(self): """Test bit read request encoding.""" context = MockContext() requests = [ @@ -104,10 +105,10 @@ async def test_bit_read_message_execute_address_errors(self): ReadDiscreteInputsRequest(1, 5, 0, 0, False), ] for request in requests: - result = await request.execute(context) + result = await request.update_datastore(context) assert ModbusExceptions.IllegalAddress == result.exception_code - async def test_bit_read_message_execute_success(self): + async def test_bit_read_message_update_datastore_success(self): """Test bit read request encoding.""" context = MockContext() context.validate = lambda a, b, c: True @@ -116,7 +117,7 @@ async def test_bit_read_message_execute_success(self): ReadDiscreteInputsRequest(1, 5, 0, False), ] for request in requests: - result = await request.execute(context) + result = await request.update_datastore(context) assert result.bits == [True] * 5 def test_bit_read_message_get_response_pdu(self): diff --git a/test/sub_function_codes/test_bit_write_messages.py b/test/pdu/test_bit_write_messages.py similarity index 89% rename from test/sub_function_codes/test_bit_write_messages.py rename to test/pdu/test_bit_write_messages.py index 54cae8b41..931d42637 100644 --- a/test/sub_function_codes/test_bit_write_messages.py +++ b/test/pdu/test_bit_write_messages.py @@ -13,7 +13,8 @@ WriteSingleCoilRequest, WriteSingleCoilResponse, ) -from test.conftest import FakeList, MockContext + +from ..conftest import FakeList, MockContext # ---------------------------------------------------------------------------# @@ -80,45 +81,45 @@ def test_write_single_coil_request_encode(self): request = WriteSingleCoilRequest(1, False) assert request.encode() == b"\x00\x01\x00\x00" - async def test_write_single_coil_execute(self): + async def test_write_single_coil_update_datastore(self): """Test write single coil.""" context = MockContext(False, default=True) request = WriteSingleCoilRequest(2, True) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalAddress context.valid = True - result = await request.execute(context) + result = await request.update_datastore(context) assert result.encode() == b"\x00\x02\xff\x00" context = MockContext(True, default=False) request = WriteSingleCoilRequest(2, False) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.encode() == b"\x00\x02\x00\x00" - async def test_write_multiple_coils_execute(self): + async def test_write_multiple_coils_update_datastore(self): """Test write multiple coils.""" context = MockContext(False) # too many values request = WriteMultipleCoilsRequest(2, FakeList(0x123456)) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalValue # bad byte count request = WriteMultipleCoilsRequest(2, [0x00] * 4) request.byte_count = 0x00 - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalValue # does not validate context.valid = False request = WriteMultipleCoilsRequest(2, [0x00] * 4) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalAddress # validated request context.valid = True - result = await request.execute(context) + result = await request.update_datastore(context) assert result.encode() == b"\x00\x02\x00\x04" def test_write_multiple_coils_response(self): diff --git a/test/sub_function_codes/test_diag_messages.py b/test/pdu/test_diag_messages.py similarity index 94% rename from test/sub_function_codes/test_diag_messages.py rename to test/pdu/test_diag_messages.py index d5caa11b6..d20eaf7df 100644 --- a/test/sub_function_codes/test_diag_messages.py +++ b/test/pdu/test_diag_messages.py @@ -142,7 +142,7 @@ async def test_diagnostic_simple_requests(self): request = DiagnosticStatusSimpleRequest(b"\x12\x34") request.sub_function_code = 0x1234 with pytest.raises(NotImplementedException): - await request.execute() + await request.update_datastore() assert request.encode() == b"\x12\x34\x12\x34" DiagnosticStatusSimpleResponse() @@ -158,11 +158,11 @@ def test_diagnostic_requests_encode(self): for msg, enc, _ in self.requests: assert msg().encode() == enc - async def test_diagnostic_execute(self): + async def test_diagnostic_update_datastore(self): """Testing diagnostic message execution.""" - for message, encoded, executed in self.requests: - encoded = (await message().execute()).encode() - assert encoded == executed + for message, encoded, update_datastored in self.requests: + encoded = (await message().update_datastore()).encode() + assert encoded == update_datastored def test_return_query_data_request(self): """Testing diagnostic message execution.""" @@ -190,13 +190,13 @@ def test_restart_communications_option(self): response = RestartCommunicationsOptionResponse(False) assert response.encode() == b"\x00\x01\x00\x00" - async def test_get_clear_modbus_plus_request_execute(self): + async def test_get_clear_modbus_plus_request_update_datastore(self): """Testing diagnostic message execution.""" request = GetClearModbusPlusRequest(data=ModbusPlusOperation.CLEAR_STATISTICS) - response = await request.execute() + response = await request.update_datastore() assert response.message == ModbusPlusOperation.CLEAR_STATISTICS request = GetClearModbusPlusRequest(data=ModbusPlusOperation.GET_STATISTICS) - response = await request.execute() + response = await request.update_datastore() resp = [ModbusPlusOperation.GET_STATISTICS] assert response.message == resp + [0x00] * 55 diff --git a/test/sub_current/test_file_message.py b/test/pdu/test_file_message.py similarity index 96% rename from test/sub_current/test_file_message.py rename to test/pdu/test_file_message.py index ab2aae876..fb18e96f3 100644 --- a/test/sub_current/test_file_message.py +++ b/test/pdu/test_file_message.py @@ -45,15 +45,15 @@ def test_read_fifo_queue_request(self): """Test basic bit message encoding/decoding.""" context = MockContext() handle = ReadFifoQueueRequest(0x1234) - result = handle.execute(context) + result = handle.update_datastore(context) assert isinstance(result, ReadFifoQueueResponse) handle.address = -1 - result = handle.execute(context) + result = handle.update_datastore(context) assert ModbusExceptions.IllegalValue == result.exception_code handle.values = [0x00] * 33 - result = handle.execute(context) + result = handle.update_datastore(context) assert ModbusExceptions.IllegalValue == result.exception_code def test_read_fifo_queue_request_error(self): @@ -61,7 +61,7 @@ def test_read_fifo_queue_request_error(self): context = MockContext() handle = ReadFifoQueueRequest(0x1234) handle.values = [0x00] * 32 - result = handle.execute(context) + result = handle.update_datastore(context) assert result.function_code == 0x98 def test_read_fifo_queue_response_encode(self): @@ -148,10 +148,10 @@ def test_read_file_record_request_rtu_frame_size(self): size = handle.calculateRtuFrameSize(request) assert size == 0x0E + 5 - def test_read_file_record_request_execute(self): + def test_read_file_record_request_update_datastore(self): """Test basic bit message encoding/decoding.""" handle = ReadFileRecordRequest() - result = handle.execute(None) + result = handle.update_datastore(None) assert isinstance(result, ReadFileRecordResponse) # -----------------------------------------------------------------------# @@ -221,10 +221,10 @@ def test_write_file_record_request_rtu_frame_size(self): size = handle.calculateRtuFrameSize(request) assert size == 0x0D + 5 - def test_write_file_record_request_execute(self): + def test_write_file_record_request_update_datastore(self): """Test basic bit message encoding/decoding.""" handle = WriteFileRecordRequest() - result = handle.execute(None) + result = handle.update_datastore(None) assert isinstance(result, WriteFileRecordResponse) # -----------------------------------------------------------------------# diff --git a/test/sub_function_codes/test_mei_messages.py b/test/pdu/test_mei_messages.py similarity index 93% rename from test/sub_function_codes/test_mei_messages.py rename to test/pdu/test_mei_messages.py index 371b23e17..2c81a8538 100644 --- a/test/sub_function_codes/test_mei_messages.py +++ b/test/pdu/test_mei_messages.py @@ -53,7 +53,7 @@ async def test_read_device_information_request(self): control.Identity.update({0x81: ["Test", "Repeated"]}) handle = ReadDeviceInformationRequest() - result = await handle.execute(context) + result = await handle.update_datastore(context) assert isinstance(result, ReadDeviceInformationResponse) assert result.information[0x00] == "Company" assert result.information[0x01] == "Product" @@ -64,20 +64,20 @@ async def test_read_device_information_request(self): handle = ReadDeviceInformationRequest( read_code=DeviceInformation.EXTENDED, object_id=0x80 ) - result = await handle.execute(context) + result = await handle.update_datastore(context) assert result.information[0x81] == ["Test", "Repeated"] async def test_read_device_information_request_error(self): """Test basic bit message encoding/decoding.""" handle = ReadDeviceInformationRequest() handle.read_code = -1 - assert (await handle.execute(None)).function_code == 0xAB + assert (await handle.update_datastore(None)).function_code == 0xAB handle.read_code = 0x05 - assert (await handle.execute(None)).function_code == 0xAB + assert (await handle.update_datastore(None)).function_code == 0xAB handle.object_id = -1 - assert (await handle.execute(None)).function_code == 0xAB + assert (await handle.update_datastore(None)).function_code == 0xAB handle.object_id = 0x100 - assert (await handle.execute(None)).function_code == 0xAB + assert (await handle.update_datastore(None)).function_code == 0xAB def test_read_device_information_encode(self): """Test that the read fifo queue response can encode.""" diff --git a/test/sub_function_codes/test_other_messages.py b/test/pdu/test_other_messages.py similarity index 91% rename from test/sub_function_codes/test_other_messages.py rename to test/pdu/test_other_messages.py index f5c51d6d4..9fe83bdbc 100644 --- a/test/sub_function_codes/test_other_messages.py +++ b/test/pdu/test_other_messages.py @@ -33,7 +33,7 @@ async def test_read_exception_status(self): request = pymodbus_message.ReadExceptionStatusRequest() request.decode(b"\x12") assert not request.encode() - assert (await request.execute()).function_code == 0x07 + assert (await request.update_datastore()).function_code == 0x07 response = pymodbus_message.ReadExceptionStatusResponse(0x12) assert response.encode() == b"\x12" @@ -45,7 +45,7 @@ async def test_get_comm_event_counter(self): request = pymodbus_message.GetCommEventCounterRequest() request.decode(b"\x12") assert not request.encode() - assert (await request.execute()).function_code == 0x0B + assert (await request.update_datastore()).function_code == 0x0B response = pymodbus_message.GetCommEventCounterResponse(0x12) assert response.encode() == b"\x00\x00\x00\x12" @@ -61,7 +61,7 @@ async def test_get_comm_event_log(self): request = pymodbus_message.GetCommEventLogRequest() request.decode(b"\x12") assert not request.encode() - assert (await request.execute()).function_code == 0x0C + assert (await request.update_datastore()).function_code == 0x0C response = pymodbus_message.GetCommEventLogResponse() assert response.encode() == b"\x06\x00\x00\x00\x00\x00\x00" @@ -103,7 +103,7 @@ async def test_report_slave_id_request(self): expected_identity = "-".join(identity.values()).encode() request = pymodbus_message.ReportSlaveIdRequest() - response = await request.execute() + response = await request.update_datastore() assert response.identifier == expected_identity # Change to byte strings and test again (final result should be the same) @@ -121,7 +121,7 @@ async def test_report_slave_id_request(self): dif.get.return_value = identity request = pymodbus_message.ReportSlaveIdRequest() - response = await request.execute() + response = await request.update_datastore() assert response.identifier == expected_identity async def test_report_slave_id(self): @@ -131,10 +131,10 @@ async def test_report_slave_id(self): request = pymodbus_message.ReportSlaveIdRequest() request.decode(b"\x12") assert not request.encode() - assert (await request.execute()).function_code == 0x11 + assert (await request.update_datastore()).function_code == 0x11 response = pymodbus_message.ReportSlaveIdResponse( - (await request.execute()).identifier, True + (await request.update_datastore()).identifier, True ) assert response.encode() == b"\tPymodbus\xff" diff --git a/test/sub_function_codes/test_register_read_messages.py b/test/pdu/test_register_read_messages.py similarity index 92% rename from test/sub_function_codes/test_register_read_messages.py rename to test/pdu/test_register_read_messages.py index ce5504e65..b25c2b122 100644 --- a/test/sub_function_codes/test_register_read_messages.py +++ b/test/pdu/test_register_read_messages.py @@ -10,7 +10,8 @@ ReadWriteMultipleRegistersRequest, ReadWriteMultipleRegistersResponse, ) -from test.conftest import FakeList, MockContext + +from ..conftest import FakeList, MockContext TEST_MESSAGE = b"\x06\x00\x0a\x00\x0b\x00\x0c" @@ -103,7 +104,7 @@ async def test_register_read_requests_count_errors(self): ), ] for request in requests: - result = await request.execute(None) + result = await request.update_datastore(None) assert ModbusExceptions.IllegalValue == result.exception_code async def test_register_read_requests_validate_errors(self): @@ -119,10 +120,10 @@ async def test_register_read_requests_validate_errors(self): # ReadWriteMultipleRegistersRequest(1,5,-1,5), ] for request in requests: - result = await request.execute(context) + result = await request.update_datastore(context) assert ModbusExceptions.IllegalAddress == result.exception_code - async def test_register_read_requests_execute(self): + async def test_register_read_requests_update_datastore(self): """This tests that the register request messages. will break on counts that are out of range @@ -133,7 +134,7 @@ async def test_register_read_requests_execute(self): ReadInputRegistersRequest(-1, 5), ] for request in requests: - response = await request.execute(context) + response = await request.update_datastore(context) assert request.function_code == response.function_code async def test_read_write_multiple_registers_request(self): @@ -142,7 +143,7 @@ async def test_read_write_multiple_registers_request(self): request = ReadWriteMultipleRegistersRequest( read_address=1, read_count=10, write_address=1, write_registers=[0x00] ) - response = await request.execute(context) + response = await request.update_datastore(context) assert request.function_code == response.function_code async def test_read_write_multiple_registers_validate(self): @@ -152,15 +153,15 @@ async def test_read_write_multiple_registers_validate(self): request = ReadWriteMultipleRegistersRequest( read_address=1, read_count=10, write_address=2, write_registers=[0x00] ) - response = await request.execute(context) + response = await request.update_datastore(context) assert response.exception_code == ModbusExceptions.IllegalAddress context.validate = lambda f, a, c: a == 2 - response = await request.execute(context) + response = await request.update_datastore(context) assert response.exception_code == ModbusExceptions.IllegalAddress request.write_byte_count = 0x100 - response = await request.execute(context) + response = await request.update_datastore(context) assert response.exception_code == ModbusExceptions.IllegalValue def test_read_write_multiple_registers_request_decode(self): diff --git a/test/sub_function_codes/test_register_write_messages.py b/test/pdu/test_register_write_messages.py similarity index 87% rename from test/sub_function_codes/test_register_write_messages.py rename to test/pdu/test_register_write_messages.py index 8d702fb77..40d64ab3d 100644 --- a/test/sub_function_codes/test_register_write_messages.py +++ b/test/pdu/test_register_write_messages.py @@ -9,7 +9,8 @@ WriteSingleRegisterRequest, WriteSingleRegisterResponse, ) -from test.conftest import MockContext, MockLastValuesContext + +from ..conftest import MockContext, MockLastValuesContext # ---------------------------------------------------------------------------# @@ -87,43 +88,43 @@ async def test_write_single_register_request(self): """Test write single register request.""" context = MockContext() request = WriteSingleRegisterRequest(0x00, 0xF0000) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalValue request.value = 0x00FF - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalAddress context.valid = True - result = await request.execute(context) + result = await request.update_datastore(context) assert result.function_code == request.function_code async def test_write_multiple_register_request(self): """Test write multiple register request.""" context = MockContext() request = WriteMultipleRegistersRequest(0x00, [0x00] * 10) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalAddress request.count = 0x05 # bytecode != code * 2 - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalValue request.count = 0x800 # outside of range - result = await request.execute(context) + result = await request.update_datastore(context) assert result.exception_code == ModbusExceptions.IllegalValue context.valid = True request = WriteMultipleRegistersRequest(0x00, [0x00] * 10) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.function_code == request.function_code request = WriteMultipleRegistersRequest(0x00, 0x00) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.function_code == request.function_code request = WriteMultipleRegistersRequest(0x00, [0x00]) - result = await request.execute(context) + result = await request.update_datastore(context) assert result.function_code == request.function_code # -----------------------------------------------------------------------# @@ -145,7 +146,7 @@ def test_mask_write_register_request_decode(self): assert handle.and_mask == 0x00F2 assert handle.or_mask == 0x0025 - async def test_mask_write_register_request_execute(self): + async def test_mask_write_register_request_update_datastore(self): """Test write register request valid execution.""" # The test uses the 4 nibbles of the 16-bit values to test # the combinations: @@ -155,23 +156,23 @@ async def test_mask_write_register_request_execute(self): # and_mask=F, or_mask=F context = MockLastValuesContext(valid=True, default=0xAA55) handle = MaskWriteRegisterRequest(0x0000, 0x0F0F, 0x00FF) - result = await handle.execute(context) + result = await handle.update_datastore(context) assert isinstance(result, MaskWriteRegisterResponse) assert context.last_values == [0x0AF5] - async def test_mask_write_register_request_invalid_execute(self): - """Test write register request execute with invalid data.""" + async def test_mask_write_register_request_invalid_update_datastore(self): + """Test write register request update_datastore with invalid data.""" context = MockContext(valid=False, default=0x0000) handle = MaskWriteRegisterRequest(0x0000, -1, 0x1010) - result = await handle.execute(context) + result = await handle.update_datastore(context) assert ModbusExceptions.IllegalValue == result.exception_code handle = MaskWriteRegisterRequest(0x0000, 0x0101, -1) - result = await handle.execute(context) + result = await handle.update_datastore(context) assert ModbusExceptions.IllegalValue == result.exception_code handle = MaskWriteRegisterRequest(0x0000, 0x0101, 0x1010) - result = await handle.execute(context) + result = await handle.update_datastore(context) assert ModbusExceptions.IllegalAddress == result.exception_code # -----------------------------------------------------------------------# diff --git a/test/sub_client/test_client.py b/test/sub_client/test_client.py index 74b4feff2..1bb08847c 100755 --- a/test/sub_client/test_client.py +++ b/test/sub_client/test_client.py @@ -386,7 +386,7 @@ def __init__(self, base, req, retries=0): async def delayed_resp(self): """Send a response to a received packet.""" await asyncio.sleep(0.05) - resp = await self.req.execute(self.ctx) + resp = await self.req.update_datastore(self.ctx) pkt = self.base.ctx.framer.buildFrame(resp) self.base.ctx.data_received(pkt) diff --git a/test/sub_server/test_server_asyncio.py b/test/sub_server/test_server_asyncio.py index a8027583f..2f40a8fce 100755 --- a/test/sub_server/test_server_asyncio.py +++ b/test/sub_server/test_server_asyncio.py @@ -266,7 +266,7 @@ async def test_async_tcp_server_modbus_error(self): BasicClient.data = TEST_DATA await self.start_server() with mock.patch( - "pymodbus.pdu.register_read_message.ReadHoldingRegistersRequest.execute", + "pymodbus.pdu.register_read_message.ReadHoldingRegistersRequest.update_datastore", side_effect=NoSuchSlaveException, ): await self.connect_server()