17
17
# along with this program. If not, see <https://www.gnu.org/licenses/>.
18
18
19
19
import logging
20
+ import socket
20
21
import typing
21
22
import unittest .mock
22
23
35
36
# pylint: disable=too-many-arguments; these are tests, no API
36
37
37
38
38
- def test__mqtt_on_connect (caplog : _pytest .logging .LogCaptureFixture ) -> None :
39
+ @pytest .mark .parametrize (
40
+ ("socket_family" , "peername" , "peername_log" ),
41
+ [
42
+ (socket .AF_INET , ("mqtt-broker.local" , 1883 ), "mqtt-broker.local:1883" ),
43
+ # https://github.com/fphammerle/switchbot-mqtt/issues/42#issuecomment-1173909335
44
+ (socket .AF_INET6 , ("::1" , 1883 , 0 , 0 ), "[::1]:1883" ),
45
+ ],
46
+ )
47
+ def test__mqtt_on_connect (
48
+ caplog : _pytest .logging .LogCaptureFixture ,
49
+ socket_family : int , # socket.AddressFamily,
50
+ peername : typing .Tuple [typing .Union [str , int ]],
51
+ peername_log : str ,
52
+ ) -> None :
39
53
mqtt_client = unittest .mock .MagicMock ()
40
- mqtt_client .socket ().getpeername .return_value = ("mqtt-broker.local" , 1883 )
54
+ mqtt_client .socket ().family = socket_family
55
+ mqtt_client .socket ().getpeername .return_value = peername
41
56
with caplog .at_level (logging .DEBUG ):
42
57
switchbot_mqtt ._mqtt_on_connect (
43
58
mqtt_client ,
@@ -60,7 +75,7 @@ def test__mqtt_on_connect(caplog: _pytest.logging.LogCaptureFixture) -> None:
60
75
(
61
76
"switchbot_mqtt" ,
62
77
logging .DEBUG ,
63
- "connected to MQTT broker mqtt-broker.local:1883" ,
78
+ "connected to MQTT broker " + peername_log ,
64
79
),
65
80
(
66
81
"switchbot_mqtt._actors.base" ,
0 commit comments