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
16 changes: 4 additions & 12 deletions tests/auth/providers/test_homeassistant.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test the Home Assistant local auth provider."""
import asyncio
from unittest.mock import Mock, patch

from asynctest import Mock, patch
import pytest
import voluptuous as vol

Expand All @@ -12,8 +12,6 @@
homeassistant as hass_auth,
)

from tests.common import mock_coro


@pytest.fixture
def data(hass):
Expand Down Expand Up @@ -156,9 +154,7 @@ async def test_get_or_create_credentials(hass, data):
provider = manager.auth_providers[0]
provider.data = data
credentials1 = await provider.async_get_or_create_credentials({"username": "hello"})
with patch.object(
provider, "async_credentials", return_value=mock_coro([credentials1])
):
with patch.object(provider, "async_credentials", return_value=[credentials1]):
credentials2 = await provider.async_get_or_create_credentials(
{"username": "hello "}
)
Expand Down Expand Up @@ -264,17 +260,13 @@ async def test_legacy_get_or_create_credentials(hass, legacy_data):
provider.data = legacy_data
credentials1 = await provider.async_get_or_create_credentials({"username": "hello"})

with patch.object(
provider, "async_credentials", return_value=mock_coro([credentials1])
):
with patch.object(provider, "async_credentials", return_value=[credentials1]):
credentials2 = await provider.async_get_or_create_credentials(
{"username": "hello"}
)
assert credentials1 is credentials2

with patch.object(
provider, "async_credentials", return_value=mock_coro([credentials1])
):
with patch.object(provider, "async_credentials", return_value=[credentials1]):
credentials3 = await provider.async_get_or_create_credentials(
{"username": "hello "}
)
Expand Down
16 changes: 7 additions & 9 deletions tests/helpers/test_config_entry_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Tests for the Config Entry Flow helper."""
from unittest.mock import Mock, patch

from asynctest import Mock, patch
import pytest

from homeassistant import config_entries, data_entry_flow, setup
Expand All @@ -9,7 +8,6 @@
from tests.common import (
MockConfigEntry,
MockModule,
mock_coro,
mock_entity_platform,
mock_integration,
)
Expand Down Expand Up @@ -209,8 +207,8 @@ async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
"""Test only a single entry is allowed."""
assert await setup.async_setup_component(hass, "cloud", {})

async_setup_entry = Mock(return_value=mock_coro(True))
async_unload_entry = Mock(return_value=mock_coro(True))
async_setup_entry = Mock(return_value=True)
async_unload_entry = Mock(return_value=True)

mock_integration(
hass,
Expand All @@ -228,10 +226,9 @@ async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM

coro = mock_coro({"cloudhook_url": "https://example.com"})

with patch(
"hass_nabucasa.cloudhooks.Cloudhooks.async_create", return_value=coro
"hass_nabucasa.cloudhooks.Cloudhooks.async_create",
return_value={"cloudhook_url": "https://example.com"},
) as mock_create, patch(
"homeassistant.components.cloud.async_active_subscription", return_value=True
), patch(
Expand All @@ -246,7 +243,8 @@ async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
assert len(async_setup_entry.mock_calls) == 1

with patch(
"hass_nabucasa.cloudhooks.Cloudhooks.async_delete", return_value=coro
"hass_nabucasa.cloudhooks.Cloudhooks.async_delete",
return_value={"cloudhook_url": "https://example.com"},
) as mock_delete:

result = await hass.config_entries.async_remove(result["result"].entry_id)
Expand Down
30 changes: 11 additions & 19 deletions tests/helpers/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from collections import OrderedDict
from copy import deepcopy
import unittest
from unittest.mock import Mock, patch

from asynctest import CoroutineMock, Mock, patch
import pytest
import voluptuous as vol

Expand All @@ -30,7 +30,6 @@
from tests.common import (
MockEntity,
get_test_home_assistant,
mock_coro,
mock_device_registry,
mock_registry,
mock_service,
Expand All @@ -40,10 +39,7 @@
@pytest.fixture
def mock_handle_entity_call():
"""Mock service platform call."""
with patch(
"homeassistant.helpers.service._handle_entity_call",
side_effect=lambda *args: mock_coro(),
) as mock_call:
with patch("homeassistant.helpers.service._handle_entity_call") as mock_call:
yield mock_call


Expand Down Expand Up @@ -310,7 +306,7 @@ async def test_async_get_all_descriptions(hass):

async def test_call_with_required_features(hass, mock_entities):
"""Test service calls invoked only if entity has required feautres."""
test_service_mock = Mock(return_value=mock_coro())
test_service_mock = CoroutineMock(return_value=None)
await service.entity_service_call(
hass,
[Mock(entities=mock_entities)],
Expand Down Expand Up @@ -374,11 +370,9 @@ async def test_call_context_target_all(hass, mock_handle_entity_call, mock_entit
"""Check we only target allowed entities if targeting all."""
with patch(
"homeassistant.auth.AuthManager.async_get_user",
return_value=mock_coro(
Mock(
permissions=PolicyPermissions(
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
)
return_value=Mock(
permissions=PolicyPermissions(
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
)
),
):
Expand All @@ -404,11 +398,9 @@ async def test_call_context_target_specific(
"""Check targeting specific entities."""
with patch(
"homeassistant.auth.AuthManager.async_get_user",
return_value=mock_coro(
Mock(
permissions=PolicyPermissions(
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
)
return_value=Mock(
permissions=PolicyPermissions(
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
)
),
):
Expand All @@ -435,7 +427,7 @@ async def test_call_context_target_specific_no_auth(
with pytest.raises(exceptions.Unauthorized) as err:
with patch(
"homeassistant.auth.AuthManager.async_get_user",
return_value=mock_coro(Mock(permissions=PolicyPermissions({}, None))),
return_value=Mock(permissions=PolicyPermissions({}, None)),
):
await service.entity_service_call(
hass,
Expand Down Expand Up @@ -606,7 +598,7 @@ async def mock_service_log(call):

with patch(
"homeassistant.helpers.entity_registry.async_get_registry",
return_value=mock_coro(Mock(entities=mock_entities)),
return_value=Mock(entities=mock_entities),
):
protected_mock_service = hass.helpers.service.verify_domain_control(
"test_domain"
Expand Down
6 changes: 3 additions & 3 deletions tests/helpers/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import asyncio
from datetime import timedelta
import json
from unittest.mock import Mock, patch

from asynctest import Mock, patch
import pytest

from homeassistant.const import (
Expand All @@ -14,7 +14,7 @@
from homeassistant.helpers import storage
from homeassistant.util import dt

from tests.common import async_fire_time_changed, mock_coro
from tests.common import async_fire_time_changed

MOCK_VERSION = 1
MOCK_KEY = "storage-test"
Expand Down Expand Up @@ -189,7 +189,7 @@ async def test_writing_while_writing_delay(hass, store, hass_storage):
async def test_migrator_no_existing_config(hass, store, hass_storage):
"""Test migrator with no existing config."""
with patch("os.path.isfile", return_value=False), patch.object(
store, "async_load", return_value=mock_coro({"cur": "config"})
store, "async_load", return_value={"cur": "config"}
):
data = await storage.async_migrator(hass, "old-path", store)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_config_entries.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""Test the config manager."""
import asyncio
from datetime import timedelta
from unittest.mock import MagicMock, patch

from asynctest import CoroutineMock
from asynctest import CoroutineMock, MagicMock, patch
import pytest

from homeassistant import config_entries, data_entry_flow, loader
Expand Down Expand Up @@ -935,8 +934,7 @@ async def test_init_custom_integration(hass):
)
with pytest.raises(data_entry_flow.UnknownHandler):
with patch(
"homeassistant.loader.async_get_integration",
return_value=mock_coro(integration),
"homeassistant.loader.async_get_integration", return_value=integration,
):
await hass.config_entries.flow.async_init("bla")

Expand Down
13 changes: 6 additions & 7 deletions tests/util/test_location.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Test Home Assistant location util methods."""
from unittest.mock import Mock, patch

import aiohttp
from asynctest import Mock, patch
import pytest

import homeassistant.util.location as location_util

from tests.common import load_fixture, mock_coro
from tests.common import load_fixture

# Paris
COORDINATES_PARIS = (48.864716, 2.349014)
Expand Down Expand Up @@ -109,7 +108,7 @@ async def test_detect_location_info_ip_api(aioclient_mock, session):
"""Test detect location info using ip-api.com."""
aioclient_mock.get(location_util.IP_API, text=load_fixture("ip-api.com.json"))

with patch("homeassistant.util.location._get_ipapi", return_value=mock_coro(None)):
with patch("homeassistant.util.location._get_ipapi", return_value=None):
info = await location_util.async_detect_location_info(session, _test_real=True)

assert info is not None
Expand All @@ -128,9 +127,9 @@ async def test_detect_location_info_ip_api(aioclient_mock, session):

async def test_detect_location_info_both_queries_fail(session):
"""Ensure we return None if both queries fail."""
with patch(
"homeassistant.util.location._get_ipapi", return_value=mock_coro(None)
), patch("homeassistant.util.location._get_ip_api", return_value=mock_coro(None)):
with patch("homeassistant.util.location._get_ipapi", return_value=None), patch(
"homeassistant.util.location._get_ip_api", return_value=None
):
info = await location_util.async_detect_location_info(session, _test_real=True)
assert info is None

Expand Down
2 changes: 1 addition & 1 deletion tests/util/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import os
from subprocess import PIPE
import sys
from unittest.mock import MagicMock, call, patch

from asynctest import MagicMock, call, patch
import pkg_resources
import pytest

Expand Down