Skip to content

Commit

Permalink
✅ adjust tests to work with new modbus_interface arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
M4GNV5 committed Dec 25, 2023
1 parent b42f679 commit 18536f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
8 changes: 4 additions & 4 deletions tests/test_modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def perform_variant_test(self, mock_modbus, variant, expected_framer):
mock_modbus().read_input_registers.side_effect = self.read_input_registers
mock_modbus().read_holding_registers.side_effect = self.read_holding_registers

m = modbus_interface.modbus_interface('1.1.1.1', 111, 2, variant)
m = modbus_interface.modbus_interface(ip='1.1.1.1', port=111, variant=variant)
m.connect()
mock_modbus.assert_called_with('1.1.1.1', 111, RetryOnEmpty=True, framer=expected_framer, retries=1, timeout=1)

Expand All @@ -64,10 +64,10 @@ def test_connection_variants(self):
self.perform_variant_test(mock_modbus, 'udp', modbus_interface.ModbusSocketFramer)
self.perform_variant_test(mock_modbus, 'binary-over-udp', modbus_interface.ModbusBinaryFramer)

m = modbus_interface.modbus_interface('1.1.1.1', 111, 2, 'notexisiting')
m = modbus_interface.modbus_interface(ip='1.1.1.1', port=111, variant='notexisiting')
self.assertRaises(ValueError, m.connect)

m = modbus_interface.modbus_interface('1.1.1.1', 111, 2, 'notexisiting-over-tcp')
m = modbus_interface.modbus_interface(ip='1.1.1.1', port=111, variant='notexisiting-over-tcp')
self.assertRaises(ValueError, m.connect)

def test_connect(self):
Expand All @@ -76,7 +76,7 @@ def test_connect(self):
mock_modbus().read_input_registers.side_effect = self.read_input_registers
mock_modbus().read_holding_registers.side_effect = self.read_holding_registers

m = modbus_interface.modbus_interface('1.1.1.1', 111, 2)
m = modbus_interface.modbus_interface(ip='1.1.1.1', port=111)
m.connect()
mock_modbus.assert_called_with('1.1.1.1', 111, RetryOnEmpty=True, framer=modbus_interface.ModbusSocketFramer, retries=1, timeout=1)

Expand Down
18 changes: 15 additions & 3 deletions tests/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,18 @@ def test_register_validation(self):
if not fail:
self.fail("Didn't throw an exception checking an invalid register configuration")

def assert_modbus_call(self, mock_modbus, word_order=modbus4mqtt.modbus_interface.WordOrder.HighLow):
mock_modbus.assert_any_call(
ip='192.168.1.90',
port=502,
update_rate_s=5,
device_address=1,
write_mode=modbus4mqtt.modbus_interface.WriteMode.Single,
variant=None,
scan_batching=None,
word_order=word_order
)

def test_word_order_setting(self):
with patch('paho.mqtt.client.Client') as mock_mqtt:
with patch('modbus4mqtt.modbus_interface.modbus_interface') as mock_modbus:
Expand All @@ -500,7 +512,7 @@ def test_word_order_setting(self):
# Default value
m = modbus4mqtt.mqtt_interface('kroopit', 1885, 'brengis', 'pranto', './tests/test_type.yaml', MQTT_TOPIC_PREFIX)
m.connect()
mock_modbus.assert_any_call('192.168.1.90', 502, 5, scan_batching=None, variant=None, word_order=modbus4mqtt.modbus_interface.WordOrder.HighLow)
self.assert_modbus_call(mock_modbus)

with patch('paho.mqtt.client.Client') as mock_mqtt:
with patch('modbus4mqtt.modbus_interface.modbus_interface') as mock_modbus:
Expand All @@ -511,7 +523,7 @@ def test_word_order_setting(self):
# Explicit HighLow
m = modbus4mqtt.mqtt_interface('kroopit', 1885, 'brengis', 'pranto', './tests/test_word_order.yaml', MQTT_TOPIC_PREFIX)
m.connect()
mock_modbus.assert_any_call('192.168.1.90', 502, 5, scan_batching=None, variant=None, word_order=modbus4mqtt.modbus_interface.WordOrder.HighLow)
self.assert_modbus_call(mock_modbus)

with patch('paho.mqtt.client.Client') as mock_mqtt:
with patch('modbus4mqtt.modbus_interface.modbus_interface') as mock_modbus:
Expand All @@ -522,7 +534,7 @@ def test_word_order_setting(self):
# Explicit HighLow
m = modbus4mqtt.mqtt_interface('kroopit', 1885, 'brengis', 'pranto', './tests/test_word_order_low_high.yaml', MQTT_TOPIC_PREFIX)
m.connect()
mock_modbus.assert_any_call('192.168.1.90', 502, 5, scan_batching=None, variant=None, word_order=modbus4mqtt.modbus_interface.WordOrder.LowHigh)
self.assert_modbus_call(mock_modbus, modbus4mqtt.modbus_interface.WordOrder.LowHigh)


if __name__ == "__main__":
Expand Down

0 comments on commit 18536f3

Please sign in to comment.