Rewrite configurator tests to pytest (#41731)

This commit is contained in:
dvermd 2020-10-13 21:42:16 +02:00 committed by GitHub
parent 1bcedd15b3
commit 840e4be029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,117 +1,106 @@
"""The tests for the Configurator component.""" """The tests for the Configurator component."""
# pylint: disable=protected-access
import unittest
import homeassistant.components.configurator as configurator import homeassistant.components.configurator as configurator
from homeassistant.const import ATTR_FRIENDLY_NAME, EVENT_TIME_CHANGED from homeassistant.const import ATTR_FRIENDLY_NAME, EVENT_TIME_CHANGED
from tests.common import get_test_home_assistant
async def test_request_least_info(hass):
"""Test request config with least amount of data."""
request_id = configurator.async_request_config(hass, "Test Request", lambda _: None)
assert 1 == len(
hass.services.async_services().get(configurator.DOMAIN, [])
), "No new service registered"
states = hass.states.async_all()
assert 1 == len(states), "Expected a new state registered"
state = states[0]
assert configurator.STATE_CONFIGURE == state.state
assert request_id == state.attributes.get(configurator.ATTR_CONFIGURE_ID)
class TestConfigurator(unittest.TestCase): async def test_request_all_info(hass):
"""Test the Configurator component.""" """Test request config with all possible info."""
exp_attr = {
# pylint: disable=invalid-name ATTR_FRIENDLY_NAME: "Test Request",
def setUp(self): configurator.ATTR_DESCRIPTION: """config description
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.addCleanup(self.hass.stop)
def test_request_least_info(self):
"""Test request config with least amount of data."""
request_id = configurator.request_config(
self.hass, "Test Request", lambda _: None
)
assert 1 == len(
self.hass.services.services.get(configurator.DOMAIN, [])
), "No new service registered"
states = self.hass.states.all()
assert 1 == len(states), "Expected a new state registered"
state = states[0]
assert configurator.STATE_CONFIGURE == state.state
assert request_id == state.attributes.get(configurator.ATTR_CONFIGURE_ID)
def test_request_all_info(self):
"""Test request config with all possible info."""
exp_attr = {
ATTR_FRIENDLY_NAME: "Test Request",
configurator.ATTR_DESCRIPTION: """config description
[link name](link url) [link name](link url)
![Description image](config image url)""", ![Description image](config image url)""",
configurator.ATTR_SUBMIT_CAPTION: "config submit caption", configurator.ATTR_SUBMIT_CAPTION: "config submit caption",
configurator.ATTR_FIELDS: [], configurator.ATTR_FIELDS: [],
configurator.ATTR_ENTITY_PICTURE: "config entity picture", configurator.ATTR_ENTITY_PICTURE: "config entity picture",
configurator.ATTR_CONFIGURE_ID: configurator.request_config( configurator.ATTR_CONFIGURE_ID: configurator.async_request_config(
self.hass, hass,
name="Test Request", name="Test Request",
callback=lambda _: None, callback=lambda _: None,
description="config description", description="config description",
description_image="config image url", description_image="config image url",
submit_caption="config submit caption", submit_caption="config submit caption",
fields=None, fields=None,
link_name="link name", link_name="link name",
link_url="link url", link_url="link url",
entity_picture="config entity picture", entity_picture="config entity picture",
), ),
} }
states = self.hass.states.all() states = hass.states.async_all()
assert 1 == len(states) assert 1 == len(states)
state = states[0] state = states[0]
assert configurator.STATE_CONFIGURE == state.state assert configurator.STATE_CONFIGURE == state.state
assert exp_attr == state.attributes assert exp_attr == state.attributes
def test_callback_called_on_configure(self):
"""Test if our callback gets called when configure service called."""
calls = []
request_id = configurator.request_config(
self.hass, "Test Request", lambda _: calls.append(1)
)
self.hass.services.call( async def test_callback_called_on_configure(hass):
configurator.DOMAIN, """Test if our callback gets called when configure service called."""
configurator.SERVICE_CONFIGURE, calls = []
{configurator.ATTR_CONFIGURE_ID: request_id}, request_id = configurator.async_request_config(
) hass, "Test Request", lambda _: calls.append(1)
)
self.hass.block_till_done() await hass.services.async_call(
assert 1 == len(calls), "Callback not called" configurator.DOMAIN,
configurator.SERVICE_CONFIGURE,
{configurator.ATTR_CONFIGURE_ID: request_id},
)
def test_state_change_on_notify_errors(self): await hass.async_block_till_done()
"""Test state change on notify errors.""" assert 1 == len(calls), "Callback not called"
request_id = configurator.request_config(
self.hass, "Test Request", lambda _: None
)
error = "Oh no bad bad bad"
configurator.notify_errors(self.hass, request_id, error)
state = self.hass.states.all()[0]
assert error == state.attributes.get(configurator.ATTR_ERRORS)
def test_notify_errors_fail_silently_on_bad_request_id(self): async def test_state_change_on_notify_errors(hass):
"""Test if notify errors fails silently with a bad request id.""" """Test state change on notify errors."""
configurator.notify_errors(self.hass, 2015, "Try this error") request_id = configurator.async_request_config(hass, "Test Request", lambda _: None)
error = "Oh no bad bad bad"
configurator.async_notify_errors(hass, request_id, error)
def test_request_done_works(self): states = hass.states.async_all()
"""Test if calling request done works.""" assert 1 == len(states)
request_id = configurator.request_config( state = states[0]
self.hass, "Test Request", lambda _: None assert error == state.attributes.get(configurator.ATTR_ERRORS)
)
configurator.request_done(self.hass, request_id)
assert 1 == len(self.hass.states.all())
self.hass.bus.fire(EVENT_TIME_CHANGED)
self.hass.block_till_done()
assert 0 == len(self.hass.states.all())
def test_request_done_fail_silently_on_bad_request_id(self): async def test_notify_errors_fail_silently_on_bad_request_id(hass):
"""Test that request_done fails silently with a bad request id.""" """Test if notify errors fails silently with a bad request id."""
configurator.request_done(self.hass, 2016) configurator.async_notify_errors(hass, 2015, "Try this error")
async def test_request_done_works(hass):
"""Test if calling request done works."""
request_id = configurator.async_request_config(hass, "Test Request", lambda _: None)
configurator.async_request_done(hass, request_id)
assert 1 == len(hass.states.async_all())
hass.bus.async_fire(EVENT_TIME_CHANGED)
await hass.async_block_till_done()
assert 0 == len(hass.states.async_all())
async def test_request_done_fail_silently_on_bad_request_id(hass):
"""Test that request_done fails silently with a bad request id."""
configurator.async_request_done(hass, 2016)