|
1 | 1 | import sys |
2 | | -import pytest |
3 | | - |
4 | | -from compas_eve.mqtt.mqtt_paho import MqttTransport, PAHO_MQTT_V2_AVAILABLE |
5 | | - |
6 | | - |
7 | | -def test_paho_mqtt_v1_compatibility(): |
8 | | - if sys.platform == "cli": |
9 | | - pytest.skip("Not running on CLI") |
10 | 2 |
|
| 3 | +if sys.platform != "cli": |
| 4 | + import pytest |
11 | 5 | from unittest.mock import Mock, patch |
12 | | - |
13 | | - """Test that the MqttTransport works with paho-mqtt 1.x style client creation.""" |
14 | | - with patch("compas_eve.mqtt.mqtt_paho.PAHO_MQTT_V2_AVAILABLE", False), patch( |
15 | | - "paho.mqtt.client.Client" |
16 | | - ) as mock_client_class: |
17 | | - |
18 | | - mock_client = Mock() |
19 | | - mock_client_class.return_value = mock_client |
20 | | - |
21 | | - # This should work as if paho-mqtt 1.x is installed |
22 | | - transport = MqttTransport("localhost") |
23 | | - |
24 | | - # Should have called mqtt.Client() with client_id parameter only (no callback_api_version) |
25 | | - mock_client_class.assert_called_once() |
26 | | - call_args = mock_client_class.call_args |
27 | | - assert "client_id" in call_args.kwargs |
28 | | - assert call_args.kwargs["client_id"].startswith("compas_eve_") |
29 | | - assert "callback_api_version" not in call_args.kwargs |
30 | | - assert transport.client == mock_client |
31 | | - |
32 | | - |
33 | | -def test_paho_mqtt_v2_compatibility(): |
34 | | - if sys.platform == "cli": |
35 | | - pytest.skip("Not running on CLI") |
36 | | - from unittest.mock import Mock, patch |
37 | | - |
38 | | - """Test that the MqttTransport works with paho-mqtt 2.x style client creation.""" |
39 | | - if not PAHO_MQTT_V2_AVAILABLE: |
40 | | - pytest.skip("paho-mqtt 2.x not available in this environment") |
41 | | - |
42 | | - with patch("paho.mqtt.client.Client") as mock_client_class: |
43 | | - from paho.mqtt.enums import CallbackAPIVersion |
44 | | - |
45 | | - mock_client = Mock() |
46 | | - mock_client_class.return_value = mock_client |
47 | | - |
48 | | - # This should work as if paho-mqtt 2.x is installed |
49 | | - transport = MqttTransport("localhost") |
50 | | - |
51 | | - # Should have called mqtt.Client() with both client_id and callback_api_version parameters |
52 | | - mock_client_class.assert_called_once() |
53 | | - call_args = mock_client_class.call_args |
54 | | - assert "client_id" in call_args.kwargs |
55 | | - assert call_args.kwargs["client_id"].startswith("compas_eve_") |
56 | | - assert "callback_api_version" in call_args.kwargs |
57 | | - assert call_args.kwargs["callback_api_version"] == CallbackAPIVersion.VERSION1 |
58 | | - assert transport.client == mock_client |
| 6 | + from compas_eve.mqtt.mqtt_paho import MqttTransport, PAHO_MQTT_V2_AVAILABLE |
| 7 | + |
| 8 | + def test_paho_mqtt_v1_compatibility(): |
| 9 | + with patch("compas_eve.mqtt.mqtt_paho.PAHO_MQTT_V2_AVAILABLE", False), patch( |
| 10 | + "paho.mqtt.client.Client" |
| 11 | + ) as mock_client_class: |
| 12 | + |
| 13 | + mock_client = Mock() |
| 14 | + mock_client_class.return_value = mock_client |
| 15 | + |
| 16 | + # This should work as if paho-mqtt 1.x is installed |
| 17 | + transport = MqttTransport("localhost") |
| 18 | + |
| 19 | + # Should have called mqtt.Client() with client_id parameter only (no callback_api_version) |
| 20 | + mock_client_class.assert_called_once() |
| 21 | + call_args = mock_client_class.call_args |
| 22 | + assert "client_id" in call_args.kwargs |
| 23 | + assert call_args.kwargs["client_id"].startswith("compas_eve_") |
| 24 | + assert "callback_api_version" not in call_args.kwargs |
| 25 | + assert transport.client == mock_client |
| 26 | + |
| 27 | + def test_paho_mqtt_v2_compatibility(): |
| 28 | + if not PAHO_MQTT_V2_AVAILABLE: |
| 29 | + pytest.skip("paho-mqtt 2.x not available in this environment") |
| 30 | + |
| 31 | + with patch("paho.mqtt.client.Client") as mock_client_class: |
| 32 | + from paho.mqtt.enums import CallbackAPIVersion |
| 33 | + |
| 34 | + mock_client = Mock() |
| 35 | + mock_client_class.return_value = mock_client |
| 36 | + |
| 37 | + # This should work as if paho-mqtt 2.x is installed |
| 38 | + transport = MqttTransport("localhost") |
| 39 | + |
| 40 | + # Should have called mqtt.Client() with both client_id and callback_api_version parameters |
| 41 | + mock_client_class.assert_called_once() |
| 42 | + call_args = mock_client_class.call_args |
| 43 | + assert "client_id" in call_args.kwargs |
| 44 | + assert call_args.kwargs["client_id"].startswith("compas_eve_") |
| 45 | + assert "callback_api_version" in call_args.kwargs |
| 46 | + assert call_args.kwargs["callback_api_version"] == CallbackAPIVersion.VERSION1 |
| 47 | + assert transport.client == mock_client |
0 commit comments