12
12
else : # Python 2
13
13
from mock import Mock
14
14
15
+
15
16
@pytest .fixture
16
17
def rtu_framer ():
17
18
return ModbusRtuFramer (ClientDecoder ())
18
19
20
+
19
21
@pytest .fixture
20
22
def ascii_framer ():
21
23
return ModbusAsciiFramer (ClientDecoder ())
22
24
25
+
23
26
@pytest .fixture
24
27
def binary_framer ():
25
28
return ModbusBinaryFramer (ClientDecoder ())
26
29
30
+
27
31
@pytest .mark .parametrize ("framer" , [ModbusRtuFramer ,
28
32
ModbusAsciiFramer ,
29
33
ModbusBinaryFramer ,
@@ -116,9 +120,12 @@ def test_populate_result(rtu_framer):
116
120
assert result .unit_id == 255
117
121
118
122
119
- def test_process_incoming_packet (rtu_framer ):
123
+ @pytest .mark .parametrize ('framer' , [ascii_framer , rtu_framer , binary_framer ])
124
+ def test_process_incoming_packet (framer ):
120
125
def cb (res ):
121
126
return res
127
+ # data = b''
128
+ # framer.processIncomingPacket(data, cb, unit=1, single=False)
122
129
123
130
124
131
def test_build_packet (rtu_framer ):
@@ -160,4 +167,22 @@ def cb(res):
160
167
161
168
def test_get_raw_frame (rtu_framer ):
162
169
rtu_framer ._buffer = b'\x00 \x01 \x00 \x01 \x00 \n \xec \x1c '
163
- assert rtu_framer .getRawFrame () == rtu_framer ._buffer
170
+ assert rtu_framer .getRawFrame () == rtu_framer ._buffer
171
+
172
+
173
+ def test_validate_unit_id (rtu_framer ):
174
+ rtu_framer .populateHeader ( b'\x00 \x01 \x00 \x01 \x00 \n \xec \x1c ' )
175
+ assert rtu_framer ._validate_unit_id ([0 ], False )
176
+ assert rtu_framer ._validate_unit_id ([1 ], True )
177
+
178
+
179
+ @pytest .mark .parametrize ('data' , [b':010100010001FC\r \n ' ,
180
+ b'' ])
181
+ def test_decode_ascii_data (ascii_framer , data ):
182
+ data = ascii_framer .decode_data (data )
183
+ assert isinstance (data , dict )
184
+ if data :
185
+ assert data .get ("unit" ) == 1
186
+ assert data .get ("fcode" ) == 1
187
+ else :
188
+ assert not data
0 commit comments