Skip to content

Commit

Permalink
test: Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinying committed Oct 13, 2024
1 parent 7195e29 commit 30813b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
33 changes: 12 additions & 21 deletions tests/test_modbus_task.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import pytest
import threading
from modpoll.arg_parser import get_parser
from modpoll.modbus_task import ModbusMaster
from modpoll.modbus_task import setup_modbus_handlers


def test_modbus_task_modbus_setup():
modbus_masters = []
parser = get_parser()
args = parser.parse_args(
[
Expand All @@ -16,14 +14,8 @@ def test_modbus_task_modbus_setup():
"modsim.topmaker.net",
]
)
event_exit = threading.Event()
for config_file in args.config:
modbus_master = ModbusMaster(args, event_exit, config_file)
if modbus_master.setup():
modbus_masters.append(modbus_master)
else:
modbus_master.close()
assert len(modbus_masters) == 2
modbus_handlers = setup_modbus_handlers(args)
assert len(modbus_handlers) == 2


@pytest.mark.integration
Expand All @@ -37,22 +29,21 @@ def test_modbus_task_poll_modsim():
"modsim.topmaker.net",
]
)
event_exit = threading.Event()
modbus_master = ModbusMaster(args, event_exit, args.config[0])
modbus_handlers = setup_modbus_handlers(args)
modbus_handler = modbus_handlers[0]

assert modbus_master.setup()
assert modbus_master.connect()
assert modbus_handler.connect()

modbus_master.poll()
modbus_handler.poll()

# Check if any data was collected
assert len(modbus_master.deviceList) > 0
assert len(modbus_master.deviceList[0].references) > 0
assert len(modbus_handler.deviceList) > 0
assert len(modbus_handler.deviceList[0].references) > 0

# Check if at least one reference has a non-None value
assert any(
ref.val is not None for ref in modbus_master.deviceList[0].references.values()
ref.val is not None for ref in modbus_handler.deviceList[0].references.values()
)

modbus_master.disconnect()
modbus_master.close()
modbus_handler.disconnect()
modbus_handler.close()
22 changes: 11 additions & 11 deletions tests/test_mqtt_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def test_mqtt_task_setup():
mqtt_task = MqttHandler(
mqtt_handler = MqttHandler(
name="test_mqtt",
host="mqtt.eclipseprojects.io",
port=1883,
Expand All @@ -15,20 +15,20 @@ def test_mqtt_task_setup():
subscribe_topics=["test/topic"],
)

assert mqtt_task.mqttc_setup()
assert mqtt_handler.setup()

# Test MQTT client properties
assert mqtt_task.mqttc is not None
assert mqtt_task.host == "mqtt.eclipseprojects.io"
assert mqtt_task.port == 1883
assert mqtt_handler.mqtt_client is not None
assert mqtt_handler.host == "mqtt.eclipseprojects.io"
assert mqtt_handler.port == 1883

# Clean up
mqtt_task.mqttc_close()
mqtt_handler.close()


@pytest.mark.integration
def test_mqtt_task_connect():
mqtt_task = MqttHandler(
mqtt_handler = MqttHandler(
name="test_mqtt",
host="mqtt.eclipseprojects.io",
port=1883,
Expand All @@ -38,14 +38,14 @@ def test_mqtt_task_connect():
qos=0,
)

assert mqtt_task.mqttc_setup()
assert mqtt_task.mqttc_connect()
assert mqtt_handler.setup()
assert mqtt_handler.connect()

# Add a short delay to allow the connection to establish
time.sleep(1)

# Test connection
assert mqtt_task.mqttc_connected()
assert mqtt_handler.is_connected()

# Clean up
mqtt_task.mqttc_close()
mqtt_handler.close()

0 comments on commit 30813b0

Please sign in to comment.