Skip to content

Commit

Permalink
Change unittest to pytest. (#1428)
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Mar 12, 2023
1 parent b1d4d83 commit 4499978
Show file tree
Hide file tree
Showing 57 changed files with 1,563 additions and 1,728 deletions.
2 changes: 1 addition & 1 deletion API_changes.rst
Expand Up @@ -6,7 +6,7 @@ PyModbus - API changes.
Version 3.3.0
-------------
- Clients have an optional parameter: on_reconnect_callback, Function that will be called just before a reconnection attempt.
- RemoteSlaveContent unit -> slave
- general parameter unit= -> slave=

-------------
Version 3.2.0
Expand Down
44 changes: 24 additions & 20 deletions examples/client_calls.py
Expand Up @@ -196,71 +196,75 @@ async def _execute_information_requests(client):
"""Execute extended information requests."""
_logger.info("### Running information requests.")
rr = _check_call(
await client.execute(req_mei.ReadDeviceInformationRequest(unit=SLAVE))
await client.execute(req_mei.ReadDeviceInformationRequest(slave=SLAVE))
)
assert rr.information[0] == b"Pymodbus"

rr = _check_call(await client.execute(req_other.ReportSlaveIdRequest(unit=SLAVE)))
rr = _check_call(await client.execute(req_other.ReportSlaveIdRequest(slave=SLAVE)))
assert rr.status

rr = _check_call(
await client.execute(req_other.ReadExceptionStatusRequest(unit=SLAVE))
await client.execute(req_other.ReadExceptionStatusRequest(slave=SLAVE))
)
assert not rr.status

rr = _check_call(
await client.execute(req_other.GetCommEventCounterRequest(unit=SLAVE))
await client.execute(req_other.GetCommEventCounterRequest(slave=SLAVE))
)
assert rr.status and not rr.count

rr = _check_call(await client.execute(req_other.GetCommEventLogRequest(unit=SLAVE)))
rr = _check_call(
await client.execute(req_other.GetCommEventLogRequest(slave=SLAVE))
)
assert rr.status and not (rr.event_count + rr.message_count + len(rr.events))


async def _execute_diagnostic_requests(client):
"""Execute extended diagnostic requests."""
_logger.info("### Running diagnostic requests.")
rr = _check_call(await client.execute(req_diag.ReturnQueryDataRequest(unit=SLAVE)))
rr = _check_call(await client.execute(req_diag.ReturnQueryDataRequest(slave=SLAVE)))
assert not rr.message[0]

_check_call(
await client.execute(req_diag.RestartCommunicationsOptionRequest(unit=SLAVE))
await client.execute(req_diag.RestartCommunicationsOptionRequest(slave=SLAVE))
)
_check_call(
await client.execute(req_diag.ReturnDiagnosticRegisterRequest(unit=SLAVE))
await client.execute(req_diag.ReturnDiagnosticRegisterRequest(slave=SLAVE))
)
_check_call(
await client.execute(req_diag.ChangeAsciiInputDelimiterRequest(unit=SLAVE))
await client.execute(req_diag.ChangeAsciiInputDelimiterRequest(slave=SLAVE))
)

# NOT WORKING: _check_call(await client.execute(req_diag.ForceListenOnlyModeRequest(unit=SLAVE)))
# NOT WORKING: _check_call(await client.execute(req_diag.ForceListenOnlyModeRequest(slave=SLAVE)))
# does not send a response

_check_call(await client.execute(req_diag.ClearCountersRequest()))
_check_call(
await client.execute(
req_diag.ReturnBusCommunicationErrorCountRequest(unit=SLAVE)
req_diag.ReturnBusCommunicationErrorCountRequest(slave=SLAVE)
)
)
_check_call(
await client.execute(req_diag.ReturnBusExceptionErrorCountRequest(unit=SLAVE))
await client.execute(req_diag.ReturnBusExceptionErrorCountRequest(slave=SLAVE))
)
_check_call(
await client.execute(req_diag.ReturnSlaveMessageCountRequest(unit=SLAVE))
await client.execute(req_diag.ReturnSlaveMessageCountRequest(slave=SLAVE))
)
_check_call(
await client.execute(req_diag.ReturnSlaveNoResponseCountRequest(unit=SLAVE))
await client.execute(req_diag.ReturnSlaveNoResponseCountRequest(slave=SLAVE))
)
_check_call(await client.execute(req_diag.ReturnSlaveNAKCountRequest(unit=SLAVE)))
_check_call(await client.execute(req_diag.ReturnSlaveBusyCountRequest(unit=SLAVE)))
_check_call(await client.execute(req_diag.ReturnSlaveNAKCountRequest(slave=SLAVE)))
_check_call(await client.execute(req_diag.ReturnSlaveBusyCountRequest(slave=SLAVE)))
_check_call(
await client.execute(
req_diag.ReturnSlaveBusCharacterOverrunCountRequest(unit=SLAVE)
req_diag.ReturnSlaveBusCharacterOverrunCountRequest(slave=SLAVE)
)
)
_check_call(await client.execute(req_diag.ReturnIopOverrunCountRequest(unit=SLAVE)))
_check_call(await client.execute(req_diag.ClearOverrunCountRequest(unit=SLAVE)))
# NOT WORKING _check_call(await client.execute(req_diag.GetClearModbusPlusRequest(unit=SLAVE)))
_check_call(
await client.execute(req_diag.ReturnIopOverrunCountRequest(slave=SLAVE))
)
_check_call(await client.execute(req_diag.ClearOverrunCountRequest(slave=SLAVE)))
# NOT WORKING _check_call(await client.execute(req_diag.GetClearModbusPlusRequest(slave=SLAVE)))


# ------------------------
Expand Down
6 changes: 3 additions & 3 deletions examples/client_test.py
Expand Up @@ -63,19 +63,19 @@ async def _execute_information_requests(client):
"""Execute extended information requests."""
_logger.info("### Running information requests.")
rr = _check_call(
await client.execute(req_mei.ReadDeviceInformationRequest(unit=SLAVE))
await client.execute(req_mei.ReadDeviceInformationRequest(slave=SLAVE))
)
assert rr.information[0] == b"pymodbus"


async def _execute_diagnostic_requests(client):
"""Execute extended diagnostic requests."""
_logger.info("### Running diagnostic requests.")
rr = _check_call(await client.execute(req_diag.ReturnQueryDataRequest(unit=SLAVE)))
rr = _check_call(await client.execute(req_diag.ReturnQueryDataRequest(slave=SLAVE)))
assert not rr.message[0]

_check_call(
await client.execute(req_diag.RestartCommunicationsOptionRequest(unit=SLAVE))
await client.execute(req_diag.RestartCommunicationsOptionRequest(slave=SLAVE))
)


Expand Down
2 changes: 1 addition & 1 deletion examples/server_async.py
Expand Up @@ -112,7 +112,7 @@ def setup_server(args):
single = False
else:
context = ModbusSlaveContext(
di=datablock, co=datablock, hr=datablock, ir=datablock, unit=1
di=datablock, co=datablock, hr=datablock, ir=datablock
)
single = True

Expand Down
4 changes: 1 addition & 3 deletions examples/server_updating.py
Expand Up @@ -69,9 +69,7 @@ def setup_updating_server(args):

# Continuing, use a sequential block without gaps.
datablock = ModbusSequentialDataBlock(0x00, [17] * 100)
context = ModbusSlaveContext(
di=datablock, co=datablock, hr=datablock, ir=datablock, unit=1
)
context = ModbusSlaveContext(di=datablock, co=datablock, hr=datablock, ir=datablock)
args.context = ModbusServerContext(slaves=context, single=True)
return setup_server(args)

Expand Down
3 changes: 1 addition & 2 deletions examples/v2.5.3/custom_message.py
Expand Up @@ -76,9 +76,8 @@ class CustomModbusRequest(ModbusRequest):
function_code = 55
_rtu_frame_size = 8

def __init__(self, address=None, slave=0, **kwargs):
def __init__(self, address=None, **kwargs):
"""Initialize."""
kwargs["unit"] = slave
ModbusRequest.__init__(self, **kwargs)
self.address = address
self.count = 16
Expand Down
4 changes: 2 additions & 2 deletions examples/v2.5.3/deviceinfo_showcase_client.py
Expand Up @@ -79,7 +79,7 @@ def run_sync_client():
# specify slave to query
# ------------------------------------------------------------------------#
# The slave to query is specified in an optional parameter for each
# individual request. This can be done by specifying the `unit` parameter
# individual request. This can be done by specifying the `slave` parameter
# which defaults to `0x00`
# ----------------------------------------------------------------------- #
log.debug("Reading Device Information")
Expand All @@ -89,7 +89,7 @@ def run_sync_client():
while not rr or rr.more_follows:
next_object_id = rr.next_object_id if rr else 0
rq = ReadDeviceInformationRequest(
read_code=0x03, unit=UNIT, object_id=next_object_id
read_code=0x03, slave=UNIT, object_id=next_object_id
)
rr = client.execute(rq)
information.update(rr.information)
Expand Down
36 changes: 18 additions & 18 deletions pymodbus/bit_read_message.py
Expand Up @@ -13,14 +13,14 @@ class ReadBitsRequestBase(ModbusRequest):

_rtu_frame_size = 8

def __init__(self, address, count, unit=Defaults.Slave, **kwargs):
def __init__(self, address, count, slave=Defaults.Slave, **kwargs):
"""Initialize the read request data.
:param address: The start address to read from
:param count: The number of bits after "address" to read
:param unit: Modbus slave unit ID
:param slave: Modbus slave slave ID
"""
ModbusRequest.__init__(self, unit, **kwargs)
ModbusRequest.__init__(self, slave, **kwargs)
self.address = address
self.count = count

Expand Down Expand Up @@ -67,13 +67,13 @@ class ReadBitsResponseBase(ModbusResponse):

_rtu_byte_count_pos = 2

def __init__(self, values, unit=Defaults.Slave, **kwargs):
def __init__(self, values, slave=Defaults.Slave, **kwargs):
"""Initialize a new instance.
:param values: The requested values to be returned
:param unit: Modbus slave unit ID
:param slave: Modbus slave slave ID
"""
ModbusResponse.__init__(self, unit, **kwargs)
ModbusResponse.__init__(self, slave, **kwargs)

#: A list of booleans representing bit values
self.bits = values or []
Expand Down Expand Up @@ -138,14 +138,14 @@ class ReadCoilsRequest(ReadBitsRequestBase):
function_code = 1
function_code_name = "read_coils"

def __init__(self, address=None, count=None, unit=Defaults.Slave, **kwargs):
def __init__(self, address=None, count=None, slave=Defaults.Slave, **kwargs):
"""Initialize a new instance.
:param address: The address to start reading from
:param count: The number of bits to read
:param unit: Modbus slave unit ID
:param slave: Modbus slave slave ID
"""
ReadBitsRequestBase.__init__(self, address, count, unit, **kwargs)
ReadBitsRequestBase.__init__(self, address, count, slave, **kwargs)

def execute(self, context):
"""Run a read coils request against a datastore.
Expand Down Expand Up @@ -183,13 +183,13 @@ class ReadCoilsResponse(ReadBitsResponseBase):

function_code = 1

def __init__(self, values=None, unit=Defaults.Slave, **kwargs):
def __init__(self, values=None, slave=Defaults.Slave, **kwargs):
"""Initialize a new instance.
:param values: The request values to respond with
:param unit: Modbus slave unit ID
:param slave: Modbus slave slave ID
"""
ReadBitsResponseBase.__init__(self, values, unit, **kwargs)
ReadBitsResponseBase.__init__(self, values, slave, **kwargs)


class ReadDiscreteInputsRequest(ReadBitsRequestBase):
Expand All @@ -204,14 +204,14 @@ class ReadDiscreteInputsRequest(ReadBitsRequestBase):
function_code = 2
function_code_name = "read_discrete_input"

def __init__(self, address=None, count=None, unit=Defaults.Slave, **kwargs):
def __init__(self, address=None, count=None, slave=Defaults.Slave, **kwargs):
"""Initialize a new instance.
:param address: The address to start reading from
:param count: The number of bits to read
:param unit: Modbus slave unit ID
:param slave: Modbus slave slave ID
"""
ReadBitsRequestBase.__init__(self, address, count, unit, **kwargs)
ReadBitsRequestBase.__init__(self, address, count, slave, **kwargs)

def execute(self, context):
"""Run a read discrete input request against a datastore.
Expand Down Expand Up @@ -249,13 +249,13 @@ class ReadDiscreteInputsResponse(ReadBitsResponseBase):

function_code = 2

def __init__(self, values=None, unit=Defaults.Slave, **kwargs):
def __init__(self, values=None, slave=Defaults.Slave, **kwargs):
"""Initialize a new instance.
:param values: The request values to respond with
:param unit: Modbus slave unit ID
:param slave: Modbus slave slave ID
"""
ReadBitsResponseBase.__init__(self, values, unit, **kwargs)
ReadBitsResponseBase.__init__(self, values, slave, **kwargs)


# ---------------------------------------------------------------------------#
Expand Down
8 changes: 4 additions & 4 deletions pymodbus/bit_write_message.py
Expand Up @@ -41,13 +41,13 @@ class WriteSingleCoilRequest(ModbusRequest):

_rtu_frame_size = 8

def __init__(self, address=None, value=None, unit=None, **kwargs):
def __init__(self, address=None, value=None, slave=None, **kwargs):
"""Initialize a new instance.
:param address: The variable address to write
:param value: The value to write at address
"""
ModbusRequest.__init__(self, unit=unit, **kwargs)
ModbusRequest.__init__(self, slave=slave, **kwargs)
self.address = address
self.value = bool(value)

Expand Down Expand Up @@ -165,13 +165,13 @@ class WriteMultipleCoilsRequest(ModbusRequest):
function_code_name = "write_coils"
_rtu_byte_count_pos = 6

def __init__(self, address=None, values=None, unit=None, **kwargs):
def __init__(self, address=None, values=None, slave=None, **kwargs):
"""Initialize a new instance.
:param address: The starting request address
:param values: The values to write
"""
ModbusRequest.__init__(self, unit=unit, **kwargs)
ModbusRequest.__init__(self, slave=slave, **kwargs)
self.address = address
if not values:
values = []
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/client/mixin.py
Expand Up @@ -473,7 +473,7 @@ def readwrite_registers(
read_count=read_count,
write_address=write_address,
values=values,
unit=slave,
slave=slave,
**kwargs,
)
)
Expand Down
8 changes: 4 additions & 4 deletions pymodbus/client/sync_diag.py
Expand Up @@ -34,13 +34,13 @@ class ModbusTcpDiagClient(ModbusTcpClient):
+---------+-----------------------------------------------------------------+
| Level | Events |
+=========+=================================================================+
| ERROR | Failure to connect to modbus unit; unexpected disconnect by |
| | modbus unit |
| ERROR | Failure to connect to modbus slave; unexpected disconnect by |
| | modbus slave |
+---------+-----------------------------------------------------------------+
| WARNING | Timeout on normal read; read took longer than warn_delay_limit |
+---------+-----------------------------------------------------------------+
| INFO | Connection attempt to modbus unit; disconnection from modbus |
| | unit; each time limited read |
| INFO | Connection attempt to modbus slave; disconnection from modbus |
| | slave; each time limited read |
+---------+-----------------------------------------------------------------+
| DEBUG | Normal read with timing information |
+---------+-----------------------------------------------------------------+
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/client/tcp.py
Expand Up @@ -340,7 +340,7 @@ def _handle_abrupt_socket_close(
result = b"".join(data)
Log.warning(" after returning {} bytes: {} ", len(result), result)
return result
msg += " without response from unit before it closed connection"
msg += " without response from slave before it closed connection"
raise ConnectionException(msg)

def is_socket_open(self):
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/datastore/context.py
Expand Up @@ -120,7 +120,7 @@ class ModbusServerContext:
"""This represents a master collection of slave contexts.
If single is set to true, it will be treated as a single
context so every unit-id returns the same context. If single
context so every slave_id returns the same context. If single
is set to false, it will be interpreted as a collection of
slave contexts.
"""
Expand Down

0 comments on commit 4499978

Please sign in to comment.