rewrite mqtt unittest tests to pytest style (#42122)

This commit is contained in:
Tim Werner 2020-10-20 16:35:49 +02:00 committed by GitHub
parent 6c3362217e
commit e5aa5e2c35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 18 deletions

View file

@ -2,7 +2,6 @@
import copy import copy
from datetime import datetime from datetime import datetime
import json import json
from unittest import mock
from homeassistant.components import mqtt from homeassistant.components import mqtt
from homeassistant.components.mqtt import debug_info from homeassistant.components.mqtt import debug_info
@ -11,7 +10,7 @@ from homeassistant.const import ATTR_ASSUMED_STATE, STATE_UNAVAILABLE
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.async_mock import ANY from tests.async_mock import ANY, patch
from tests.common import async_fire_mqtt_message, mock_registry from tests.common import async_fire_mqtt_message, mock_registry
DEFAULT_CONFIG_DEVICE_INFO_ID = { DEFAULT_CONFIG_DEVICE_INFO_ID = {
@ -834,7 +833,7 @@ async def help_test_entity_debug_info_max_messages(hass, mqtt_mock, domain, conf
] ]
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0)
with mock.patch("homeassistant.util.dt.utcnow") as dt_utcnow: with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = start_dt dt_utcnow.return_value = start_dt
for i in range(0, debug_info.STORED_MESSAGES + 1): for i in range(0, debug_info.STORED_MESSAGES + 1):
async_fire_mqtt_message(hass, "test-topic", f"{i}") async_fire_mqtt_message(hass, "test-topic", f"{i}")
@ -896,7 +895,7 @@ async def help_test_entity_debug_info_message(
] ]
start_dt = datetime(2019, 1, 1, 0, 0, 0) start_dt = datetime(2019, 1, 1, 0, 0, 0)
with mock.patch("homeassistant.util.dt.utcnow") as dt_utcnow: with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = start_dt dt_utcnow.return_value = start_dt
async_fire_mqtt_message(hass, topic, payload) async_fire_mqtt_message(hass, topic, payload)

View file

@ -1,12 +1,11 @@
"""The tests for the MQTT subscription component.""" """The tests for the MQTT subscription component."""
from unittest import mock
from homeassistant.components.mqtt.subscription import ( from homeassistant.components.mqtt.subscription import (
async_subscribe_topics, async_subscribe_topics,
async_unsubscribe_topics, async_unsubscribe_topics,
) )
from homeassistant.core import callback from homeassistant.core import callback
from tests.async_mock import ANY
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
@ -131,9 +130,7 @@ async def test_qos_encoding_default(hass, mqtt_mock, caplog):
sub_state, sub_state,
{"test_topic1": {"topic": "test-topic1", "msg_callback": msg_callback}}, {"test_topic1": {"topic": "test-topic1", "msg_callback": msg_callback}},
) )
mqtt_mock.async_subscribe.assert_called_once_with( mqtt_mock.async_subscribe.assert_called_once_with("test-topic1", ANY, 0, "utf-8")
"test-topic1", mock.ANY, 0, "utf-8"
)
async def test_qos_encoding_custom(hass, mqtt_mock, caplog): async def test_qos_encoding_custom(hass, mqtt_mock, caplog):
@ -157,9 +154,7 @@ async def test_qos_encoding_custom(hass, mqtt_mock, caplog):
} }
}, },
) )
mqtt_mock.async_subscribe.assert_called_once_with( mqtt_mock.async_subscribe.assert_called_once_with("test-topic1", ANY, 1, "utf-16")
"test-topic1", mock.ANY, 1, "utf-16"
)
async def test_no_change(hass, mqtt_mock, caplog): async def test_no_change(hass, mqtt_mock, caplog):

View file

@ -1,12 +1,11 @@
"""The tests for the MQTT automation.""" """The tests for the MQTT automation."""
from unittest import mock
import pytest import pytest
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.async_mock import ANY
from tests.common import async_fire_mqtt_message, async_mock_service, mock_component from tests.common import async_fire_mqtt_message, async_mock_service, mock_component
@ -115,9 +114,7 @@ async def test_encoding_default(hass, calls, mqtt_mock):
}, },
) )
mqtt_mock.async_subscribe.assert_called_once_with( mqtt_mock.async_subscribe.assert_called_once_with("test-topic", ANY, 0, "utf-8")
"test-topic", mock.ANY, 0, "utf-8"
)
async def test_encoding_custom(hass, calls, mqtt_mock): async def test_encoding_custom(hass, calls, mqtt_mock):
@ -133,4 +130,4 @@ async def test_encoding_custom(hass, calls, mqtt_mock):
}, },
) )
mqtt_mock.async_subscribe.assert_called_once_with("test-topic", mock.ANY, 0, None) mqtt_mock.async_subscribe.assert_called_once_with("test-topic", ANY, 0, None)