|
@@ -33,7 +33,7 @@ def set_test_db():
|
33
|
33
|
|
34
|
34
|
yield
|
35
|
35
|
|
36
|
|
- os.unlink(tmpdb)
|
|
36
|
+ #os.unlink(tmpdb)
|
37
|
37
|
|
38
|
38
|
@pytest.fixture(scope='module')
|
39
|
39
|
def serial_conn():
|
|
@@ -44,8 +44,8 @@ def serial_conn():
|
44
|
44
|
|
45
|
45
|
@patch('umodbus.client.serial.rtu.send_message')
|
46
|
46
|
@patch('umodbus.client.serial.rtu.read_coils')
|
47
|
|
-def test_rtu_call_mocked(RtuReadCoils, RtuSendMessage, serial_conn):
|
48
|
|
- modbus.read_holding_registers(1, 3)
|
|
47
|
+def test_rtu__coils_call_mocked(RtuReadCoils, RtuSendMessage, serial_conn):
|
|
48
|
+ modbus.read_coils(1, 3)
|
49
|
49
|
RtuReadCoils.assert_any_call(slave_id=1, starting_address=1, quantity=2)
|
50
|
50
|
RtuSendMessage.assert_called()
|
51
|
51
|
|
|
@@ -53,6 +53,18 @@ def test_rtu_call_mocked(RtuReadCoils, RtuSendMessage, serial_conn):
|
53
|
53
|
RtuReadCoils.assert_any_call(slave_id=1, starting_address=1, quantity=2)
|
54
|
54
|
RtuSendMessage.assert_called()
|
55
|
55
|
|
|
56
|
+
|
|
57
|
+@patch('umodbus.client.serial.rtu.send_message')
|
|
58
|
+@patch('umodbus.client.serial.rtu.read_holding_registers')
|
|
59
|
+def test_rtu_registers_call_mocked(RtuReadHoldingRegisters, RtuSendMessage, serial_conn):
|
|
60
|
+ modbus.read_holding_registers(1, 3)
|
|
61
|
+ RtuReadHoldingRegisters.assert_any_call(slave_id=1, starting_address=1, quantity=2)
|
|
62
|
+ RtuSendMessage.assert_called()
|
|
63
|
+
|
|
64
|
+ modbus.read_holding_registers(1, 500)
|
|
65
|
+ RtuReadHoldingRegisters.assert_any_call(slave_id=1, starting_address=1, quantity=125)
|
|
66
|
+ RtuSendMessage.assert_called()
|
|
67
|
+
|
56
|
68
|
def test_rtu_call_holding_registers(serial_conn):
|
57
|
69
|
r = modbus.read_holding_registers(1, 1)
|
58
|
70
|
assert type(r) == list
|
|
@@ -70,25 +82,45 @@ def test_rtu_call_holding_registers(serial_conn):
|
70
|
82
|
assert type(r) == list
|
71
|
83
|
assert len(r) == 150
|
72
|
84
|
|
|
85
|
+@pytest.fixture
|
|
86
|
+def var_types():
|
|
87
|
+ return VariableType.getall()
|
73
|
88
|
|
74
|
|
-
|
75
|
|
-
|
76
|
|
-def test_get_var(set_test_db, serial_conn):
|
77
|
|
- d_var_types = VariableType.getall()
|
78
|
|
-
|
79
|
|
- analog = d_var_types['Analog']
|
|
89
|
+def test_get_analog(set_test_db, serial_conn, var_types):
|
|
90
|
+ analog = var_types['Analog']
|
|
91
|
+ print(analog.__dict__)
|
80
|
92
|
res = modbus.read_holding_registers(analog.start_address, analog.end_address)
|
81
|
93
|
assert type(res) == list
|
82
|
94
|
assert len(res) == analog.end_address - analog.start_address
|
83
|
95
|
for r in res:
|
84
|
96
|
assert type(r) == int
|
85
|
97
|
|
86
|
|
- integer = d_var_types['Integer']
|
|
98
|
+def test_get_integer(set_test_db, serial_conn, var_types):
|
|
99
|
+ integer = var_types['Integer']
|
|
100
|
+ print(integer.__dict__)
|
87
|
101
|
try:
|
88
|
102
|
res = modbus.read_holding_registers(integer.start_address, integer.end_address)
|
89
|
|
- except umodbus.exceptions.IllegalDataAddressError:
|
|
103
|
+ assert type(res) == list
|
|
104
|
+ assert len(res) == integer.end_address - integer.start_address
|
|
105
|
+ for r in res:
|
|
106
|
+ assert type(r) == int
|
|
107
|
+
|
|
108
|
+ except umodbus.exceptions.IllegalDataAddressError as e:
|
|
109
|
+ print(e)
|
90
|
110
|
print(f'The start and end addresses are not available [{integer.start_address}, {integer.end_address}]')
|
|
111
|
+ assert False
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+def test_get_digital(set_test_db, serial_conn, var_types):
|
|
115
|
+ digital = var_types['Digital']
|
|
116
|
+ print(digital.__dict__)
|
|
117
|
+ try:
|
|
118
|
+ res = modbus.read_coils(digital.start_address, digital.end_address)
|
|
119
|
+ except umodbus.exceptions.IllegalDataAddressError:
|
|
120
|
+ print(f'The start and end addresses are not available [{digital.start_address}, {digital.end_address}]')
|
91
|
121
|
assert type(res) == list
|
92
|
|
- assert len(res) == integer.end_address - integer.start_address
|
|
122
|
+ assert len(res) == digital.end_address - digital.start_address
|
93
|
123
|
for r in res:
|
94
|
124
|
assert type(r) == int
|
|
125
|
+ assert r in [0, 1]
|
|
126
|
+
|