From b15989f2bfa621c148a96696e89820e71ca6c2dd Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 26 May 2025 19:39:11 +0200 Subject: [PATCH] Make tests less dependent on issue registry size (#145631) * Make tests less dependent on issue registry size * Make tests less dependent on issue registry size --- tests/components/camera/test_init.py | 6 +-- tests/components/esphome/test_repairs.py | 6 +-- tests/components/group/test_sensor.py | 6 ++- .../components/homeassistant/test_repairs.py | 44 ++++++------------- tests/components/lcn/test_binary_sensor.py | 2 - .../smartthings/test_binary_sensor.py | 4 -- tests/components/smartthings/test_sensor.py | 4 -- 7 files changed, 23 insertions(+), 49 deletions(-) diff --git a/tests/components/camera/test_init.py b/tests/components/camera/test_init.py index 2348ca58673..7c56d142920 100644 --- a/tests/components/camera/test_init.py +++ b/tests/components/camera/test_init.py @@ -237,6 +237,7 @@ async def test_snapshot_service( expected_filename: str, expected_issues: list, snapshot: SnapshotAssertion, + issue_registry: ir.IssueRegistry, ) -> None: """Test snapshot service.""" mopen = mock_open() @@ -265,8 +266,6 @@ async def test_snapshot_service( assert len(mock_write.mock_calls) == 1 assert mock_write.mock_calls[0][1][0] == b"Test" - issue_registry = ir.async_get(hass) - assert len(issue_registry.issues) == 1 + len(expected_issues) for expected_issue in expected_issues: issue = issue_registry.async_get_issue(DOMAIN, expected_issue) assert issue is not None @@ -638,6 +637,7 @@ async def test_record_service( expected_filename: str, expected_issues: list, snapshot: SnapshotAssertion, + issue_registry: ir.IssueRegistry, ) -> None: """Test record service.""" with ( @@ -666,8 +666,6 @@ async def test_record_service( ANY, expected_filename, duration=30, lookback=0 ) - issue_registry = ir.async_get(hass) - assert len(issue_registry.issues) == 1 + len(expected_issues) for expected_issue in expected_issues: issue = issue_registry.async_get_issue(DOMAIN, expected_issue) assert issue is not None diff --git a/tests/components/esphome/test_repairs.py b/tests/components/esphome/test_repairs.py index 268b30f8b52..692a7dd9cc9 100644 --- a/tests/components/esphome/test_repairs.py +++ b/tests/components/esphome/test_repairs.py @@ -70,8 +70,7 @@ async def test_device_conflict_manual( issues = await get_repairs(hass, hass_ws_client) assert issues - assert len(issues) == 1 - assert any(True for issue in issues if issue["issue_id"] == issue_id) + assert issue_registry.async_get_issue(DOMAIN, issue_id) is not None await async_process_repairs_platforms(hass) client = await hass_client() @@ -182,8 +181,7 @@ async def test_device_conflict_migration( issues = await get_repairs(hass, hass_ws_client) assert issues - assert len(issues) == 1 - assert any(True for issue in issues if issue["issue_id"] == issue_id) + assert issue_registry.async_get_issue(DOMAIN, issue_id) is not None await async_process_repairs_platforms(hass) client = await hass_client() diff --git a/tests/components/group/test_sensor.py b/tests/components/group/test_sensor.py index 187991141e7..de48c711587 100644 --- a/tests/components/group/test_sensor.py +++ b/tests/components/group/test_sensor.py @@ -481,7 +481,11 @@ async def test_sensor_with_uoms_but_no_device_class( assert state.attributes.get("unit_of_measurement") == "W" assert state.state == str(float(sum(VALUES))) - assert not issue_registry.issues + assert not [ + issue + for issue in issue_registry.issues.values() + if issue.domain == GROUP_DOMAIN + ] hass.states.async_set( entity_ids[0], diff --git a/tests/components/homeassistant/test_repairs.py b/tests/components/homeassistant/test_repairs.py index f84b29d8d2d..d9329744694 100644 --- a/tests/components/homeassistant/test_repairs.py +++ b/tests/components/homeassistant/test_repairs.py @@ -2,6 +2,7 @@ from homeassistant.components.repairs import DOMAIN as REPAIRS_DOMAIN from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant +from homeassistant.helpers import issue_registry as ir from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry @@ -10,13 +11,13 @@ from tests.components.repairs import ( process_repair_fix_flow, start_repair_fix_flow, ) -from tests.typing import ClientSessionGenerator, WebSocketGenerator +from tests.typing import ClientSessionGenerator async def test_integration_not_found_confirm_step( hass: HomeAssistant, hass_client: ClientSessionGenerator, - hass_ws_client: WebSocketGenerator, + issue_registry: ir.IssueRegistry, ) -> None: """Test the integration_not_found issue confirm step.""" assert await async_setup_component(hass, HOMEASSISTANT_DOMAIN, {}) @@ -33,17 +34,11 @@ async def test_integration_not_found_confirm_step( issue_id = "integration_not_found.test1" await async_process_repairs_platforms(hass) - ws_client = await hass_ws_client(hass) http_client = await hass_client() - # Assert the issue is present - await ws_client.send_json({"id": 1, "type": "repairs/list_issues"}) - msg = await ws_client.receive_json() - assert msg["success"] - assert len(msg["result"]["issues"]) == 1 - issue = msg["result"]["issues"][0] - assert issue["issue_id"] == issue_id - assert issue["translation_placeholders"] == {"domain": "test1"} + issue = issue_registry.async_get_issue(HOMEASSISTANT_DOMAIN, issue_id) + assert issue is not None + assert issue.translation_placeholders == {"domain": "test1"} data = await start_repair_fix_flow(http_client, HOMEASSISTANT_DOMAIN, issue_id) @@ -68,16 +63,13 @@ async def test_integration_not_found_confirm_step( assert hass.config_entries.async_get_entry(entry2.entry_id) is None # Assert the issue is resolved - await ws_client.send_json({"id": 2, "type": "repairs/list_issues"}) - msg = await ws_client.receive_json() - assert msg["success"] - assert len(msg["result"]["issues"]) == 0 + assert not issue_registry.async_get_issue(HOMEASSISTANT_DOMAIN, issue_id) async def test_integration_not_found_ignore_step( hass: HomeAssistant, hass_client: ClientSessionGenerator, - hass_ws_client: WebSocketGenerator, + issue_registry: ir.IssueRegistry, ) -> None: """Test the integration_not_found issue ignore step.""" assert await async_setup_component(hass, HOMEASSISTANT_DOMAIN, {}) @@ -92,17 +84,11 @@ async def test_integration_not_found_ignore_step( issue_id = "integration_not_found.test1" await async_process_repairs_platforms(hass) - ws_client = await hass_ws_client(hass) http_client = await hass_client() - # Assert the issue is present - await ws_client.send_json({"id": 1, "type": "repairs/list_issues"}) - msg = await ws_client.receive_json() - assert msg["success"] - assert len(msg["result"]["issues"]) == 1 - issue = msg["result"]["issues"][0] - assert issue["issue_id"] == issue_id - assert issue["translation_placeholders"] == {"domain": "test1"} + issue = issue_registry.async_get_issue(HOMEASSISTANT_DOMAIN, issue_id) + assert issue is not None + assert issue.translation_placeholders == {"domain": "test1"} data = await start_repair_fix_flow(http_client, HOMEASSISTANT_DOMAIN, issue_id) @@ -128,8 +114,6 @@ async def test_integration_not_found_ignore_step( assert hass.config_entries.async_get_entry(entry1.entry_id) # Assert the issue is resolved - await ws_client.send_json({"id": 2, "type": "repairs/list_issues"}) - msg = await ws_client.receive_json() - assert msg["success"] - assert len(msg["result"]["issues"]) == 1 - assert msg["result"]["issues"][0].get("dismissed_version") is not None + issue = issue_registry.async_get_issue(HOMEASSISTANT_DOMAIN, issue_id) + assert issue is not None + assert issue.dismissed_version is not None diff --git a/tests/components/lcn/test_binary_sensor.py b/tests/components/lcn/test_binary_sensor.py index 7e828dbc588..b9362dcd242 100644 --- a/tests/components/lcn/test_binary_sensor.py +++ b/tests/components/lcn/test_binary_sensor.py @@ -190,5 +190,3 @@ async def test_create_issue( assert issue_registry.async_get_issue( DOMAIN, f"deprecated_binary_sensor_{entity_id}" ) - - assert len(issue_registry.issues) == 1 diff --git a/tests/components/smartthings/test_binary_sensor.py b/tests/components/smartthings/test_binary_sensor.py index 45643f80d2c..ab9531bbef6 100644 --- a/tests/components/smartthings/test_binary_sensor.py +++ b/tests/components/smartthings/test_binary_sensor.py @@ -190,7 +190,6 @@ async def test_create_issue_with_items( assert automations_with_entity(hass, entity_id)[0] == "automation.test" assert scripts_with_entity(hass, entity_id)[0] == "script.test" - assert len(issue_registry.issues) == 1 issue = issue_registry.async_get_issue(DOMAIN, issue_id) assert issue is not None assert issue.translation_key == f"deprecated_binary_{issue_string}_scripts" @@ -210,7 +209,6 @@ async def test_create_issue_with_items( # Assert the issue is no longer present assert not issue_registry.async_get_issue(DOMAIN, issue_id) - assert len(issue_registry.issues) == 0 @pytest.mark.parametrize( @@ -258,7 +256,6 @@ async def test_create_issue( assert hass.states.get(entity_id).state == STATE_OFF - assert len(issue_registry.issues) == 1 issue = issue_registry.async_get_issue(DOMAIN, issue_id) assert issue is not None assert issue.translation_key == f"deprecated_binary_{issue_string}" @@ -277,4 +274,3 @@ async def test_create_issue( # Assert the issue is no longer present assert not issue_registry.async_get_issue(DOMAIN, issue_id) - assert len(issue_registry.issues) == 0 diff --git a/tests/components/smartthings/test_sensor.py b/tests/components/smartthings/test_sensor.py index bfb203c1485..a004dec214a 100644 --- a/tests/components/smartthings/test_sensor.py +++ b/tests/components/smartthings/test_sensor.py @@ -205,7 +205,6 @@ async def test_create_issue_with_items( assert automations_with_entity(hass, entity_id)[0] == "automation.test" assert scripts_with_entity(hass, entity_id)[0] == "script.test" - assert len(issue_registry.issues) == 1 issue = issue_registry.async_get_issue(DOMAIN, issue_id) assert issue is not None assert issue.translation_key == f"deprecated_{issue_string}_scripts" @@ -226,7 +225,6 @@ async def test_create_issue_with_items( # Assert the issue is no longer present assert not issue_registry.async_get_issue(DOMAIN, issue_id) - assert len(issue_registry.issues) == 0 @pytest.mark.parametrize( @@ -333,7 +331,6 @@ async def test_create_issue( assert hass.states.get(entity_id).state == expected_state - assert len(issue_registry.issues) == 1 issue = issue_registry.async_get_issue(DOMAIN, issue_id) assert issue is not None assert issue.translation_key == f"deprecated_{issue_string}" @@ -353,7 +350,6 @@ async def test_create_issue( # Assert the issue is no longer present assert not issue_registry.async_get_issue(DOMAIN, issue_id) - assert len(issue_registry.issues) == 0 @pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])