mirror of
https://github.com/home-assistant/core.git
synced 2025-08-14 07:50:01 +00:00
.devcontainer
.github
.vscode
docs
homeassistant
machine
pylint
rootfs
script
tests
auth
backports
components
fixtures
hassfest
helpers
__init__.py
test_aiohttp_client.py
test_area_registry.py
test_check_config.py
test_collection.py
test_condition.py
test_config_entry_flow.py
test_config_entry_oauth2_flow.py
test_config_validation.py
test_debounce.py
test_deprecation.py
test_device_registry.py
test_discovery.py
test_discovery_flow.py
test_dispatcher.py
test_entity.py
test_entity_component.py
test_entity_platform.py
test_entity_registry.py
test_entity_values.py
test_entityfilter.py
test_event.py
test_frame.py
test_httpx_client.py
test_icon.py
test_init.py
test_instance_id.py
test_integration_platform.py
test_intent.py
test_issue_registry.py
test_json.py
test_location.py
test_network.py
test_ratelimit.py
test_recorder.py
test_reload.py
test_restore_state.py
test_schema_config_entry_flow.py
test_script.py
test_script_variables.py
test_selector.py
test_service.py
test_significant_change.py
test_singleton.py
test_start.py
test_state.py
test_storage.py
test_storage_remove.py
test_sun.py
test_system_info.py
test_temperature.py
test_template.py
test_translation.py
test_trigger.py
test_update_coordinator.py
pylint
resources
scripts
test_util
testing_config
util
__init__.py
bandit.yaml
common.py
conftest.py
ignore_uncaught_exceptions.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_exceptions.py
test_loader.py
test_main.py
test_requirements.py
test_runner.py
test_setup.py
test_test_fixtures.py
.core_files.yaml
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hadolint.yaml
.pre-commit-config.yaml
.prettierignore
.readthedocs.yml
.strict-typing
.yamllint
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
Dockerfile.dev
LICENSE.md
MANIFEST.in
README.rst
build.yaml
codecov.yml
mypy.ini
pyproject.toml
requirements.txt
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
requirements_test_pre_commit.txt
setup.cfg
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
"""Test singleton helper."""
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from homeassistant.helpers import singleton
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_hass():
|
|
"""Mock hass fixture."""
|
|
return Mock(data={})
|
|
|
|
|
|
@pytest.mark.parametrize("result", (object(), {}, []))
|
|
async def test_singleton_async(mock_hass, result):
|
|
"""Test singleton with async function."""
|
|
|
|
@singleton.singleton("test_key")
|
|
async def something(hass):
|
|
return result
|
|
|
|
result1 = await something(mock_hass)
|
|
result2 = await something(mock_hass)
|
|
assert result1 is result
|
|
assert result1 is result2
|
|
assert "test_key" in mock_hass.data
|
|
assert mock_hass.data["test_key"] is result1
|
|
|
|
|
|
@pytest.mark.parametrize("result", (object(), {}, []))
|
|
def test_singleton(mock_hass, result):
|
|
"""Test singleton with function."""
|
|
|
|
@singleton.singleton("test_key")
|
|
def something(hass):
|
|
return result
|
|
|
|
result1 = something(mock_hass)
|
|
result2 = something(mock_hass)
|
|
assert result1 is result
|
|
assert result1 is result2
|
|
assert "test_key" in mock_hass.data
|
|
assert mock_hass.data["test_key"] is result1
|