Use mock storage for MQTT tests (#33553)

* mock storage for MQTT

* more mqtt storage mocks
pull/33555/head
David F. Mulcahey 2020-04-02 18:13:44 -04:00 committed by GitHub
parent 36a606f81d
commit 201e958854
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 1 deletions

View File

@ -33,6 +33,7 @@ from tests.common import (
mock_device_registry, mock_device_registry,
mock_mqtt_component, mock_mqtt_component,
mock_registry, mock_registry,
mock_storage,
threadsafe_coroutine_factory, threadsafe_coroutine_factory,
) )
@ -83,12 +84,15 @@ class TestMQTTComponent(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started.""" """Set up things to be run when tests are started."""
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
mock_mqtt_component(self.hass) mock_mqtt_component(self.hass)
self.calls = [] self.calls = []
def tearDown(self): # pylint: disable=invalid-name def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started.""" """Stop everything that was started."""
self.hass.stop() self.hass.stop()
self.mock_storage.__exit__(None, None, None)
@callback @callback
def record_calls(self, *args): def record_calls(self, *args):
@ -298,12 +302,15 @@ class TestMQTTCallbacks(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started.""" """Set up things to be run when tests are started."""
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
mock_mqtt_client(self.hass) mock_mqtt_client(self.hass)
self.calls = [] self.calls = []
def tearDown(self): # pylint: disable=invalid-name def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started.""" """Stop everything that was started."""
self.hass.stop() self.hass.stop()
self.mock_storage.__exit__(None, None, None)
@callback @callback
def record_calls(self, *args): def record_calls(self, *args):

View File

@ -7,7 +7,7 @@ import homeassistant.components.mqtt as mqtt
from homeassistant.const import CONF_PASSWORD from homeassistant.const import CONF_PASSWORD
from homeassistant.setup import setup_component from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant, mock_coro from tests.common import get_test_home_assistant, mock_coro, mock_storage
class TestMQTT: class TestMQTT:
@ -16,10 +16,13 @@ class TestMQTT:
def setup_method(self, method): def setup_method(self, method):
"""Set up things to be run when tests are started.""" """Set up things to be run when tests are started."""
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
def teardown_method(self, method): def teardown_method(self, method):
"""Stop everything that was started.""" """Stop everything that was started."""
self.hass.stop() self.hass.stop()
self.mock_storage.__exit__(None, None, None)
@patch("passlib.apps.custom_app_context", Mock(return_value="")) @patch("passlib.apps.custom_app_context", Mock(return_value=""))
@patch("tempfile.NamedTemporaryFile", Mock(return_value=MagicMock())) @patch("tempfile.NamedTemporaryFile", Mock(return_value=MagicMock()))

View File

@ -15,6 +15,7 @@ from tests.common import (
get_test_home_assistant, get_test_home_assistant,
mock_mqtt_component, mock_mqtt_component,
mock_state_change_event, mock_state_change_event,
mock_storage,
) )
@ -24,11 +25,14 @@ class TestMqttEventStream:
def setup_method(self): def setup_method(self):
"""Set up things to be run when tests are started.""" """Set up things to be run when tests are started."""
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
self.mock_mqtt = mock_mqtt_component(self.hass) self.mock_mqtt = mock_mqtt_component(self.hass)
def teardown_method(self): def teardown_method(self):
"""Stop everything that was started.""" """Stop everything that was started."""
self.hass.stop() self.hass.stop()
self.mock_storage.__exit__(None, None, None)
def add_eventstream(self, sub_topic=None, pub_topic=None, ignore_event=None): def add_eventstream(self, sub_topic=None, pub_topic=None, ignore_event=None):
"""Add a mqtt_eventstream component.""" """Add a mqtt_eventstream component."""

View File

@ -9,6 +9,7 @@ from tests.common import (
get_test_home_assistant, get_test_home_assistant,
mock_mqtt_component, mock_mqtt_component,
mock_state_change_event, mock_state_change_event,
mock_storage,
) )
@ -18,11 +19,14 @@ class TestMqttStateStream:
def setup_method(self): def setup_method(self):
"""Set up things to be run when tests are started.""" """Set up things to be run when tests are started."""
self.hass = get_test_home_assistant() self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
self.mock_mqtt = mock_mqtt_component(self.hass) self.mock_mqtt = mock_mqtt_component(self.hass)
def teardown_method(self): def teardown_method(self):
"""Stop everything that was started.""" """Stop everything that was started."""
self.hass.stop() self.hass.stop()
self.mock_storage.__exit__(None, None, None)
def add_statestream( def add_statestream(
self, self,