Add LogCaptureFixture type hints in tests (#118372)

This commit is contained in:
epenet 2024-05-29 14:10:00 +02:00 committed by GitHub
parent aeee222df4
commit 166c588cac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 98 additions and 47 deletions

View file

@ -76,7 +76,7 @@ async def test_sensors_disappearing(
open_api: OpenAPI,
aioambient,
config_entry,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that we log errors properly."""

View file

@ -83,7 +83,9 @@ async def test_async_setup_raises_entry_auth_failed(
assert flow["context"]["entry_id"] == entry.entry_id
async def test_integration_services(hass: HomeAssistant, cfupdate, caplog) -> None:
async def test_integration_services(
hass: HomeAssistant, cfupdate, caplog: pytest.LogCaptureFixture
) -> None:
"""Test integration services."""
instance = cfupdate.return_value
@ -144,7 +146,7 @@ async def test_integration_services_with_issue(hass: HomeAssistant, cfupdate) ->
async def test_integration_services_with_nonexisting_record(
hass: HomeAssistant, cfupdate, caplog
hass: HomeAssistant, cfupdate, caplog: pytest.LogCaptureFixture
) -> None:
"""Test integration services."""
instance = cfupdate.return_value
@ -185,7 +187,7 @@ async def test_integration_services_with_nonexisting_record(
async def test_integration_update_interval(
hass: HomeAssistant,
cfupdate,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test integration update interval."""
instance = cfupdate.return_value

View file

@ -74,7 +74,7 @@ async def test_fetching_url(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
fakeimgbytes_png,
caplog: pytest.CaptureFixture,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that it fetches the given url."""
hass.states.async_set("sensor.temp", "http://example.com/0a")

View file

@ -471,7 +471,7 @@ async def test_sensor_bad_value(hass: HomeAssistant, setup_comp_2) -> None:
async def test_sensor_bad_value_twice(
hass: HomeAssistant, setup_comp_2, caplog
hass: HomeAssistant, setup_comp_2, caplog: pytest.LogCaptureFixture
) -> None:
"""Test sensor that the second bad value is not logged as warning."""
assert hass.states.get(ENTITY).state == STATE_ON

View file

@ -1,5 +1,7 @@
"""Test the send_message service."""
import pytest
from homeassistant.components.matrix import (
ATTR_FORMAT,
ATTR_IMAGES,
@ -14,7 +16,11 @@ from tests.components.matrix.conftest import TEST_BAD_ROOM, TEST_JOINABLE_ROOMS
async def test_send_message(
hass: HomeAssistant, matrix_bot: MatrixBot, image_path, matrix_events, caplog
hass: HomeAssistant,
matrix_bot: MatrixBot,
image_path,
matrix_events,
caplog: pytest.LogCaptureFixture,
):
"""Test the send_message service."""
@ -55,7 +61,10 @@ async def test_send_message(
async def test_unsendable_message(
hass: HomeAssistant, matrix_bot: MatrixBot, matrix_events, caplog
hass: HomeAssistant,
matrix_bot: MatrixBot,
matrix_events,
caplog: pytest.LogCaptureFixture,
):
"""Test the send_message service with an invalid room."""
assert len(matrix_events) == 0

View file

@ -8,6 +8,7 @@ mode (e.g. yaml, ConfigEntry, etc) however some tests override and just run in
relevant modes.
"""
from collections.abc import Generator
import logging
from typing import Any
from unittest.mock import patch
@ -48,14 +49,18 @@ def platforms() -> list[str]:
@pytest.fixture
def error_caplog(caplog):
def error_caplog(
caplog: pytest.LogCaptureFixture,
) -> Generator[pytest.LogCaptureFixture, None, None]:
"""Fixture to capture nest init error messages."""
with caplog.at_level(logging.ERROR, logger="homeassistant.components.nest"):
yield caplog
@pytest.fixture
def warning_caplog(caplog):
def warning_caplog(
caplog: pytest.LogCaptureFixture,
) -> Generator[pytest.LogCaptureFixture, None, None]:
"""Fixture to capture nest init warning messages."""
with caplog.at_level(logging.WARNING, logger="homeassistant.components.nest"):
yield caplog
@ -78,7 +83,9 @@ def failing_subscriber(subscriber_side_effect: Any) -> YieldFixture[FakeSubscrib
yield subscriber
async def test_setup_success(hass: HomeAssistant, error_caplog, setup_platform) -> None:
async def test_setup_success(
hass: HomeAssistant, error_caplog: pytest.LogCaptureFixture, setup_platform
) -> None:
"""Test successful setup."""
await setup_platform()
assert not error_caplog.records
@ -109,7 +116,10 @@ async def test_setup_configuration_failure(
@pytest.mark.parametrize("subscriber_side_effect", [SubscriberException()])
async def test_setup_susbcriber_failure(
hass: HomeAssistant, caplog, failing_subscriber, setup_base_platform
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
failing_subscriber,
setup_base_platform,
) -> None:
"""Test configuration error."""
await setup_base_platform()
@ -121,7 +131,7 @@ async def test_setup_susbcriber_failure(
async def test_setup_device_manager_failure(
hass: HomeAssistant, caplog, setup_base_platform
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, setup_base_platform
) -> None:
"""Test device manager api failure."""
with (
@ -161,7 +171,7 @@ async def test_subscriber_auth_failure(
@pytest.mark.parametrize("subscriber_id", [(None)])
async def test_setup_missing_subscriber_id(
hass: HomeAssistant, warning_caplog, setup_base_platform
hass: HomeAssistant, warning_caplog: pytest.LogCaptureFixture, setup_base_platform
) -> None:
"""Test missing subscriber id from configuration."""
await setup_base_platform()
@ -174,7 +184,10 @@ async def test_setup_missing_subscriber_id(
@pytest.mark.parametrize("subscriber_side_effect", [(ConfigurationException())])
async def test_subscriber_configuration_failure(
hass: HomeAssistant, error_caplog, setup_base_platform, failing_subscriber
hass: HomeAssistant,
error_caplog: pytest.LogCaptureFixture,
setup_base_platform,
failing_subscriber,
) -> None:
"""Test configuration error."""
await setup_base_platform()
@ -187,7 +200,7 @@ async def test_subscriber_configuration_failure(
@pytest.mark.parametrize("nest_test_config", [TEST_CONFIGFLOW_APP_CREDS])
async def test_empty_config(
hass: HomeAssistant, error_caplog, config, setup_platform
hass: HomeAssistant, error_caplog: pytest.LogCaptureFixture, config, setup_platform
) -> None:
"""Test setup is a no-op with not config."""
await setup_platform()

View file

@ -122,7 +122,7 @@ async def test_data_caching_error_observation(
freezer: FrozenDateTimeFactory,
mock_simple_nws,
no_sensor,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test caching of data with errors."""
instance = mock_simple_nws.return_value
@ -165,7 +165,7 @@ async def test_data_caching_error_observation(
async def test_no_data_error_observation(
hass: HomeAssistant, mock_simple_nws, no_sensor, caplog
hass: HomeAssistant, mock_simple_nws, no_sensor, caplog: pytest.LogCaptureFixture
) -> None:
"""Test catching NwsNoDataDrror."""
instance = mock_simple_nws.return_value
@ -183,7 +183,7 @@ async def test_no_data_error_observation(
async def test_no_data_error_forecast(
hass: HomeAssistant, mock_simple_nws, no_sensor, caplog
hass: HomeAssistant, mock_simple_nws, no_sensor, caplog: pytest.LogCaptureFixture
) -> None:
"""Test catching NwsNoDataDrror."""
instance = mock_simple_nws.return_value
@ -203,7 +203,7 @@ async def test_no_data_error_forecast(
async def test_no_data_error_forecast_hourly(
hass: HomeAssistant, mock_simple_nws, no_sensor, caplog
hass: HomeAssistant, mock_simple_nws, no_sensor, caplog: pytest.LogCaptureFixture
) -> None:
"""Test catching NwsNoDataDrror."""
instance = mock_simple_nws.return_value

View file

@ -20,7 +20,11 @@ from tests.common import MockConfigEntry
],
)
async def test_init_error(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, caplog, side_effect, error
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
caplog: pytest.LogCaptureFixture,
side_effect,
error,
) -> None:
"""Test initialization errors."""
with patch(

View file

@ -179,7 +179,11 @@ async def test_generate_image_service_error(
],
)
async def test_init_error(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, caplog, side_effect, error
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
caplog: pytest.LogCaptureFixture,
side_effect,
error,
) -> None:
"""Test initialization errors."""
with patch(

View file

@ -682,7 +682,7 @@ hass.states.set('hello.c', c)
],
)
async def test_prohibited_augmented_assignment_operations(
hass: HomeAssistant, case: str, error: str, caplog
hass: HomeAssistant, case: str, error: str, caplog: pytest.LogCaptureFixture
) -> None:
"""Test that prohibited augmented assignment operations raise an error."""
hass.async_add_executor_job(execute, hass, "aug_assign_prohibited.py", case, {})

View file

@ -417,7 +417,9 @@ async def test_keepalive(
)
async def test_keepalive_2(hass, monkeypatch, caplog):
async def test_keepalive_2(
hass: HomeAssistant, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""Validate very short keepalive values."""
keepalive_value = 30
domain = RFLINK_DOMAIN
@ -443,7 +445,9 @@ async def test_keepalive_2(hass, monkeypatch, caplog):
)
async def test_keepalive_3(hass, monkeypatch, caplog):
async def test_keepalive_3(
hass: HomeAssistant, monkeypatch, caplog: pytest.LogCaptureFixture
) -> None:
"""Validate keepalive=0 value."""
domain = RFLINK_DOMAIN
config = {

View file

@ -82,7 +82,7 @@ async def test_error_on_setup(
hass: HomeAssistant,
requests_mock: requests_mock.Mocker,
mock_config_entry: MockConfigEntry,
caplog,
caplog: pytest.LogCaptureFixture,
error_type,
log_msg,
) -> None:
@ -111,7 +111,7 @@ async def test_auth_failure_on_global_update(
hass: HomeAssistant,
requests_mock: requests_mock.Mocker,
mock_config_entry: MockConfigEntry,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test authentication failure on global data update."""
mock_config_entry.add_to_hass(hass)
@ -139,7 +139,7 @@ async def test_auth_failure_on_device_update(
hass: HomeAssistant,
requests_mock: requests_mock.Mocker,
mock_config_entry: MockConfigEntry,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test authentication failure on device data update."""
mock_config_entry.add_to_hass(hass)
@ -181,7 +181,7 @@ async def test_error_on_global_update(
hass: HomeAssistant,
requests_mock: requests_mock.Mocker,
mock_config_entry: MockConfigEntry,
caplog,
caplog: pytest.LogCaptureFixture,
error_type,
log_msg,
) -> None:
@ -222,7 +222,7 @@ async def test_error_on_device_update(
hass: HomeAssistant,
requests_mock: requests_mock.Mocker,
mock_config_entry: MockConfigEntry,
caplog,
caplog: pytest.LogCaptureFixture,
error_type,
log_msg,
) -> None:

View file

@ -3,6 +3,7 @@
import logging
from freezegun.api import FrozenDateTimeFactory
import pytest
import requests_mock
from homeassistant.components.ring.const import SCAN_INTERVAL
@ -94,7 +95,7 @@ async def test_only_chime_devices(
hass: HomeAssistant,
requests_mock: requests_mock.Mocker,
freezer: FrozenDateTimeFactory,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Tests the update service works correctly if only chimes are returned."""
await hass.config.async_set_time_zone("UTC")

View file

@ -405,7 +405,9 @@ async def test_disconnected(
@pytest.mark.parametrize(
("error_code", "swallow"), [(ERROR_REQUEST_RETRY, True), (1234, False)]
)
async def test_error_swallowing(hass, caplog, service, error_code, swallow):
async def test_error_swallowing(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, service, error_code, swallow
) -> None:
"""Test swallowing specific errors on turn_on and turn_off."""
mocked_device = _create_mocked_device()
entry = MockConfigEntry(domain=songpal.DOMAIN, data=CONF_DATA)

View file

@ -15,7 +15,9 @@ def calls(hass: HomeAssistant) -> list[ServiceCall]:
@pytest.fixture
async def start_ha(hass, count, domain, config, caplog):
async def start_ha(
hass: HomeAssistant, count, domain, config, caplog: pytest.LogCaptureFixture
):
"""Do setup of integration."""
with assert_setup_component(count, domain):
assert await async_setup_component(
@ -30,6 +32,6 @@ async def start_ha(hass, count, domain, config, caplog):
@pytest.fixture
async def caplog_setup_text(caplog):
async def caplog_setup_text(caplog: pytest.LogCaptureFixture) -> str:
"""Return setup log of integration."""
return caplog.text

View file

@ -213,7 +213,9 @@ async def test_add_bad_dataset(hass: HomeAssistant, dataset, error) -> None:
await dataset_store.async_add_dataset(hass, "test", dataset)
async def test_update_dataset_newer(hass: HomeAssistant, caplog) -> None:
async def test_update_dataset_newer(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test updating a dataset."""
await dataset_store.async_add_dataset(hass, "test", DATASET_1)
await dataset_store.async_add_dataset(hass, "test", DATASET_1_LARGER_TIMESTAMP)
@ -232,7 +234,9 @@ async def test_update_dataset_newer(hass: HomeAssistant, caplog) -> None:
)
async def test_update_dataset_older(hass: HomeAssistant, caplog) -> None:
async def test_update_dataset_older(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test updating a dataset."""
await dataset_store.async_add_dataset(hass, "test", DATASET_1_LARGER_TIMESTAMP)
await dataset_store.async_add_dataset(hass, "test", DATASET_1)
@ -354,7 +358,7 @@ async def test_loading_datasets_from_storage(
async def test_migrate_drop_bad_datasets(
hass: HomeAssistant, hass_storage: dict[str, Any], caplog
hass: HomeAssistant, hass_storage: dict[str, Any], caplog: pytest.LogCaptureFixture
) -> None:
"""Test migrating the dataset store when the store has bad datasets."""
hass_storage[dataset_store.STORAGE_KEY] = {
@ -398,7 +402,7 @@ async def test_migrate_drop_bad_datasets(
async def test_migrate_drop_bad_datasets_preferred(
hass: HomeAssistant, hass_storage: dict[str, Any], caplog
hass: HomeAssistant, hass_storage: dict[str, Any], caplog: pytest.LogCaptureFixture
) -> None:
"""Test migrating the dataset store when the store has bad datasets."""
hass_storage[dataset_store.STORAGE_KEY] = {
@ -429,7 +433,7 @@ async def test_migrate_drop_bad_datasets_preferred(
async def test_migrate_drop_duplicate_datasets(
hass: HomeAssistant, hass_storage: dict[str, Any], caplog
hass: HomeAssistant, hass_storage: dict[str, Any], caplog: pytest.LogCaptureFixture
) -> None:
"""Test migrating the dataset store when the store has duplicated datasets."""
hass_storage[dataset_store.STORAGE_KEY] = {
@ -466,7 +470,7 @@ async def test_migrate_drop_duplicate_datasets(
async def test_migrate_drop_duplicate_datasets_2(
hass: HomeAssistant, hass_storage: dict[str, Any], caplog
hass: HomeAssistant, hass_storage: dict[str, Any], caplog: pytest.LogCaptureFixture
) -> None:
"""Test migrating the dataset store when the store has duplicated datasets."""
hass_storage[dataset_store.STORAGE_KEY] = {
@ -503,7 +507,7 @@ async def test_migrate_drop_duplicate_datasets_2(
async def test_migrate_drop_duplicate_datasets_preferred(
hass: HomeAssistant, hass_storage: dict[str, Any], caplog
hass: HomeAssistant, hass_storage: dict[str, Any], caplog: pytest.LogCaptureFixture
) -> None:
"""Test migrating the dataset store when the store has duplicated datasets."""
hass_storage[dataset_store.STORAGE_KEY] = {
@ -540,7 +544,7 @@ async def test_migrate_drop_duplicate_datasets_preferred(
async def test_migrate_set_default_border_agent_id(
hass: HomeAssistant, hass_storage: dict[str, Any], caplog
hass: HomeAssistant, hass_storage: dict[str, Any], caplog: pytest.LogCaptureFixture
) -> None:
"""Test migrating the dataset store adds default border agent."""
hass_storage[dataset_store.STORAGE_KEY] = {

View file

@ -213,7 +213,7 @@ async def test_config_entry_device_config_invalid(
hass: HomeAssistant,
mock_discovery: AsyncMock,
mock_connect: AsyncMock,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that an invalid device config logs an error and loads the config entry."""
entry_data = copy.deepcopy(CREATE_ENTRY_DATA_AUTH)

View file

@ -839,7 +839,9 @@ async def test_configure_reporting(hass: HomeAssistant, endpoint) -> None:
]
async def test_invalid_cluster_handler(hass: HomeAssistant, caplog) -> None:
async def test_invalid_cluster_handler(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setting up a cluster handler that fails to match properly."""
class TestZigbeeClusterHandler(cluster_handlers.ClusterHandler):
@ -881,7 +883,9 @@ async def test_invalid_cluster_handler(hass: HomeAssistant, caplog) -> None:
assert "missing_attr" in caplog.text
async def test_standard_cluster_handler(hass: HomeAssistant, caplog) -> None:
async def test_standard_cluster_handler(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setting up a cluster handler that matches a standard cluster."""
class TestZigbeeClusterHandler(ColorClusterHandler):
@ -916,7 +920,9 @@ async def test_standard_cluster_handler(hass: HomeAssistant, caplog) -> None:
)
async def test_quirk_id_cluster_handler(hass: HomeAssistant, caplog) -> None:
async def test_quirk_id_cluster_handler(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setting up a cluster handler that matches a standard cluster."""
class TestZigbeeClusterHandler(ColorClusterHandler):

View file

@ -233,7 +233,7 @@ async def test_zha_retry_unique_ids(
config_entry: MockConfigEntry,
zigpy_device_mock,
mock_zigpy_connect: ControllerApplication,
caplog,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that ZHA retrying creates unique entity IDs."""