Refactor frame.get_integration_frame (#101322)

This commit is contained in:
Erik Montnemery 2023-10-03 19:21:27 +02:00 committed by GitHub
parent 956098ae3a
commit ab2de18f8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 163 additions and 60 deletions

View file

@ -1,5 +1,6 @@
"""Test the frame helper."""
from collections.abc import Generator
from unittest.mock import Mock, patch
import pytest
@ -7,15 +8,41 @@ import pytest
from homeassistant.helpers import frame
@pytest.fixture
def mock_integration_frame() -> Generator[Mock, None, None]:
"""Mock as if we're calling code from inside an integration."""
correct_frame = Mock(
filename="/home/paulus/homeassistant/components/hue/light.py",
lineno="23",
line="self.light.is_on",
)
with patch(
"homeassistant.helpers.frame.extract_stack",
return_value=[
Mock(
filename="/home/paulus/homeassistant/core.py",
lineno="23",
line="do_something()",
),
correct_frame,
Mock(
filename="/home/paulus/aiohue/lights.py",
lineno="2",
line="something()",
),
],
):
yield correct_frame
async def test_extract_frame_integration(
caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock
) -> None:
"""Test extracting the current frame from integration context."""
found_frame, integration, path = frame.get_integration_frame()
assert integration == "hue"
assert path == "homeassistant/components/"
assert found_frame == mock_integration_frame
integration_frame = frame.get_integration_frame()
assert integration_frame == frame.IntegrationFrame(
False, "homeassistant/components/hue/light.py", mock_integration_frame, "hue"
)
async def test_extract_frame_integration_with_excluded_integration(
@ -48,13 +75,13 @@ async def test_extract_frame_integration_with_excluded_integration(
),
],
):
found_frame, integration, path = frame.get_integration_frame(
integration_frame = frame.get_integration_frame(
exclude_integrations={"zeroconf"}
)
assert integration == "mdns"
assert path == "homeassistant/components/"
assert found_frame == correct_frame
assert integration_frame == frame.IntegrationFrame(
False, "homeassistant/components/mdns/light.py", correct_frame, "mdns"
)
async def test_extract_frame_no_integration(caplog: pytest.LogCaptureFixture) -> None:
@ -77,23 +104,32 @@ async def test_extract_frame_no_integration(caplog: pytest.LogCaptureFixture) ->
frame.get_integration_frame()
@pytest.mark.usefixtures("mock_integration_frame")
@patch.object(frame, "_REPORTED_INTEGRATIONS", set())
async def test_prevent_flooding(caplog: pytest.LogCaptureFixture) -> None:
async def test_prevent_flooding(
caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock
) -> None:
"""Test to ensure a report is only written once to the log."""
what = "accessed hi instead of hello"
key = "/home/paulus/homeassistant/components/hue/light.py:23"
integration = "hue"
filename = "homeassistant/components/hue/light.py"
expected_message = (
f"Detected integration that {what}. Please report issue for {integration} using"
f" this method at {filename}, line "
f"{mock_integration_frame.lineno}: {mock_integration_frame.line}"
)
frame.report(what, error_if_core=False)
assert what in caplog.text
assert expected_message in caplog.text
assert key in frame._REPORTED_INTEGRATIONS
assert len(frame._REPORTED_INTEGRATIONS) == 1
caplog.clear()
frame.report(what, error_if_core=False)
assert what not in caplog.text
assert expected_message not in caplog.text
assert key in frame._REPORTED_INTEGRATIONS
assert len(frame._REPORTED_INTEGRATIONS) == 1