Fix singleton not working with falsey values (#56072)

This commit is contained in:
Paulus Schoutsen 2021-09-11 12:02:01 -07:00 committed by GitHub
parent 6e7ce89c64
commit 8a611eb640
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 60 deletions

View file

@ -12,29 +12,33 @@ def mock_hass():
return Mock(data={})
async def test_singleton_async(mock_hass):
@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 object()
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
def test_singleton(mock_hass):
@pytest.mark.parametrize("result", (object(), {}, []))
def test_singleton(mock_hass, result):
"""Test singleton with function."""
@singleton.singleton("test_key")
def something(hass):
return object()
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