Fix incorrect type hints in azure_data_explorer tests (#119065)

This commit is contained in:
epenet 2024-06-07 20:07:38 +02:00 committed by GitHub
parent 9008b4295c
commit cd7f2f9f77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 29 deletions

View File

@ -3,7 +3,7 @@
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import Any from typing import Any
from unittest.mock import Mock, patch from unittest.mock import AsyncMock, MagicMock, patch
import pytest import pytest
from typing_extensions import Generator from typing_extensions import Generator
@ -94,7 +94,7 @@ async def mock_entry_with_one_event(
# Fixtures for config_flow tests # Fixtures for config_flow tests
@pytest.fixture @pytest.fixture
def mock_setup_entry() -> Generator[MockConfigEntry]: def mock_setup_entry() -> Generator[AsyncMock]:
"""Mock the setup entry call, used for config flow tests.""" """Mock the setup entry call, used for config flow tests."""
with patch( with patch(
f"{AZURE_DATA_EXPLORER_PATH}.async_setup_entry", return_value=True 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. # Fixtures for mocking the Azure Data Explorer SDK calls.
@pytest.fixture(autouse=True) @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.""" """mock_azure_data_explorer_ManagedStreamingIngestClient_ingest_data."""
with patch( with patch(
"azure.kusto.ingest.ManagedStreamingIngestClient.ingest_from_stream", "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) @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.""" """mock_azure_data_explorer_QueuedIngestClient_ingest_data."""
with patch( with patch(
"azure.kusto.ingest.QueuedIngestClient.ingest_from_stream", "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) @pytest.fixture(autouse=True)
def mock_execute_query() -> Generator[Mock, Any, Any]: def mock_execute_query() -> Generator[MagicMock]:
"""Mock KustoClient execute_query.""" """Mock KustoClient execute_query."""
with patch( with patch(
"azure.kusto.data.KustoClient.execute_query", "azure.kusto.data.KustoClient.execute_query",

View File

@ -1,5 +1,7 @@
"""Test the Azure Data Explorer config flow.""" """Test the Azure Data Explorer config flow."""
from unittest.mock import AsyncMock, MagicMock
from azure.kusto.data.exceptions import KustoAuthenticationError, KustoServiceError from azure.kusto.data.exceptions import KustoAuthenticationError, KustoServiceError
import pytest import pytest
@ -10,7 +12,7 @@ from homeassistant.core import HomeAssistant
from .const import BASE_CONFIG 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.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=None 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( async def test_config_flow_errors(
test_input, test_input: Exception,
expected, expected: str,
hass: HomeAssistant, hass: HomeAssistant,
mock_execute_query, mock_execute_query: MagicMock,
) -> None: ) -> None:
"""Test we handle connection KustoServiceError.""" """Test we handle connection KustoServiceError."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(

View File

@ -2,7 +2,7 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
import logging 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.data.exceptions import KustoAuthenticationError, KustoServiceError
from azure.kusto.ingest import StreamDescriptor 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.freeze_time("2024-01-01 00:00:00")
@pytest.mark.usefixtures("entry_managed")
async def test_put_event_on_queue_with_managed_client( async def test_put_event_on_queue_with_managed_client(
hass: HomeAssistant, hass: HomeAssistant, mock_managed_streaming: Mock
entry_managed,
mock_managed_streaming: Mock,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test listening to events from Hass. and writing to ADX with managed client.""" """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"], ids=["KustoServiceError", "KustoAuthenticationError"],
) )
@pytest.mark.usefixtures("entry_managed")
async def test_put_event_on_queue_with_managed_client_with_errors( async def test_put_event_on_queue_with_managed_client_with_errors(
hass: HomeAssistant, hass: HomeAssistant,
entry_managed,
mock_managed_streaming: Mock, mock_managed_streaming: Mock,
sideeffect, sideeffect: Exception,
log_message, log_message: str,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test listening to events from Hass. and writing to ADX with managed client.""" """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( async def test_put_event_on_queue_with_queueing_client(
hass: HomeAssistant, hass: HomeAssistant,
entry_queued, entry_queued: MockConfigEntry,
mock_queued_ingest: Mock, mock_queued_ingest: Mock,
) -> None: ) -> None:
"""Test listening to events from Hass. and writing to ADX with managed client.""" """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( async def test_unload_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry_managed, entry_managed: MockConfigEntry,
mock_managed_streaming: Mock, mock_managed_streaming: Mock,
) -> None: ) -> None:
"""Test being able to unload an entry. """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") @pytest.mark.freeze_time("2024-01-01 00:00:00")
async def test_late_event( @pytest.mark.usefixtures("entry_with_one_event")
hass: HomeAssistant, async def test_late_event(hass: HomeAssistant, mock_managed_streaming: Mock) -> None:
entry_with_one_event,
mock_managed_streaming: Mock,
) -> None:
"""Test the check on late events.""" """Test the check on late events."""
with patch( with patch(
f"{AZURE_DATA_EXPLORER_PATH}.utcnow", f"{AZURE_DATA_EXPLORER_PATH}.utcnow",
@ -225,8 +220,8 @@ async def test_late_event(
) )
async def test_filter( async def test_filter(
hass: HomeAssistant, hass: HomeAssistant,
entry_managed, entry_managed: MockConfigEntry,
tests, tests: list[FilterTest],
mock_managed_streaming: Mock, mock_managed_streaming: Mock,
) -> None: ) -> None:
"""Test different filters. """Test different filters.
@ -254,9 +249,9 @@ async def test_filter(
) )
async def test_event( async def test_event(
hass: HomeAssistant, hass: HomeAssistant,
entry_managed, entry_managed: MockConfigEntry,
mock_managed_streaming: Mock, mock_managed_streaming: Mock,
event, event: str | None,
) -> None: ) -> None:
"""Test listening to events from Hass. and getting an event with a newline in the state.""" """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"], 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.""" """Test Error when no getting proper connection with Exception."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=azure_data_explorer.DOMAIN, domain=azure_data_explorer.DOMAIN,