From cd7f2f9f7720d827be121d141ec4853e2f772e3a Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 7 Jun 2024 20:07:38 +0200 Subject: [PATCH] Fix incorrect type hints in azure_data_explorer tests (#119065) --- .../azure_data_explorer/conftest.py | 10 ++--- .../azure_data_explorer/test_config_flow.py | 10 +++-- .../azure_data_explorer/test_init.py | 37 +++++++++---------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/tests/components/azure_data_explorer/conftest.py b/tests/components/azure_data_explorer/conftest.py index 28743bec719..4168021b333 100644 --- a/tests/components/azure_data_explorer/conftest.py +++ b/tests/components/azure_data_explorer/conftest.py @@ -3,7 +3,7 @@ from datetime import timedelta import logging from typing import Any -from unittest.mock import Mock, patch +from unittest.mock import AsyncMock, MagicMock, patch import pytest from typing_extensions import Generator @@ -94,7 +94,7 @@ async def mock_entry_with_one_event( # Fixtures for config_flow tests @pytest.fixture -def mock_setup_entry() -> Generator[MockConfigEntry]: +def mock_setup_entry() -> Generator[AsyncMock]: """Mock the setup entry call, used for config flow tests.""" with patch( f"{AZURE_DATA_EXPLORER_PATH}.async_setup_entry", return_value=True @@ -104,7 +104,7 @@ def mock_setup_entry() -> Generator[MockConfigEntry]: # Fixtures for mocking the Azure Data Explorer SDK calls. @pytest.fixture(autouse=True) -def mock_managed_streaming() -> Generator[mock_entry_fixture_managed, Any, Any]: +def mock_managed_streaming() -> Generator[MagicMock]: """mock_azure_data_explorer_ManagedStreamingIngestClient_ingest_data.""" with patch( "azure.kusto.ingest.ManagedStreamingIngestClient.ingest_from_stream", @@ -114,7 +114,7 @@ def mock_managed_streaming() -> Generator[mock_entry_fixture_managed, Any, Any]: @pytest.fixture(autouse=True) -def mock_queued_ingest() -> Generator[mock_entry_fixture_queued, Any, Any]: +def mock_queued_ingest() -> Generator[MagicMock]: """mock_azure_data_explorer_QueuedIngestClient_ingest_data.""" with patch( "azure.kusto.ingest.QueuedIngestClient.ingest_from_stream", @@ -124,7 +124,7 @@ def mock_queued_ingest() -> Generator[mock_entry_fixture_queued, Any, Any]: @pytest.fixture(autouse=True) -def mock_execute_query() -> Generator[Mock, Any, Any]: +def mock_execute_query() -> Generator[MagicMock]: """Mock KustoClient execute_query.""" with patch( "azure.kusto.data.KustoClient.execute_query", diff --git a/tests/components/azure_data_explorer/test_config_flow.py b/tests/components/azure_data_explorer/test_config_flow.py index 5c9fe6506fa..a700299be33 100644 --- a/tests/components/azure_data_explorer/test_config_flow.py +++ b/tests/components/azure_data_explorer/test_config_flow.py @@ -1,5 +1,7 @@ """Test the Azure Data Explorer config flow.""" +from unittest.mock import AsyncMock, MagicMock + from azure.kusto.data.exceptions import KustoAuthenticationError, KustoServiceError import pytest @@ -10,7 +12,7 @@ from homeassistant.core import HomeAssistant from .const import BASE_CONFIG -async def test_config_flow(hass, mock_setup_entry) -> None: +async def test_config_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, data=None @@ -36,10 +38,10 @@ async def test_config_flow(hass, mock_setup_entry) -> None: ], ) async def test_config_flow_errors( - test_input, - expected, + test_input: Exception, + expected: str, hass: HomeAssistant, - mock_execute_query, + mock_execute_query: MagicMock, ) -> None: """Test we handle connection KustoServiceError.""" result = await hass.config_entries.flow.async_init( diff --git a/tests/components/azure_data_explorer/test_init.py b/tests/components/azure_data_explorer/test_init.py index dcafcfce500..4d339728d09 100644 --- a/tests/components/azure_data_explorer/test_init.py +++ b/tests/components/azure_data_explorer/test_init.py @@ -2,7 +2,7 @@ from datetime import datetime, timedelta import logging -from unittest.mock import Mock, patch +from unittest.mock import MagicMock, Mock, patch from azure.kusto.data.exceptions import KustoAuthenticationError, KustoServiceError from azure.kusto.ingest import StreamDescriptor @@ -28,11 +28,9 @@ _LOGGER = logging.getLogger(__name__) @pytest.mark.freeze_time("2024-01-01 00:00:00") +@pytest.mark.usefixtures("entry_managed") async def test_put_event_on_queue_with_managed_client( - hass: HomeAssistant, - entry_managed, - mock_managed_streaming: Mock, - caplog: pytest.LogCaptureFixture, + hass: HomeAssistant, mock_managed_streaming: Mock ) -> None: """Test listening to events from Hass. and writing to ADX with managed client.""" @@ -59,12 +57,12 @@ async def test_put_event_on_queue_with_managed_client( ], ids=["KustoServiceError", "KustoAuthenticationError"], ) +@pytest.mark.usefixtures("entry_managed") async def test_put_event_on_queue_with_managed_client_with_errors( hass: HomeAssistant, - entry_managed, mock_managed_streaming: Mock, - sideeffect, - log_message, + sideeffect: Exception, + log_message: str, caplog: pytest.LogCaptureFixture, ) -> None: """Test listening to events from Hass. and writing to ADX with managed client.""" @@ -83,7 +81,7 @@ async def test_put_event_on_queue_with_managed_client_with_errors( async def test_put_event_on_queue_with_queueing_client( hass: HomeAssistant, - entry_queued, + entry_queued: MockConfigEntry, mock_queued_ingest: Mock, ) -> None: """Test listening to events from Hass. and writing to ADX with managed client.""" @@ -124,7 +122,7 @@ async def test_import(hass: HomeAssistant) -> None: async def test_unload_entry( hass: HomeAssistant, - entry_managed, + entry_managed: MockConfigEntry, mock_managed_streaming: Mock, ) -> None: """Test being able to unload an entry. @@ -140,11 +138,8 @@ async def test_unload_entry( @pytest.mark.freeze_time("2024-01-01 00:00:00") -async def test_late_event( - hass: HomeAssistant, - entry_with_one_event, - mock_managed_streaming: Mock, -) -> None: +@pytest.mark.usefixtures("entry_with_one_event") +async def test_late_event(hass: HomeAssistant, mock_managed_streaming: Mock) -> None: """Test the check on late events.""" with patch( f"{AZURE_DATA_EXPLORER_PATH}.utcnow", @@ -225,8 +220,8 @@ async def test_late_event( ) async def test_filter( hass: HomeAssistant, - entry_managed, - tests, + entry_managed: MockConfigEntry, + tests: list[FilterTest], mock_managed_streaming: Mock, ) -> None: """Test different filters. @@ -254,9 +249,9 @@ async def test_filter( ) async def test_event( hass: HomeAssistant, - entry_managed, + entry_managed: MockConfigEntry, mock_managed_streaming: Mock, - event, + event: str | None, ) -> None: """Test listening to events from Hass. and getting an event with a newline in the state.""" @@ -279,7 +274,9 @@ async def test_event( ], ids=["KustoServiceError", "KustoAuthenticationError", "Exception"], ) -async def test_connection(hass, mock_execute_query, sideeffect) -> None: +async def test_connection( + hass: HomeAssistant, mock_execute_query: MagicMock, sideeffect: Exception +) -> None: """Test Error when no getting proper connection with Exception.""" entry = MockConfigEntry( domain=azure_data_explorer.DOMAIN,