Add ClientSessionGenerator type hints in tests (#118377)

pull/118378/head
epenet 2024-05-29 14:49:14 +02:00 committed by GitHub
parent d10362e226
commit 461ac1e0bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
26 changed files with 127 additions and 47 deletions

View File

@ -26,7 +26,7 @@ from tests.common import (
mock_integration,
mock_platform,
)
from tests.typing import WebSocketGenerator
from tests.typing import ClientSessionGenerator, WebSocketGenerator
@pytest.fixture
@ -43,7 +43,9 @@ def mock_test_component(hass):
@pytest.fixture
async def client(hass, hass_client) -> TestClient:
async def client(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> TestClient:
"""Fixture that can interact with the config manager API."""
await async_setup_component(hass, "http", {})
config_entries.async_setup(hass)

View File

@ -13,6 +13,8 @@ from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.setup import async_setup_component
from tests.typing import ClientSessionGenerator
SESSION_ID = "a9b84cec-46b6-484e-8f31-f65dba03ae6d"
INTENT_ID = "c6a74079-a8f0-46cd-b372-5a934d23591c"
INTENT_NAME = "tests"
@ -37,7 +39,7 @@ async def calls(hass: HomeAssistant, fixture) -> list[ServiceCall]:
@pytest.fixture
async def fixture(hass, hass_client_no_auth):
async def fixture(hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator):
"""Initialize a Home Assistant server for testing this module."""
await async_setup_component(hass, dialogflow.DOMAIN, {"dialogflow": {}})
await async_setup_component(

View File

@ -8,6 +8,7 @@ import json
from unittest.mock import patch
from aiohttp.hdrs import CONTENT_TYPE
from aiohttp.test_utils import TestClient
import pytest
from homeassistant import const, setup
@ -243,7 +244,9 @@ def _mock_hue_endpoints(
@pytest.fixture
async def hue_client(hass_hue, hass_client_no_auth):
async def hue_client(
hass_hue, hass_client_no_auth: ClientSessionGenerator
) -> TestClient:
"""Create web client for emulated hue api."""
_mock_hue_endpoints(
hass_hue,

View File

@ -1,12 +1,14 @@
"""The tests for the emulated Hue component."""
from asyncio import AbstractEventLoop
from collections.abc import Generator
from http import HTTPStatus
import json
import unittest
from unittest.mock import patch
from aiohttp import web
from aiohttp.test_utils import TestClient
import defusedxml.ElementTree as ET
import pytest
@ -45,7 +47,9 @@ def aiohttp_client(
@pytest.fixture
def hue_client(aiohttp_client):
def hue_client(
aiohttp_client: ClientSessionGenerator,
) -> Generator[TestClient, None, None]:
"""Return a hue API client."""
app = web.Application()
with unittest.mock.patch(

View File

@ -16,7 +16,9 @@ from tests.typing import ClientSessionGenerator
@pytest.fixture
async def uploaded_file_dir(hass: HomeAssistant, hass_client) -> Path:
async def uploaded_file_dir(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> Path:
"""Test uploading and using a file."""
assert await async_setup_component(hass, "file_upload", {})
client = await hass_client()

View File

@ -6,6 +6,7 @@ import re
from typing import Any
from unittest.mock import patch
from aiohttp.test_utils import TestClient
from freezegun.api import FrozenDateTimeFactory
import pytest
@ -99,13 +100,17 @@ def aiohttp_client(
@pytest.fixture
async def mock_http_client(hass, aiohttp_client, frontend):
async def mock_http_client(
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator, frontend
) -> TestClient:
"""Start the Home Assistant HTTP component."""
return await aiohttp_client(hass.http.app)
@pytest.fixture
async def themes_ws_client(hass, hass_ws_client, frontend_themes):
async def themes_ws_client(
hass: HomeAssistant, hass_ws_client: ClientSessionGenerator, frontend_themes
) -> TestClient:
"""Start the Home Assistant HTTP component."""
return await hass_ws_client(hass)
@ -117,7 +122,9 @@ async def ws_client(hass, hass_ws_client, frontend):
@pytest.fixture
async def mock_http_client_with_extra_js(hass, aiohttp_client, ignore_frontend_deps):
async def mock_http_client_with_extra_js(
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator, ignore_frontend_deps
) -> TestClient:
"""Start the Home Assistant HTTP component."""
assert await async_setup_component(
hass,

View File

@ -3,6 +3,7 @@
from http import HTTPStatus
from unittest.mock import patch
from aiohttp.test_utils import TestClient
import pytest
from homeassistant import config_entries
@ -21,6 +22,8 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component
from homeassistant.util import slugify
from tests.typing import ClientSessionGenerator
HOME_LATITUDE = 37.239622
HOME_LONGITUDE = -115.815811
@ -118,7 +121,9 @@ def mock_dev_track(mock_device_tracker_conf):
@pytest.fixture
async def geofency_client(hass, hass_client_no_auth):
async def geofency_client(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> TestClient:
"""Geofency mock client (unauthenticated)."""
assert await async_setup_component(

View File

@ -90,9 +90,9 @@ async def test_full_flow(
)
async def test_reauth(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
current_request_with_host,
current_request_with_host: None,
config_entry: MockConfigEntry,
fixture: str,
abort_reason: str,

View File

@ -19,6 +19,7 @@ from homeassistant.helpers import config_entry_oauth2_flow
from tests.common import MockConfigEntry, load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
from tests.typing import ClientSessionGenerator
CLIENT_ID = "1234"
CLIENT_SECRET = "5678"
@ -43,9 +44,9 @@ def setup_userinfo(user_identifier: str) -> Generator[Mock, None, None]:
async def test_full_flow(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
current_request_with_host,
current_request_with_host: None,
setup_credentials,
setup_userinfo,
) -> None:
@ -98,9 +99,9 @@ async def test_full_flow(
async def test_api_not_enabled(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
current_request_with_host,
current_request_with_host: None,
setup_credentials,
setup_userinfo,
) -> None:
@ -159,9 +160,9 @@ async def test_api_not_enabled(
async def test_general_exception(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
current_request_with_host,
current_request_with_host: None,
setup_credentials,
setup_userinfo,
) -> None:
@ -236,9 +237,9 @@ async def test_general_exception(
)
async def test_reauth(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
current_request_with_host,
current_request_with_host: None,
setup_credentials,
setup_userinfo,
user_identifier: str,

View File

@ -3,6 +3,7 @@
from http import HTTPStatus
from unittest.mock import patch
from aiohttp.test_utils import TestClient
import pytest
from homeassistant import config_entries
@ -17,6 +18,8 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.dispatcher import DATA_DISPATCHER
from homeassistant.setup import async_setup_component
from tests.typing import ClientSessionGenerator
HOME_LATITUDE = 37.239622
HOME_LONGITUDE = -115.815811
@ -27,7 +30,9 @@ def mock_dev_track(mock_device_tracker_conf):
@pytest.fixture
async def gpslogger_client(hass, hass_client_no_auth):
async def gpslogger_client(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> TestClient:
"""Mock client for GPSLogger (unauthenticated)."""
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})

View File

@ -4,6 +4,7 @@ import os
import re
from unittest.mock import Mock, patch
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components.hassio.handler import HassIO, HassioAPIError
@ -84,19 +85,25 @@ def hassio_stubs(
@pytest.fixture
def hassio_client(hassio_stubs, hass, hass_client):
def hassio_client(
hassio_stubs, hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> TestClient:
"""Return a Hass.io HTTP client."""
return hass.loop.run_until_complete(hass_client())
@pytest.fixture
def hassio_noauth_client(hassio_stubs, hass, aiohttp_client):
def hassio_noauth_client(
hassio_stubs, hass: HomeAssistant, aiohttp_client: ClientSessionGenerator
) -> TestClient:
"""Return a Hass.io HTTP client without auth."""
return hass.loop.run_until_complete(aiohttp_client(hass.http.app))
@pytest.fixture
async def hassio_client_supervisor(hass, aiohttp_client, hassio_stubs):
async def hassio_client_supervisor(
hass: HomeAssistant, aiohttp_client: ClientSessionGenerator, hassio_stubs
) -> TestClient:
"""Return an authenticated HTTP client."""
access_token = hass.auth.async_create_access_token(hassio_stubs)
return await aiohttp_client(

View File

@ -32,9 +32,9 @@ from tests.typing import ClientSessionGenerator
)
async def test_full_flow(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
current_request_with_host,
current_request_with_host: None,
jwt: str,
new_scope: str,
amount: int,

View File

@ -3,6 +3,7 @@
from http import HTTPStatus
from unittest.mock import patch
from aiohttp.test_utils import TestClient
import pytest
from homeassistant import config_entries
@ -15,6 +16,8 @@ from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.dispatcher import DATA_DISPATCHER
from homeassistant.setup import async_setup_component
from tests.typing import ClientSessionGenerator
@pytest.fixture(autouse=True)
def mock_dev_track(mock_device_tracker_conf):
@ -22,7 +25,9 @@ def mock_dev_track(mock_device_tracker_conf):
@pytest.fixture
async def locative_client(hass, hass_client):
async def locative_client(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> TestClient:
"""Locative mock client."""
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()

View File

@ -15,14 +15,15 @@ from homeassistant.helpers.network import get_url
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture
from tests.typing import ClientSessionGenerator
async def test_webhook_accepts_valid_message(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
integration: MockConfigEntry,
lock: loqed.Lock,
):
) -> None:
"""Test webhook called with valid message."""
await async_setup_component(hass, "http", {"http": {}})
client = await hass_client_no_auth()

View File

@ -3,21 +3,26 @@
import hashlib
import hmac
from aiohttp.test_utils import TestClient
import pytest
from homeassistant import config_entries
from homeassistant.components import mailgun, webhook
from homeassistant.config import async_process_ha_core_config
from homeassistant.const import CONF_API_KEY, CONF_DOMAIN
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.setup import async_setup_component
from tests.typing import ClientSessionGenerator
API_KEY = "abc123"
@pytest.fixture
async def http_client(hass, hass_client_no_auth):
async def http_client(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> TestClient:
"""Initialize a Home Assistant Server for testing this module."""
await async_setup_component(hass, webhook.DOMAIN, {})
return await hass_client_no_auth()

View File

@ -2,13 +2,17 @@
from http import HTTPStatus
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components.mobile_app.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .const import REGISTER, REGISTER_CLEARTEXT
from tests.typing import ClientSessionGenerator
@pytest.fixture
async def create_registrations(hass, webhook_client):
@ -53,7 +57,9 @@ async def push_registration(hass, webhook_client):
@pytest.fixture
async def webhook_client(hass, hass_client):
async def webhook_client(
hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> TestClient:
"""Provide an authenticated client for mobile_app to use."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
await hass.async_block_till_done()

View File

@ -98,7 +98,7 @@ def aiohttp_client(
@pytest.fixture
async def auth(aiohttp_client):
async def auth(aiohttp_client: ClientSessionGenerator) -> FakeAuth:
"""Fixture for an AbstractAuth."""
auth = FakeAuth()
app = aiohttp.web.Application()

View File

@ -1,11 +1,14 @@
"""Test the owntracks_http platform."""
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components import owntracks
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, mock_component
from tests.typing import ClientSessionGenerator
MINIMAL_LOCATION_MESSAGE = {
"_type": "location",
@ -39,7 +42,9 @@ def mock_dev_track(mock_device_tracker_conf):
@pytest.fixture
def mock_client(hass, hass_client_no_auth):
def mock_client(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> TestClient:
"""Start the Home Assistant HTTP component."""
mock_component(hass, "group")
mock_component(hass, "zone")

View File

@ -20,6 +20,7 @@ from .conftest import CONFIG_ENTRY_DATA_OLD_FORMAT, mock_response, mock_response
from tests.common import MockConfigEntry
from tests.test_util.aiohttp import AiohttpClientMockResponse
from tests.typing import ClientSessionGenerator
TEST_ENTITY = "calendar.rain_bird_controller"
type GetEventsFn = Callable[[str, str], Awaitable[dict[str, Any]]]
@ -237,7 +238,7 @@ async def test_no_schedule(
hass: HomeAssistant,
get_events: GetEventsFn,
responses: list[AiohttpClientMockResponse],
hass_client: Callable[..., Awaitable[ClientSession]],
hass_client: ClientSessionGenerator,
) -> None:
"""Test calendar error when fetching the calendar."""
responses.extend([mock_response_error(HTTPStatus.BAD_GATEWAY)]) # Arbitrary error

View File

@ -13,6 +13,7 @@ from .const import DEMAND, NETWORK_INFO, PRICE_CLUSTER, SUMMATION
from tests.common import patch
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
@pytest.fixture
@ -47,8 +48,11 @@ async def mock_entry_no_meters(hass: HomeAssistant, mock_device):
async def test_entry_diagnostics_no_meters(
hass, hass_client, mock_device, mock_entry_no_meters
):
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_device,
mock_entry_no_meters,
) -> None:
"""Test RAVEn diagnostics before the coordinator has updated."""
result = await get_diagnostics_for_config_entry(
hass, hass_client, mock_entry_no_meters
@ -66,7 +70,9 @@ async def test_entry_diagnostics_no_meters(
}
async def test_entry_diagnostics(hass, hass_client, mock_device, mock_entry):
async def test_entry_diagnostics(
hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_device, mock_entry
) -> None:
"""Test RAVEn diagnostics."""
result = await get_diagnostics_for_config_entry(hass, hass_client, mock_entry)

View File

@ -3,6 +3,7 @@
from http import HTTPStatus
from unittest.mock import patch
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components.spaceapi import DOMAIN, SPACEAPI_VERSION, URL_API_SPACEAPI
@ -10,6 +11,8 @@ from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, PERCENTAGE, UnitOfTemp
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.typing import ClientSessionGenerator
CONFIG = {
DOMAIN: {
"space": "Home",
@ -80,7 +83,7 @@ SENSOR_OUTPUT = {
@pytest.fixture
def mock_client(hass, hass_client):
def mock_client(hass: HomeAssistant, hass_client: ClientSessionGenerator) -> TestClient:
"""Start the Home Assistant HTTP component."""
with patch("homeassistant.components.spaceapi", return_value=True):
hass.loop.run_until_complete(async_setup_component(hass, "spaceapi", CONFIG))

View File

@ -69,7 +69,7 @@ class HlsClient:
@pytest.fixture
def hls_stream(hass, hass_client):
def hls_stream(hass: HomeAssistant, hass_client: ClientSessionGenerator):
"""Create test fixture for creating an HLS client for a stream."""
async def create_client_for_stream(stream):

View File

@ -33,6 +33,8 @@ from .common import (
)
from .test_hls import STREAM_SOURCE, HlsClient, make_playlist
from tests.typing import ClientSessionGenerator
SEGMENT_DURATION = 6
TEST_PART_DURATION = 0.75
NUM_PART_SEGMENTS = int(-(-SEGMENT_DURATION // TEST_PART_DURATION))
@ -45,7 +47,7 @@ VERY_LARGE_LAST_BYTE_POS = 9007199254740991
@pytest.fixture
def hls_stream(hass, hass_client):
def hls_stream(hass: HomeAssistant, hass_client: ClientSessionGenerator):
"""Create test fixture for creating an HLS client for a stream."""
async def create_client_for_stream(stream):

View File

@ -5,6 +5,7 @@ from ipaddress import ip_address
from unittest.mock import Mock, patch
from aiohttp import web
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components import webhook
@ -12,11 +13,11 @@ from homeassistant.config import async_process_ha_core_config
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.typing import WebSocketGenerator
from tests.typing import ClientSessionGenerator, WebSocketGenerator
@pytest.fixture
def mock_client(hass, hass_client):
def mock_client(hass: HomeAssistant, hass_client: ClientSessionGenerator) -> TestClient:
"""Create http client for webhooks."""
hass.loop.run_until_complete(async_setup_component(hass, "webhook", {}))
return hass.loop.run_until_complete(hass_client())

View File

@ -1,5 +1,6 @@
"""Fixtures for websocket tests."""
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components.websocket_api.auth import TYPE_AUTH_REQUIRED
@ -7,7 +8,11 @@ from homeassistant.components.websocket_api.http import URL
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.typing import MockHAClientWebSocket, WebSocketGenerator
from tests.typing import (
ClientSessionGenerator,
MockHAClientWebSocket,
WebSocketGenerator,
)
@pytest.fixture
@ -19,7 +24,9 @@ async def websocket_client(
@pytest.fixture
async def no_auth_websocket_client(hass, hass_client_no_auth):
async def no_auth_websocket_client(
hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator
) -> TestClient:
"""Websocket connection that requires authentication."""
assert await async_setup_component(hass, "websocket_api", {})
await hass.async_block_till_done()

View File

@ -231,9 +231,9 @@ async def test_flow_http_error(
)
async def test_reauth(
hass: HomeAssistant,
hass_client_no_auth,
hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker,
current_request_with_host,
current_request_with_host: None,
config_entry: MockConfigEntry,
fixture: str,
abort_reason: str,