Remove unittest.TestCase from service helper tests (#89283)

* Remove unittest.TestCase from service helper tests

* Update

* Improve tests
This commit is contained in:
Erik Montnemery 2023-03-07 16:54:35 +01:00 committed by GitHub
parent 058bb4c3e6
commit bc0b3abb01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,6 @@
"""Test service helpers.""" """Test service helpers."""
from collections import OrderedDict from collections import OrderedDict
from copy import deepcopy from copy import deepcopy
import unittest
from unittest.mock import AsyncMock, Mock, patch from unittest.mock import AsyncMock, Mock, patch
import pytest import pytest
@ -33,10 +32,8 @@ from tests.common import (
MockEntity, MockEntity,
MockUser, MockUser,
async_mock_service, async_mock_service,
get_test_home_assistant,
mock_device_registry, mock_device_registry,
mock_registry, mock_registry,
mock_service,
) )
SUPPORT_A = 1 SUPPORT_A = 1
@ -226,20 +223,24 @@ def area_mock(hass):
) )
class TestServiceHelpers(unittest.TestCase): async def test_call_from_config(hass: HomeAssistant):
"""Test the Home Assistant service helpers.""" """Test the sync wrapper of service.async_call_from_config."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = {
"service_template": "{{ 'test_domain.test_service' }}",
"entity_id": "hello.world",
"data": {"hello": "goodbye"},
}
def setUp(self): # pylint: disable=invalid-name await hass.async_add_executor_job(service.call_from_config, hass, config)
"""Set up things to be run when tests are started.""" await hass.async_block_till_done()
self.hass = get_test_home_assistant()
self.calls = mock_service(self.hass, "test_domain", "test_service")
def tearDown(self): # pylint: disable=invalid-name assert calls[0].data == {"hello": "goodbye", "entity_id": ["hello.world"]}
"""Stop down everything that was started."""
self.hass.stop()
def test_service_call(self):
async def test_service_call(hass: HomeAssistant):
"""Test service call with templating.""" """Test service call with templating."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = { config = {
"service": "{{ 'test_domain.test_service' }}", "service": "{{ 'test_domain.test_service' }}",
"entity_id": "hello.world", "entity_id": "hello.world",
@ -251,10 +252,10 @@ class TestServiceHelpers(unittest.TestCase):
"target": {"area_id": "test-area-id", "entity_id": "will.be_overridden"}, "target": {"area_id": "test-area-id", "entity_id": "will.be_overridden"},
} }
service.call_from_config(self.hass, config) await service.async_call_from_config(hass, config)
self.hass.block_till_done() await hass.async_block_till_done()
assert dict(self.calls[0].data) == { assert dict(calls[0].data) == {
"hello": "goodbye", "hello": "goodbye",
"effect": { "effect": {
"value": "complex", "value": "complex",
@ -274,10 +275,10 @@ class TestServiceHelpers(unittest.TestCase):
}, },
} }
service.call_from_config(self.hass, config) await service.async_call_from_config(hass, config)
self.hass.block_till_done() await hass.async_block_till_done()
assert dict(self.calls[1].data) == { assert dict(calls[1].data) == {
"area_id": ["area-42", "area-51"], "area_id": ["area-42", "area-51"],
"device_id": ["abcdef", "fedcba"], "device_id": ["abcdef", "fedcba"],
"entity_id": ["light.static", "light.dynamic"], "entity_id": ["light.static", "light.dynamic"],
@ -288,8 +289,8 @@ class TestServiceHelpers(unittest.TestCase):
"target": "{{ var_target }}", "target": "{{ var_target }}",
} }
service.call_from_config( await service.async_call_from_config(
self.hass, hass,
config, config,
variables={ variables={
"var_target": { "var_target": {
@ -298,82 +299,90 @@ class TestServiceHelpers(unittest.TestCase):
}, },
}, },
) )
await hass.async_block_till_done()
service.call_from_config(self.hass, config) assert dict(calls[2].data) == {
self.hass.block_till_done()
assert dict(self.calls[2].data) == {
"area_id": ["area-42", "area-51"], "area_id": ["area-42", "area-51"],
"entity_id": ["light.static"], "entity_id": ["light.static"],
} }
def test_service_template_service_call(self):
async def test_service_template_service_call(hass: HomeAssistant):
"""Test legacy service_template call with templating.""" """Test legacy service_template call with templating."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = { config = {
"service_template": "{{ 'test_domain.test_service' }}", "service_template": "{{ 'test_domain.test_service' }}",
"entity_id": "hello.world", "entity_id": "hello.world",
"data": {"hello": "goodbye"}, "data": {"hello": "goodbye"},
} }
service.call_from_config(self.hass, config) await service.async_call_from_config(hass, config)
self.hass.block_till_done() await hass.async_block_till_done()
assert self.calls[0].data["hello"] == "goodbye" assert calls[0].data == {"hello": "goodbye", "entity_id": ["hello.world"]}
def test_passing_variables_to_templates(self):
async def test_passing_variables_to_templates(hass: HomeAssistant):
"""Test passing variables to templates.""" """Test passing variables to templates."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = { config = {
"service_template": "{{ var_service }}", "service_template": "{{ var_service }}",
"entity_id": "hello.world", "entity_id": "hello.world",
"data_template": {"hello": "{{ var_data }}"}, "data_template": {"hello": "{{ var_data }}"},
} }
service.call_from_config( await service.async_call_from_config(
self.hass, hass,
config, config,
variables={ variables={
"var_service": "test_domain.test_service", "var_service": "test_domain.test_service",
"var_data": "goodbye", "var_data": "goodbye",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert self.calls[0].data["hello"] == "goodbye" assert calls[0].data == {"hello": "goodbye", "entity_id": ["hello.world"]}
def test_bad_template(self):
async def test_bad_template(hass: HomeAssistant):
"""Test passing bad template.""" """Test passing bad template."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = { config = {
"service_template": "{{ var_service }}", "service_template": "{{ var_service }}",
"entity_id": "hello.world", "entity_id": "hello.world",
"data_template": {"hello": "{{ states + unknown_var }}"}, "data_template": {"hello": "{{ states + unknown_var }}"},
} }
service.call_from_config( await service.async_call_from_config(
self.hass, hass,
config, config,
variables={ variables={
"var_service": "test_domain.test_service", "var_service": "test_domain.test_service",
"var_data": "goodbye", "var_data": "goodbye",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert len(self.calls) == 0 assert len(calls) == 0
def test_split_entity_string(self):
async def test_split_entity_string(hass: HomeAssistant):
"""Test splitting of entity string.""" """Test splitting of entity string."""
service.call_from_config( calls = async_mock_service(hass, "test_domain", "test_service")
self.hass, await service.async_call_from_config(
hass,
{ {
"service": "test_domain.test_service", "service": "test_domain.test_service",
"entity_id": "hello.world, sensor.beer", "entity_id": "hello.world, sensor.beer",
}, },
) )
self.hass.block_till_done() await hass.async_block_till_done()
assert ["hello.world", "sensor.beer"] == self.calls[-1].data.get("entity_id") assert ["hello.world", "sensor.beer"] == calls[-1].data.get("entity_id")
def test_not_mutate_input(self):
async def test_not_mutate_input(hass: HomeAssistant):
"""Test for immutable input.""" """Test for immutable input."""
async_mock_service(hass, "test_domain", "test_service")
config = cv.SERVICE_SCHEMA( config = cv.SERVICE_SCHEMA(
{ {
"service": "test_domain.test_service", "service": "test_domain.test_service",
@ -385,21 +394,22 @@ class TestServiceHelpers(unittest.TestCase):
orig = deepcopy(config) orig = deepcopy(config)
# Only change after call is each template getting hass attached # Only change after call is each template getting hass attached
template.attach(self.hass, orig) template.attach(hass, orig)
service.call_from_config(self.hass, config, validate_config=False) await service.async_call_from_config(hass, config, validate_config=False)
assert orig == config assert orig == config
@patch("homeassistant.helpers.service._LOGGER.error")
def test_fail_silently_if_no_service(self, mock_log): @patch("homeassistant.helpers.service._LOGGER.error")
async def test_fail_silently_if_no_service(mock_log, hass: HomeAssistant):
"""Test failing if service is missing.""" """Test failing if service is missing."""
service.call_from_config(self.hass, None) await service.async_call_from_config(hass, None)
assert mock_log.call_count == 1 assert mock_log.call_count == 1
service.call_from_config(self.hass, {}) await service.async_call_from_config(hass, {})
assert mock_log.call_count == 2 assert mock_log.call_count == 2
service.call_from_config(self.hass, {"service": "invalid"}) await service.async_call_from_config(hass, {"service": "invalid"})
assert mock_log.call_count == 3 assert mock_log.call_count == 3