Add MockModule type hints in tests (#120007)

This commit is contained in:
epenet 2024-06-21 11:11:48 +02:00 committed by GitHub
parent 0dd5391cd7
commit 7af79ba013
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 21 deletions

View File

@ -783,21 +783,38 @@ class MockModule:
def __init__( def __init__(
self, self,
domain=None, domain: str | None = None,
dependencies=None, *,
setup=None, dependencies: list[str] | None = None,
requirements=None, setup: Callable[[HomeAssistant, ConfigType], bool] | None = None,
config_schema=None, requirements: list[str] | None = None,
platform_schema=None, config_schema: vol.Schema | None = None,
platform_schema_base=None, platform_schema: vol.Schema | None = None,
async_setup=None, platform_schema_base: vol.Schema | None = None,
async_setup_entry=None, async_setup: Callable[[HomeAssistant, ConfigType], Coroutine[Any, Any, bool]]
async_unload_entry=None, | None = None,
async_migrate_entry=None, async_setup_entry: Callable[
async_remove_entry=None, [HomeAssistant, ConfigEntry], Coroutine[Any, Any, bool]
partial_manifest=None, ]
async_remove_config_entry_device=None, | None = None,
): async_unload_entry: Callable[
[HomeAssistant, ConfigEntry], Coroutine[Any, Any, bool]
]
| None = None,
async_migrate_entry: Callable[
[HomeAssistant, ConfigEntry], Coroutine[Any, Any, bool]
]
| None = None,
async_remove_entry: Callable[
[HomeAssistant, ConfigEntry], Coroutine[Any, Any, None]
]
| None = None,
partial_manifest: dict[str, Any] | None = None,
async_remove_config_entry_device: Callable[
[HomeAssistant, ConfigEntry, dr.DeviceEntry], Coroutine[Any, Any, bool]
]
| None = None,
) -> None:
"""Initialize the mock module.""" """Initialize the mock module."""
self.__name__ = f"homeassistant.components.{domain}" self.__name__ = f"homeassistant.components.{domain}"
self.__file__ = f"homeassistant/components/{domain}" self.__file__ = f"homeassistant/components/{domain}"

View File

@ -25,20 +25,20 @@ from .common import MockModule, async_get_persistent_notifications, mock_integra
async def test_circular_component_dependencies(hass: HomeAssistant) -> None: async def test_circular_component_dependencies(hass: HomeAssistant) -> None:
"""Test if we can detect circular dependencies of components.""" """Test if we can detect circular dependencies of components."""
mock_integration(hass, MockModule("mod1")) mock_integration(hass, MockModule("mod1"))
mock_integration(hass, MockModule("mod2", ["mod1"])) mock_integration(hass, MockModule("mod2", dependencies=["mod1"]))
mock_integration(hass, MockModule("mod3", ["mod1"])) mock_integration(hass, MockModule("mod3", dependencies=["mod1"]))
mod_4 = mock_integration(hass, MockModule("mod4", ["mod2", "mod3"])) mod_4 = mock_integration(hass, MockModule("mod4", dependencies=["mod2", "mod3"]))
deps = await loader._async_component_dependencies(hass, mod_4) deps = await loader._async_component_dependencies(hass, mod_4)
assert deps == {"mod1", "mod2", "mod3", "mod4"} assert deps == {"mod1", "mod2", "mod3", "mod4"}
# Create a circular dependency # Create a circular dependency
mock_integration(hass, MockModule("mod1", ["mod4"])) mock_integration(hass, MockModule("mod1", dependencies=["mod4"]))
with pytest.raises(loader.CircularDependency): with pytest.raises(loader.CircularDependency):
await loader._async_component_dependencies(hass, mod_4) await loader._async_component_dependencies(hass, mod_4)
# Create a different circular dependency # Create a different circular dependency
mock_integration(hass, MockModule("mod1", ["mod3"])) mock_integration(hass, MockModule("mod1", dependencies=["mod3"]))
with pytest.raises(loader.CircularDependency): with pytest.raises(loader.CircularDependency):
await loader._async_component_dependencies(hass, mod_4) await loader._async_component_dependencies(hass, mod_4)
@ -59,7 +59,7 @@ async def test_circular_component_dependencies(hass: HomeAssistant) -> None:
async def test_nonexistent_component_dependencies(hass: HomeAssistant) -> None: async def test_nonexistent_component_dependencies(hass: HomeAssistant) -> None:
"""Test if we can detect nonexistent dependencies of components.""" """Test if we can detect nonexistent dependencies of components."""
mod_1 = mock_integration(hass, MockModule("mod1", ["nonexistent"])) mod_1 = mock_integration(hass, MockModule("mod1", dependencies=["nonexistent"]))
with pytest.raises(loader.IntegrationNotFound): with pytest.raises(loader.IntegrationNotFound):
await loader._async_component_dependencies(hass, mod_1) await loader._async_component_dependencies(hass, mod_1)