From 18536f37028f0296b19235a9689613b87a27a45b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20L=C3=B6w?= Date: Mon, 25 Dec 2023 18:48:42 +0100 Subject: [PATCH] :white_check_mark: adjust tests to work with new modbus_interface arguments --- tests/test_modbus.py | 8 ++++---- tests/test_mqtt.py | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tests/test_modbus.py b/tests/test_modbus.py index f9a0c1e..685d69e 100644 --- a/tests/test_modbus.py +++ b/tests/test_modbus.py @@ -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) @@ -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): @@ -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) diff --git a/tests/test_mqtt.py b/tests/test_mqtt.py index e19d4e1..2fa5dc1 100644 --- a/tests/test_mqtt.py +++ b/tests/test_mqtt.py @@ -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: @@ -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: @@ -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: @@ -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__":