From e7f8d6bbf7d17460a3be46f7f1858a5d490b67ec Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 25 Apr 2020 15:52:50 -0700 Subject: [PATCH] Fix more tests on Python 3.8 (#34703) --- tests/auth/providers/test_homeassistant.py | 16 +++--------- tests/helpers/test_config_entry_flow.py | 16 +++++------- tests/helpers/test_service.py | 30 ++++++++-------------- tests/helpers/test_storage.py | 6 ++--- tests/test_config_entries.py | 6 ++--- tests/util/test_location.py | 13 +++++----- tests/util/test_package.py | 2 +- 7 files changed, 34 insertions(+), 55 deletions(-) diff --git a/tests/auth/providers/test_homeassistant.py b/tests/auth/providers/test_homeassistant.py index 9a275c78ba6..9cfdfc30aa5 100644 --- a/tests/auth/providers/test_homeassistant.py +++ b/tests/auth/providers/test_homeassistant.py @@ -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 @@ -12,8 +12,6 @@ from homeassistant.auth.providers import ( homeassistant as hass_auth, ) -from tests.common import mock_coro - @pytest.fixture def data(hass): @@ -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 "} ) @@ -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 "} ) diff --git a/tests/helpers/test_config_entry_flow.py b/tests/helpers/test_config_entry_flow.py index 54003b9dd18..3126c2c0c8d 100644 --- a/tests/helpers/test_config_entry_flow.py +++ b/tests/helpers/test_config_entry_flow.py @@ -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 @@ -9,7 +8,6 @@ from homeassistant.helpers import config_entry_flow from tests.common import ( MockConfigEntry, MockModule, - mock_coro, mock_entity_platform, mock_integration, ) @@ -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, @@ -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( @@ -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) diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 04d0ec64b83..8819684e8d6 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -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 @@ -30,7 +30,6 @@ from homeassistant.setup import async_setup_component from tests.common import ( MockEntity, get_test_home_assistant, - mock_coro, mock_device_registry, mock_registry, mock_service, @@ -40,10 +39,7 @@ from tests.common import ( @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 @@ -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)], @@ -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 ) ), ): @@ -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 ) ), ): @@ -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, @@ -606,7 +598,7 @@ async def test_domain_control_unknown(hass, mock_entities): 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" diff --git a/tests/helpers/test_storage.py b/tests/helpers/test_storage.py index 61648c85ada..1966430113c 100644 --- a/tests/helpers/test_storage.py +++ b/tests/helpers/test_storage.py @@ -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 ( @@ -14,7 +14,7 @@ from homeassistant.core import CoreState 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" @@ -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) diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 28746bbfbe0..6d5c735b882 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -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 @@ -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") diff --git a/tests/util/test_location.py b/tests/util/test_location.py index 3f03619a052..968c6257f37 100644 --- a/tests/util/test_location.py +++ b/tests/util/test_location.py @@ -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) @@ -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 @@ -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 diff --git a/tests/util/test_package.py b/tests/util/test_package.py index ca4ed83734a..f7cce0e298f 100644 --- a/tests/util/test_package.py +++ b/tests/util/test_package.py @@ -4,8 +4,8 @@ import logging 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