Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import os
import sys
import threading
from unittest.mock import MagicMock, Mock, patch
import uuid

from aiohttp.test_utils import unused_port as get_test_instance_port # noqa
from asynctest import MagicMock, Mock, patch

from homeassistant import auth, config_entries, core as ha, loader
from homeassistant.auth import (
Expand Down
29 changes: 11 additions & 18 deletions tests/components/hue/test_bridge.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
"""Test Hue bridge."""
from unittest.mock import Mock, patch

from asynctest import CoroutineMock, Mock, patch
import pytest

from homeassistant.components.hue import bridge, errors
from homeassistant.exceptions import ConfigEntryNotReady

from tests.common import mock_coro


async def test_bridge_setup(hass):
"""Test a successful setup."""
entry = Mock()
api = Mock(initialize=mock_coro)
api = Mock(initialize=CoroutineMock())
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
hue_bridge = bridge.HueBridge(hass, entry, False, False)

Expand All @@ -35,9 +32,7 @@ async def test_bridge_setup_invalid_username(hass):

with patch.object(
bridge, "authenticate_bridge", side_effect=errors.AuthenticationRequired
), patch.object(
hass.config_entries.flow, "async_init", return_value=mock_coro()
) as mock_init:
), patch.object(hass.config_entries.flow, "async_init") as mock_init:
assert await hue_bridge.async_setup() is False

assert len(mock_init.mock_calls) == 1
Expand Down Expand Up @@ -78,18 +73,16 @@ async def test_reset_unloads_entry_if_setup(hass):
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
hue_bridge = bridge.HueBridge(hass, entry, False, False)

with patch.object(
bridge, "authenticate_bridge", return_value=mock_coro(Mock())
), patch("aiohue.Bridge", return_value=Mock()), patch.object(
hass.config_entries, "async_forward_entry_setup"
) as mock_forward:
with patch.object(bridge, "authenticate_bridge", return_value=Mock()), patch(
"aiohue.Bridge", return_value=Mock()
), patch.object(hass.config_entries, "async_forward_entry_setup") as mock_forward:
assert await hue_bridge.async_setup() is True

assert len(hass.services.async_services()) == 1
assert len(mock_forward.mock_calls) == 3

with patch.object(
hass.config_entries, "async_forward_entry_unload", return_value=mock_coro(True)
hass.config_entries, "async_forward_entry_unload", return_value=True
) as mock_forward:
assert await hue_bridge.async_reset()

Expand All @@ -99,13 +92,13 @@ async def test_reset_unloads_entry_if_setup(hass):

async def test_handle_unauthorized(hass):
"""Test handling an unauthorized error on update."""
entry = Mock(async_setup=Mock(return_value=mock_coro(Mock())))
entry = Mock(async_setup=CoroutineMock())
entry.data = {"host": "1.2.3.4", "username": "mock-username"}
hue_bridge = bridge.HueBridge(hass, entry, False, False)

with patch.object(
bridge, "authenticate_bridge", return_value=mock_coro(Mock())
), patch("aiohue.Bridge", return_value=Mock()):
with patch.object(bridge, "authenticate_bridge", return_value=Mock()), patch(
"aiohue.Bridge", return_value=Mock()
):
assert await hue_bridge.async_setup() is True

assert hue_bridge.authorized is True
Expand Down
12 changes: 5 additions & 7 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
mock_device_registry,
mock_mqtt_component,
mock_registry,
mock_storage,
threadsafe_coroutine_factory,
)
from tests.testing_config.custom_components.test.sensor import DEVICE_CLASSES


@pytest.fixture(autouse=True)
def mock_storage(hass_storage):
"""Autouse hass_storage for the TestCase tests."""


@pytest.fixture
def device_reg(hass):
"""Return an empty, loaded, registry."""
Expand Down Expand Up @@ -87,15 +91,12 @@ class TestMQTTComponent(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
mock_mqtt_component(self.hass)
self.calls = []

def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
self.mock_storage.__exit__(None, None, None)

@callback
def record_calls(self, *args):
Expand Down Expand Up @@ -305,15 +306,12 @@ class TestMQTTCallbacks(unittest.TestCase):
def setUp(self): # pylint: disable=invalid-name
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
mock_mqtt_client(self.hass)
self.calls = []

def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
self.mock_storage.__exit__(None, None, None)

@callback
def record_calls(self, *args):
Expand Down
11 changes: 7 additions & 4 deletions tests/components/mqtt/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
from unittest.mock import MagicMock, Mock

from asynctest import CoroutineMock, patch
import pytest

import homeassistant.components.mqtt as mqtt
from homeassistant.const import CONF_PASSWORD
from homeassistant.setup import setup_component

from tests.common import get_test_home_assistant, mock_coro, mock_storage
from tests.common import get_test_home_assistant, mock_coro


@pytest.fixture(autouse=True)
def inject_fixture(hass_storage):
"""Inject pytest fixtures."""


class TestMQTT:
Expand All @@ -16,13 +22,10 @@ class TestMQTT:
def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()

def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
self.mock_storage.__exit__(None, None, None)

@patch("passlib.apps.custom_app_context", Mock(return_value=""))
@patch("tempfile.NamedTemporaryFile", Mock(return_value=MagicMock()))
Expand Down
11 changes: 7 additions & 4 deletions tests/components/mqtt_eventstream/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
from unittest.mock import ANY, patch

import pytest

import homeassistant.components.mqtt_eventstream as eventstream
from homeassistant.const import EVENT_STATE_CHANGED
from homeassistant.core import State, callback
Expand All @@ -15,24 +17,25 @@
get_test_home_assistant,
mock_mqtt_component,
mock_state_change_event,
mock_storage,
)


@pytest.fixture(autouse=True)
def mock_storage(hass_storage):
"""Autouse hass_storage for the TestCase tests."""


class TestMqttEventStream:
"""Test the MQTT eventstream module."""

def setup_method(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
self.mock_mqtt = mock_mqtt_component(self.hass)

def teardown_method(self):
"""Stop everything that was started."""
self.hass.stop()
self.mock_storage.__exit__(None, None, None)

def add_eventstream(self, sub_topic=None, pub_topic=None, ignore_event=None):
"""Add a mqtt_eventstream component."""
Expand Down
11 changes: 7 additions & 4 deletions tests/components/mqtt_statestream/test_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""The tests for the MQTT statestream component."""
from unittest.mock import ANY, call, patch

import pytest

import homeassistant.components.mqtt_statestream as statestream
from homeassistant.core import State
from homeassistant.setup import setup_component
Expand All @@ -9,24 +11,25 @@
get_test_home_assistant,
mock_mqtt_component,
mock_state_change_event,
mock_storage,
)


@pytest.fixture(autouse=True)
def mock_storage(hass_storage):
"""Autouse hass_storage for the TestCase tests."""


class TestMqttStateStream:
"""Test the MQTT statestream module."""

def setup_method(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.mock_storage = mock_storage()
self.mock_storage.__enter__()
self.mock_mqtt = mock_mqtt_component(self.hass)

def teardown_method(self):
"""Stop everything that was started."""
self.hass.stop()
self.mock_storage.__exit__(None, None, None)

def add_statestream(
self,
Expand Down
Loading