Improve error validation in service tests (#162851)

This commit is contained in:
epenet
2026-02-12 15:34:31 +01:00
committed by GitHub
parent 7d583be8e1
commit 47be13e6bf
13 changed files with 51 additions and 31 deletions

View File

@@ -24,7 +24,7 @@ from homeassistant.components.google_photos.services import (
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_FILENAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from tests.common import MockConfigEntry
@@ -146,7 +146,7 @@ async def test_upload_service_config_entry_not_found(
config_entry: MockConfigEntry,
) -> None:
"""Test upload service call with a config entry that does not exist."""
with pytest.raises(HomeAssistantError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
@@ -158,6 +158,7 @@ async def test_upload_service_config_entry_not_found(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"
@pytest.mark.usefixtures("setup_integration")
@@ -171,7 +172,7 @@ async def test_config_entry_not_loaded(
assert config_entry.state is ConfigEntryState.NOT_LOADED
with pytest.raises(HomeAssistantError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
@@ -183,6 +184,7 @@ async def test_config_entry_not_loaded(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
@pytest.mark.usefixtures("setup_integration")

View File

@@ -368,7 +368,7 @@ async def test_append_sheet_invalid_config_entry(
assert config_entry2.state is ConfigEntryState.LOADED
# Exercise service call on a config entry that does not exist
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
"append_sheet",
@@ -379,13 +379,14 @@ async def test_append_sheet_invalid_config_entry(
},
blocking=True,
)
assert err.value.translation_key == "service_config_entry_not_found"
# Unload the config entry invoke the service on the unloaded entry id
await hass.config_entries.async_unload(config_entry2.entry_id)
await hass.async_block_till_done()
assert config_entry2.state is ConfigEntryState.NOT_LOADED
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
"append_sheet",
@@ -396,6 +397,7 @@ async def test_append_sheet_invalid_config_entry(
},
blocking=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
async def test_get_sheet_invalid_config_entry(
@@ -427,7 +429,7 @@ async def test_get_sheet_invalid_config_entry(
assert config_entry2.state is ConfigEntryState.LOADED
# Exercise service call on a config entry that does not exist
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_GET_SHEET,
@@ -439,6 +441,7 @@ async def test_get_sheet_invalid_config_entry(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"
# Unload the config entry invoke the service on the unloaded entry id
await hass.config_entries.async_unload(config_entry2.entry_id)

View File

@@ -96,7 +96,7 @@ async def test_upload_file_config_entry_not_found(
"""Test upload_file service raising config_entry_not_found."""
await setup_integration(hass, mock_config_entry)
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_UPLOAD_FILE,
@@ -109,6 +109,7 @@ async def test_upload_file_config_entry_not_found(
},
blocking=True,
)
assert err.value.translation_key == "service_config_entry_not_found"
async def test_upload_file_config_entry_not_loaded(
@@ -120,7 +121,7 @@ async def test_upload_file_config_entry_not_loaded(
mock_config_entry.disabled_by = er.RegistryEntryDisabler.USER
await setup_integration(hass, mock_config_entry)
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_UPLOAD_FILE,
@@ -133,6 +134,7 @@ async def test_upload_file_config_entry_not_loaded(
},
blocking=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
async def test_upload_file_only_local_media_supported(

View File

@@ -311,7 +311,7 @@ async def test_service_entry_availability(
payload = {"status": "test toot"}
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_POST,
@@ -319,8 +319,9 @@ async def test_service_entry_availability(
blocking=True,
return_response=False,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_POST,
@@ -328,3 +329,4 @@ async def test_service_entry_availability(
blocking=True,
return_response=False,
)
assert err.value.translation_key == "service_config_entry_not_found"

View File

@@ -542,7 +542,7 @@ async def test_service_entry_availability(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
service,
@@ -550,8 +550,9 @@ async def test_service_entry_availability(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
service,
@@ -559,3 +560,4 @@ async def test_service_entry_availability(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"

View File

@@ -85,7 +85,7 @@ async def test_list_charge_slots_exception(
await setup_integration(hass, mock_config_entry)
# Test error
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_LIST_CHARGE_SLOTS,
@@ -93,3 +93,4 @@ async def test_list_charge_slots_exception(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"

View File

@@ -123,7 +123,7 @@ async def test_upload_service_config_entry_not_found(
) -> None:
"""Test upload service call with a config entry that does not exist."""
await setup_integration(hass, mock_config_entry)
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
@@ -135,6 +135,7 @@ async def test_upload_service_config_entry_not_found(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"
async def test_config_entry_not_loaded(
@@ -148,7 +149,7 @@ async def test_config_entry_not_loaded(
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
UPLOAD_SERVICE,
@@ -160,6 +161,7 @@ async def test_config_entry_not_loaded(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
@pytest.mark.parametrize("upload_file", [MockUploadFile(is_allowed_path=False)])

View File

@@ -135,7 +135,7 @@ async def test_service_entry_availability(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
service,
@@ -143,8 +143,9 @@ async def test_service_entry_availability(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
service,
@@ -152,3 +153,4 @@ async def test_service_entry_availability(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"

View File

@@ -137,7 +137,7 @@ async def test_services_invalid_entry(
# Set up at least one entry so the service gets registered
await setup_integration(hass, aioclient_mock)
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
service,
@@ -145,6 +145,7 @@ async def test_services_invalid_entry(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"
@pytest.mark.parametrize(
@@ -163,7 +164,7 @@ async def test_services_entry_not_loaded(
# Now create a second entry that isn't loaded
unloaded_entry = create_entry(hass)
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
service,
@@ -171,3 +172,4 @@ async def test_services_entry_not_loaded(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"

View File

@@ -61,10 +61,11 @@ async def test_set_time_service_with_invalid_entry(
ATTR_CONFIG_ENTRY_ID: "invalid_entry_id",
}
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True
)
assert err.value.translation_key == "service_config_entry_not_found"
async def test_set_time_service_with_not_loaded_entry(
@@ -80,10 +81,11 @@ async def test_set_time_service_with_not_loaded_entry(
ATTR_CONFIG_ENTRY_ID: local_config_entry.entry_id,
}
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True
)
assert err.value.translation_key == "service_config_entry_not_loaded"
async def test_set_time_service_with_cloud_entry(

View File

@@ -40,7 +40,7 @@ async def test_service_entry_not_loaded(
mock_config_entry2 = MockConfigEntry(domain=DOMAIN)
mock_config_entry2.add_to_hass(hass)
with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
@@ -48,6 +48,7 @@ async def test_service_entry_not_loaded(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
@pytest.mark.usefixtures("init_integration")
@@ -57,7 +58,7 @@ async def test_service_integration_not_found(
mock_config_entry: MockConfigEntry,
) -> None:
"""Test error handling when integration not in registry."""
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
@@ -65,3 +66,4 @@ async def test_service_integration_not_found(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"

View File

@@ -200,9 +200,7 @@ async def test_service_call_load_unload(
await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
with pytest.raises(
ServiceValidationError, match="service_config_entry_not_loaded"
):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
domain=DOMAIN,
service=SERVICE_FETCH_CONNECTIONS,
@@ -212,11 +210,9 @@ async def test_service_call_load_unload(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_loaded"
with pytest.raises(
ServiceValidationError,
match="service_config_entry_not_found",
):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
domain=DOMAIN,
service=SERVICE_FETCH_CONNECTIONS,
@@ -226,3 +222,4 @@ async def test_service_call_load_unload(
blocking=True,
return_response=True,
)
assert err.value.translation_key == "service_config_entry_not_found"

View File

@@ -59,7 +59,7 @@ async def test_service_integration_not_found(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"):
with pytest.raises(ServiceValidationError) as err:
await hass.services.async_call(
DOMAIN,
SERVICE_ADD_TORRENT,
@@ -69,6 +69,7 @@ async def test_service_integration_not_found(
},
blocking=True,
)
assert err.value.translation_key == "service_config_entry_not_found"
@pytest.mark.parametrize(