mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Fix more tests on Python 3.8 (#34703)
This commit is contained in:
parent
5760fb94fe
commit
e7f8d6bbf7
@ -1,7 +1,7 @@
|
|||||||
"""Test the Home Assistant local auth provider."""
|
"""Test the Home Assistant local auth provider."""
|
||||||
import asyncio
|
import asyncio
|
||||||
from unittest.mock import Mock, patch
|
|
||||||
|
|
||||||
|
from asynctest import Mock, patch
|
||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -12,8 +12,6 @@ from homeassistant.auth.providers import (
|
|||||||
homeassistant as hass_auth,
|
homeassistant as hass_auth,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.common import mock_coro
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def data(hass):
|
def data(hass):
|
||||||
@ -156,9 +154,7 @@ async def test_get_or_create_credentials(hass, data):
|
|||||||
provider = manager.auth_providers[0]
|
provider = manager.auth_providers[0]
|
||||||
provider.data = data
|
provider.data = data
|
||||||
credentials1 = await provider.async_get_or_create_credentials({"username": "hello"})
|
credentials1 = await provider.async_get_or_create_credentials({"username": "hello"})
|
||||||
with patch.object(
|
with patch.object(provider, "async_credentials", return_value=[credentials1]):
|
||||||
provider, "async_credentials", return_value=mock_coro([credentials1])
|
|
||||||
):
|
|
||||||
credentials2 = await provider.async_get_or_create_credentials(
|
credentials2 = await provider.async_get_or_create_credentials(
|
||||||
{"username": "hello "}
|
{"username": "hello "}
|
||||||
)
|
)
|
||||||
@ -264,17 +260,13 @@ async def test_legacy_get_or_create_credentials(hass, legacy_data):
|
|||||||
provider.data = legacy_data
|
provider.data = legacy_data
|
||||||
credentials1 = await provider.async_get_or_create_credentials({"username": "hello"})
|
credentials1 = await provider.async_get_or_create_credentials({"username": "hello"})
|
||||||
|
|
||||||
with patch.object(
|
with patch.object(provider, "async_credentials", return_value=[credentials1]):
|
||||||
provider, "async_credentials", return_value=mock_coro([credentials1])
|
|
||||||
):
|
|
||||||
credentials2 = await provider.async_get_or_create_credentials(
|
credentials2 = await provider.async_get_or_create_credentials(
|
||||||
{"username": "hello"}
|
{"username": "hello"}
|
||||||
)
|
)
|
||||||
assert credentials1 is credentials2
|
assert credentials1 is credentials2
|
||||||
|
|
||||||
with patch.object(
|
with patch.object(provider, "async_credentials", return_value=[credentials1]):
|
||||||
provider, "async_credentials", return_value=mock_coro([credentials1])
|
|
||||||
):
|
|
||||||
credentials3 = await provider.async_get_or_create_credentials(
|
credentials3 = await provider.async_get_or_create_credentials(
|
||||||
{"username": "hello "}
|
{"username": "hello "}
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
"""Tests for the Config Entry Flow helper."""
|
"""Tests for the Config Entry Flow helper."""
|
||||||
from unittest.mock import Mock, patch
|
from asynctest import Mock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow, setup
|
from homeassistant import config_entries, data_entry_flow, setup
|
||||||
@ -9,7 +8,6 @@ from homeassistant.helpers import config_entry_flow
|
|||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockConfigEntry,
|
MockConfigEntry,
|
||||||
MockModule,
|
MockModule,
|
||||||
mock_coro,
|
|
||||||
mock_entity_platform,
|
mock_entity_platform,
|
||||||
mock_integration,
|
mock_integration,
|
||||||
)
|
)
|
||||||
@ -209,8 +207,8 @@ async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
|
|||||||
"""Test only a single entry is allowed."""
|
"""Test only a single entry is allowed."""
|
||||||
assert await setup.async_setup_component(hass, "cloud", {})
|
assert await setup.async_setup_component(hass, "cloud", {})
|
||||||
|
|
||||||
async_setup_entry = Mock(return_value=mock_coro(True))
|
async_setup_entry = Mock(return_value=True)
|
||||||
async_unload_entry = Mock(return_value=mock_coro(True))
|
async_unload_entry = Mock(return_value=True)
|
||||||
|
|
||||||
mock_integration(
|
mock_integration(
|
||||||
hass,
|
hass,
|
||||||
@ -228,10 +226,9 @@ async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
|
|||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||||
|
|
||||||
coro = mock_coro({"cloudhook_url": "https://example.com"})
|
|
||||||
|
|
||||||
with patch(
|
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(
|
) as mock_create, patch(
|
||||||
"homeassistant.components.cloud.async_active_subscription", return_value=True
|
"homeassistant.components.cloud.async_active_subscription", return_value=True
|
||||||
), patch(
|
), patch(
|
||||||
@ -246,7 +243,8 @@ async def test_webhook_create_cloudhook(hass, webhook_flow_conf):
|
|||||||
assert len(async_setup_entry.mock_calls) == 1
|
assert len(async_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
with patch(
|
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:
|
) as mock_delete:
|
||||||
|
|
||||||
result = await hass.config_entries.async_remove(result["result"].entry_id)
|
result = await hass.config_entries.async_remove(result["result"].entry_id)
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import Mock, patch
|
|
||||||
|
|
||||||
|
from asynctest import CoroutineMock, Mock, patch
|
||||||
import pytest
|
import pytest
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -30,7 +30,6 @@ from homeassistant.setup import async_setup_component
|
|||||||
from tests.common import (
|
from tests.common import (
|
||||||
MockEntity,
|
MockEntity,
|
||||||
get_test_home_assistant,
|
get_test_home_assistant,
|
||||||
mock_coro,
|
|
||||||
mock_device_registry,
|
mock_device_registry,
|
||||||
mock_registry,
|
mock_registry,
|
||||||
mock_service,
|
mock_service,
|
||||||
@ -40,10 +39,7 @@ from tests.common import (
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_handle_entity_call():
|
def mock_handle_entity_call():
|
||||||
"""Mock service platform call."""
|
"""Mock service platform call."""
|
||||||
with patch(
|
with patch("homeassistant.helpers.service._handle_entity_call") as mock_call:
|
||||||
"homeassistant.helpers.service._handle_entity_call",
|
|
||||||
side_effect=lambda *args: mock_coro(),
|
|
||||||
) as mock_call:
|
|
||||||
yield 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):
|
async def test_call_with_required_features(hass, mock_entities):
|
||||||
"""Test service calls invoked only if entity has required feautres."""
|
"""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(
|
await service.entity_service_call(
|
||||||
hass,
|
hass,
|
||||||
[Mock(entities=mock_entities)],
|
[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."""
|
"""Check we only target allowed entities if targeting all."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.auth.AuthManager.async_get_user",
|
"homeassistant.auth.AuthManager.async_get_user",
|
||||||
return_value=mock_coro(
|
return_value=Mock(
|
||||||
Mock(
|
permissions=PolicyPermissions(
|
||||||
permissions=PolicyPermissions(
|
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
|
||||||
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
@ -404,11 +398,9 @@ async def test_call_context_target_specific(
|
|||||||
"""Check targeting specific entities."""
|
"""Check targeting specific entities."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.auth.AuthManager.async_get_user",
|
"homeassistant.auth.AuthManager.async_get_user",
|
||||||
return_value=mock_coro(
|
return_value=Mock(
|
||||||
Mock(
|
permissions=PolicyPermissions(
|
||||||
permissions=PolicyPermissions(
|
{"entities": {"entity_ids": {"light.kitchen": True}}}, None
|
||||||
{"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 pytest.raises(exceptions.Unauthorized) as err:
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.auth.AuthManager.async_get_user",
|
"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(
|
await service.entity_service_call(
|
||||||
hass,
|
hass,
|
||||||
@ -606,7 +598,7 @@ async def test_domain_control_unknown(hass, mock_entities):
|
|||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.helpers.entity_registry.async_get_registry",
|
"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(
|
protected_mock_service = hass.helpers.service.verify_domain_control(
|
||||||
"test_domain"
|
"test_domain"
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import json
|
import json
|
||||||
from unittest.mock import Mock, patch
|
|
||||||
|
|
||||||
|
from asynctest import Mock, patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -14,7 +14,7 @@ from homeassistant.core import CoreState
|
|||||||
from homeassistant.helpers import storage
|
from homeassistant.helpers import storage
|
||||||
from homeassistant.util import dt
|
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_VERSION = 1
|
||||||
MOCK_KEY = "storage-test"
|
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):
|
async def test_migrator_no_existing_config(hass, store, hass_storage):
|
||||||
"""Test migrator with no existing config."""
|
"""Test migrator with no existing config."""
|
||||||
with patch("os.path.isfile", return_value=False), patch.object(
|
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)
|
data = await storage.async_migrator(hass, "old-path", store)
|
||||||
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
"""Test the config manager."""
|
"""Test the config manager."""
|
||||||
import asyncio
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import MagicMock, patch
|
|
||||||
|
|
||||||
from asynctest import CoroutineMock
|
from asynctest import CoroutineMock, MagicMock, patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow, loader
|
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 pytest.raises(data_entry_flow.UnknownHandler):
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.loader.async_get_integration",
|
"homeassistant.loader.async_get_integration", return_value=integration,
|
||||||
return_value=mock_coro(integration),
|
|
||||||
):
|
):
|
||||||
await hass.config_entries.flow.async_init("bla")
|
await hass.config_entries.flow.async_init("bla")
|
||||||
|
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
"""Test Home Assistant location util methods."""
|
"""Test Home Assistant location util methods."""
|
||||||
from unittest.mock import Mock, patch
|
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
from asynctest import Mock, patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import homeassistant.util.location as location_util
|
import homeassistant.util.location as location_util
|
||||||
|
|
||||||
from tests.common import load_fixture, mock_coro
|
from tests.common import load_fixture
|
||||||
|
|
||||||
# Paris
|
# Paris
|
||||||
COORDINATES_PARIS = (48.864716, 2.349014)
|
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."""
|
"""Test detect location info using ip-api.com."""
|
||||||
aioclient_mock.get(location_util.IP_API, text=load_fixture("ip-api.com.json"))
|
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)
|
info = await location_util.async_detect_location_info(session, _test_real=True)
|
||||||
|
|
||||||
assert info is not None
|
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):
|
async def test_detect_location_info_both_queries_fail(session):
|
||||||
"""Ensure we return None if both queries fail."""
|
"""Ensure we return None if both queries fail."""
|
||||||
with patch(
|
with patch("homeassistant.util.location._get_ipapi", return_value=None), patch(
|
||||||
"homeassistant.util.location._get_ipapi", return_value=mock_coro(None)
|
"homeassistant.util.location._get_ip_api", return_value=None
|
||||||
), patch("homeassistant.util.location._get_ip_api", return_value=mock_coro(None)):
|
):
|
||||||
info = await location_util.async_detect_location_info(session, _test_real=True)
|
info = await location_util.async_detect_location_info(session, _test_real=True)
|
||||||
assert info is None
|
assert info is None
|
||||||
|
|
||||||
|
@ -4,8 +4,8 @@ import logging
|
|||||||
import os
|
import os
|
||||||
from subprocess import PIPE
|
from subprocess import PIPE
|
||||||
import sys
|
import sys
|
||||||
from unittest.mock import MagicMock, call, patch
|
|
||||||
|
|
||||||
|
from asynctest import MagicMock, call, patch
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user