Skip to content

Commit 2bc8b0d

Browse files
authored
Please black (test). (#899)
1 parent 390cb5d commit 2bc8b0d

31 files changed

+1516
-749
lines changed

test/asyncio_test_helper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
"""Asyncio helper."""
32
import functools
43

test/test_all_messages.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ class ModbusAllMessagesTests(unittest.TestCase):
4444
def setUp(self):
4545
"""Initialize the test environment and builds request/result encoding pairs."""
4646
arguments = {
47-
"read_address": 1, "read_count": 1,
48-
"write_address": 1, "write_registers": 1
47+
"read_address": 1,
48+
"read_count": 1,
49+
"write_address": 1,
50+
"write_registers": 1,
4951
}
5052
self.requests = [
5153
lambda unit: ReadCoilsRequest(1, 5, unit=unit),

test/test_bit_read_messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pymodbus.pdu import ModbusExceptions
1616

1717
from .modbus_mocks import MockContext
18+
1819
res = [True] * 21
1920
res.extend([False] * 3)
2021
# ---------------------------------------------------------------------------#
@@ -77,7 +78,7 @@ def test_bit_read_base_requests(self):
7778
"""Test bit read request encoding"""
7879
messages = {
7980
ReadBitsRequestBase(12, 14): b"\x00\x0c\x00\x0e",
80-
ReadBitsResponseBase([1, 0, 1, 1, 0]): b"\x01\x0d"
81+
ReadBitsResponseBase([1, 0, 1, 1, 0]): b"\x01\x0d",
8182
}
8283
for request, expected in iter(messages.items()):
8384
self.assertEqual(request.encode(), expected)
@@ -91,8 +92,7 @@ def test_bit_read_message_execute_value_errors(self):
9192
]
9293
for request in requests:
9394
result = request.execute(context)
94-
self.assertEqual(ModbusExceptions.IllegalValue,
95-
result.exception_code)
95+
self.assertEqual(ModbusExceptions.IllegalValue, result.exception_code)
9696

9797
def test_bit_read_message_execute_address_errors(self):
9898
"""Test bit read request encoding"""

test/test_bit_write_messages.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def tearDown(self):
3939
def test_bit_write_base_requests(self):
4040
"""Test bit write base."""
4141
messages = {
42-
WriteSingleCoilRequest(1, 0xabcd): b"\x00\x01\xff\x00",
43-
WriteSingleCoilResponse(1, 0xabcd): b"\x00\x01\xff\x00",
42+
WriteSingleCoilRequest(1, 0xABCD): b"\x00\x01\xff\x00",
43+
WriteSingleCoilResponse(1, 0xABCD): b"\x00\x01\xff\x00",
4444
WriteMultipleCoilsRequest(1, [True] * 5): b"\x00\x01\x00\x05\x01\x1f",
4545
WriteMultipleCoilsResponse(1, 5): b"\x00\x01\x00\x05",
4646
}
@@ -49,9 +49,7 @@ def test_bit_write_base_requests(self):
4949

5050
def test_bit_write_message_get_response_pdu(self):
5151
"""Test bit write message."""
52-
requests = {
53-
WriteSingleCoilRequest(1, 0xabcd): 5
54-
}
52+
requests = {WriteSingleCoilRequest(1, 0xABCD): 5}
5553
for request, expected in iter(requests.items()):
5654
pdu_len = request.get_response_pdu_size()
5755
self.assertEqual(pdu_len, expected)
@@ -126,8 +124,8 @@ def test_write_multiple_coils_response(self):
126124
def test_serializing_to_string(self):
127125
"""Test serializing to string."""
128126
requests = [
129-
WriteSingleCoilRequest(1, 0xabcd),
130-
WriteSingleCoilResponse(1, 0xabcd),
127+
WriteSingleCoilRequest(1, 0xABCD),
128+
WriteSingleCoilResponse(1, 0xABCD),
131129
WriteMultipleCoilsRequest(1, [True] * 5),
132130
WriteMultipleCoilsResponse(1, 5),
133131
]

test/test_client_async.py

Lines changed: 109 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717
from pymodbus.client.asynchronous.tls import AsyncModbusTLSClient
1818
from pymodbus.client.asynchronous.udp import AsyncModbusUDPClient
1919
from pymodbus.client.asynchronous.tornado import (
20-
AsyncModbusSerialClient as AsyncTornadoModbusSerialClient
20+
AsyncModbusSerialClient as AsyncTornadoModbusSerialClient,
21+
)
22+
from pymodbus.client.asynchronous.tornado import (
23+
AsyncModbusTCPClient as AsyncTornadoModbusTcpClient,
24+
)
25+
from pymodbus.client.asynchronous.tornado import (
26+
AsyncModbusUDPClient as AsyncTornadoModbusUdoClient,
2127
)
22-
from pymodbus.client.asynchronous.tornado import AsyncModbusTCPClient as AsyncTornadoModbusTcpClient
23-
from pymodbus.client.asynchronous.tornado import AsyncModbusUDPClient as AsyncTornadoModbusUdoClient
2428
from pymodbus.client.asynchronous import schedulers
2529
from pymodbus.factory import ClientDecoder
2630
from pymodbus.exceptions import ConnectionException
@@ -58,20 +62,26 @@ class TestAsynchronousClient:
5862
def test_tcp_twisted_client(self): # pylint: disable=no-self-use
5963
"""Test the TCP Twisted client."""
6064
with patch("twisted.internet.reactor"):
65+
6166
def test_callback(client): # pylint: disable=unused-argument
6267
pass
6368

64-
AsyncModbusTCPClient(schedulers.REACTOR,
65-
framer=ModbusSocketFramer(ClientDecoder()),
66-
callback=test_callback,
67-
errback=test_callback)
69+
AsyncModbusTCPClient(
70+
schedulers.REACTOR,
71+
framer=ModbusSocketFramer(ClientDecoder()),
72+
callback=test_callback,
73+
errback=test_callback,
74+
)
6875

6976
@patch("pymodbus.client.asynchronous.tornado.IOLoop")
7077
@patch("pymodbus.client.asynchronous.tornado.IOStream")
71-
def test_tcp_tornado_client(self, mock_iostream, mock_ioloop): # pylint: disable=no-self-use,unused-argument
78+
def test_tcp_tornado_client(
79+
self, mock_iostream, mock_ioloop
80+
): # pylint: disable=no-self-use,unused-argument
7281
"""Test the TCP tornado client client initialize"""
73-
protocol, future = AsyncModbusTCPClient( # NOSOANR pylint: disable=unpacking-non-sequence
74-
schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder()))
82+
protocol, future = AsyncModbusTCPClient( # NOSONAR pylint: disable=unpacking-non-sequence
83+
schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder())
84+
)
7585
client = future.result()
7686
assert isinstance(client, AsyncTornadoModbusTcpClient) # nosec
7787
assert not list(client.transaction) # nosec
@@ -94,7 +104,9 @@ def handle_failure(failure):
94104

95105
@patch("asyncio.get_event_loop")
96106
@patch("asyncio.gather")
97-
def test_tcp_asyncio_client(self, mock_gather, mock_loop): # pylint: disable=no-self-use,unused-argument
107+
def test_tcp_asyncio_client(
108+
self, mock_gather, mock_loop
109+
): # pylint: disable=no-self-use,unused-argument
98110
"""Test the TCP Twisted client."""
99111
pytest.skip("TBD")
100112

@@ -104,7 +116,9 @@ def test_tcp_asyncio_client(self, mock_gather, mock_loop): # pylint: disable=no
104116

105117
def test_tls_asyncio_client(self): # pylint: disable=no-self-use
106118
"""Test the TLS AsyncIO client."""
107-
_, client = AsyncModbusTLSClient(schedulers.ASYNC_IO) # NOSONAR pylint: disable=unpacking-non-sequence
119+
_, client = AsyncModbusTLSClient( # NOSONAR pylint: disable=unpacking-non-sequence
120+
schedulers.ASYNC_IO
121+
)
108122
assert isinstance(client, ReconnectingAsyncioModbusTlsClient) # nosec
109123
assert isinstance(client.framer, ModbusTlsFramer) # nosec
110124
assert isinstance(client.sslctx, ssl.SSLContext) # nosec
@@ -119,10 +133,13 @@ def test_tls_asyncio_client(self): # pylint: disable=no-self-use
119133

120134
@patch("pymodbus.client.asynchronous.tornado.IOLoop")
121135
@patch("pymodbus.client.asynchronous.tornado.IOStream")
122-
def test_udp_tornado_client(self, mock_iostream, mock_ioloop): # pylint: disable=no-self-use,unused-argument
136+
def test_udp_tornado_client(
137+
self, mock_iostream, mock_ioloop
138+
): # pylint: disable=no-self-use,unused-argument
123139
"""Test the udp tornado client client initialize"""
124140
protocol, future = AsyncModbusUDPClient( # NOSONAR pylint: disable=unpacking-non-sequence
125-
schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder()))
141+
schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder())
142+
)
126143
client = future.result()
127144
assert isinstance(client, AsyncTornadoModbusUdoClient) # nosec
128145
assert not list(client.transaction) # nosec
@@ -144,12 +161,15 @@ def handle_failure(failure):
144161
def test_udp_twisted_client(self): # pylint: disable=no-self-use
145162
"""Test the udp twisted client client initialize"""
146163
with pytest.raises(NotImplementedError):
147-
AsyncModbusUDPClient(schedulers.REACTOR,
148-
framer=ModbusSocketFramer(ClientDecoder()))
164+
AsyncModbusUDPClient(
165+
schedulers.REACTOR, framer=ModbusSocketFramer(ClientDecoder())
166+
)
149167

150168
@patch("asyncio.get_event_loop")
151169
@patch("asyncio.gather", side_effect=mock_asyncio_gather)
152-
def test_udp_asyncio_client(self, mock_gather, mock_event_loop): # pylint: disable=no-self-use,unused-argument
170+
def test_udp_asyncio_client(
171+
self, mock_gather, mock_event_loop
172+
): # pylint: disable=no-self-use,unused-argument
153173
"""Test the udp asyncio client"""
154174
pytest.skip("TBD")
155175

@@ -161,51 +181,78 @@ def test_udp_asyncio_client(self, mock_gather, mock_event_loop): # pylint: disa
161181
sys.platform == "win32" and platform.python_implementation() == "PyPy",
162182
reason="Twisted serial requires pywin32 which is not compatible with PyPy",
163183
)
164-
@pytest.mark.parametrize("method, framer", [("rtu", ModbusRtuFramer),
165-
("socket", ModbusSocketFramer),
166-
("binary", ModbusBinaryFramer),
167-
("ascii", ModbusAsciiFramer)])
184+
@pytest.mark.parametrize(
185+
"method, framer",
186+
[
187+
("rtu", ModbusRtuFramer),
188+
("socket", ModbusSocketFramer),
189+
("binary", ModbusBinaryFramer),
190+
("ascii", ModbusAsciiFramer),
191+
],
192+
)
168193
def test_serial_twisted_client(self, method, framer): # pylint: disable=no-self-use
169194
"""Test the serial twisted client client initialize"""
170195
with patch("serial.Serial"):
171-
from twisted.internet.serialport import SerialPort # pylint: disable=import-outside-toplevel
172-
with maybe_manage(sys.platform == "win32", patch.object(
173-
SerialPort, "_finishPortSetup")):
196+
from twisted.internet.serialport import ( # pylint: disable=import-outside-toplevel
197+
SerialPort,
198+
)
199+
200+
with maybe_manage(
201+
sys.platform == "win32", patch.object(SerialPort, "_finishPortSetup")
202+
):
174203
with patch("twisted.internet.reactor"):
175204

176205
protocol, client = AsyncModbusSerialClient( # NOSONAR pylint: disable=unpacking-non-sequence
177206
schedulers.REACTOR,
178207
method=method,
179208
port=pytest.SERIAL_PORT,
180-
proto_cls=ModbusSerClientProtocol
209+
proto_cls=ModbusSerClientProtocol,
181210
)
182211

183212
assert isinstance(client, SerialPort) # nosec
184213
assert isinstance(client.protocol, ModbusSerClientProtocol) # nosec
185214
assert not list(client.protocol.transaction) # nosec
186215
assert isinstance(client.protocol.framer, framer) # nosec
187-
assert client.protocol._connected # nosec pylint: disable=protected-access
216+
assert (
217+
client.protocol._connected # nosec pylint: disable=protected-access
218+
)
188219

189220
def handle_failure(failure):
190-
assert (isinstance(failure.exception(), ConnectionException)) # nosec
221+
assert isinstance(
222+
failure.exception(), ConnectionException # nosec
223+
)
191224

192-
response = client.protocol._buildResponse(0x00) # pylint: disable=protected-access
225+
response = client.protocol._buildResponse( # pylint: disable=protected-access
226+
0x00
227+
)
193228
response.addCallback(handle_failure)
194229

195-
assert client.protocol._connected # nosec pylint: disable=protected-access
230+
assert (
231+
client.protocol._connected # nosec pylint: disable=protected-access
232+
)
196233
client.protocol.close()
197234
protocol.stop()
198-
assert not client.protocol._connected # nosec pylint: disable=protected-access
235+
assert (
236+
not client.protocol._connected # nosec pylint: disable=protected-access
237+
)
199238

200-
@pytest.mark.parametrize("method, framer", [("rtu", ModbusRtuFramer),
201-
("socket", ModbusSocketFramer),
202-
("binary", ModbusBinaryFramer),
203-
("ascii", ModbusAsciiFramer)])
239+
@pytest.mark.parametrize(
240+
"method, framer",
241+
[
242+
("rtu", ModbusRtuFramer),
243+
("socket", ModbusSocketFramer),
244+
("binary", ModbusBinaryFramer),
245+
("ascii", ModbusAsciiFramer),
246+
],
247+
)
204248
def test_serial_tornado_client(self, method, framer): # pylint: disable=no-self-use
205249
"""Test the serial tornado client client initialize"""
206-
with maybe_manage(sys.platform in {"darwin", "win32"}, patch.object(Serial, "open")):
250+
with maybe_manage(
251+
sys.platform in {"darwin", "win32"}, patch.object(Serial, "open")
252+
):
207253
protocol, future = AsyncModbusSerialClient( # NOSONAR pylint: disable=unpacking-non-sequence
208-
schedulers.IO_LOOP, method=method, port=pytest.SERIAL_PORT)
254+
schedulers.IO_LOOP, method=method, port=pytest.SERIAL_PORT
255+
)
209256
client = future.result()
210257
assert isinstance(client, AsyncTornadoModbusSerialClient) # nosec
211258
assert not list(client.transaction) # nosec
@@ -226,18 +273,35 @@ def handle_failure(failure):
226273

227274
@patch("asyncio.get_event_loop")
228275
@patch("asyncio.gather", side_effect=mock_asyncio_gather)
229-
@pytest.mark.parametrize("method, framer", [("rtu", ModbusRtuFramer),
230-
("socket", ModbusSocketFramer),
231-
("binary", ModbusBinaryFramer),
232-
("ascii", ModbusAsciiFramer)])
233-
def test_serial_asyncio_client(self, mock_gather, # pylint: disable=no-self-use,unused-argument
234-
mock_event_loop, method, framer): # pylint: disable=unused-argument
276+
@pytest.mark.parametrize(
277+
"method, framer",
278+
[
279+
("rtu", ModbusRtuFramer),
280+
("socket", ModbusSocketFramer),
281+
("binary", ModbusBinaryFramer),
282+
("ascii", ModbusAsciiFramer),
283+
],
284+
)
285+
def test_serial_asyncio_client( # pylint: disable=no-self-use
286+
self,
287+
mock_gather, # pylint: disable=unused-argument
288+
mock_event_loop,
289+
method,
290+
framer,
291+
): # pylint: disable=unused-argument
235292
"""Test that AsyncModbusSerialClient instantiates AsyncioModbusSerialClient for asyncio scheduler."""
236293
loop = asyncio.get_event_loop()
237294
loop.is_running.side_effect = lambda: False
238295
loop, client = AsyncModbusSerialClient( # NOSONAR pylint: disable=unpacking-non-sequence
239-
schedulers.ASYNC_IO, method=method, port=pytest.SERIAL_PORT, loop=loop,
240-
baudrate=19200, parity="E", stopbits=2, bytesize=7)
296+
schedulers.ASYNC_IO,
297+
method=method,
298+
port=pytest.SERIAL_PORT,
299+
loop=loop,
300+
baudrate=19200,
301+
parity="E",
302+
stopbits=2,
303+
bytesize=7,
304+
)
241305
assert isinstance(client, AsyncioModbusSerialClient) # nosec
242306
assert isinstance(client.framer, framer) # nosec
243307
assert client.port == pytest.SERIAL_PORT # nosec
@@ -248,6 +312,7 @@ def test_serial_asyncio_client(self, mock_gather, # pylint: disable=no-self-use
248312
client.stop()
249313
loop.stop()
250314

315+
251316
# ---------------------------------------------------------------------------#
252317
# Main
253318
# ---------------------------------------------------------------------------#

0 commit comments

Comments
 (0)