Rewrite panel_iframe unittest tests to pytest style tests (#41368)

This commit is contained in:
Alexander Pitkin 2020-10-07 01:40:28 +03:00 committed by GitHub
parent ca59c4a645
commit 5b0282e56d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,100 +1,87 @@
"""The tests for the panel_iframe component.""" """The tests for the panel_iframe component."""
import unittest import pytest
from homeassistant import setup
from homeassistant.components import frontend from homeassistant.components import frontend
from homeassistant.setup import async_setup_component
from tests.async_mock import patch
from tests.common import get_test_home_assistant
class TestPanelIframe(unittest.TestCase): @pytest.mark.parametrize(
"""Test the panel_iframe component.""" "config_to_try",
(
{"invalid space": {"url": "https://home-assistant.io"}},
{"router": {"url": "not-a-url"}},
),
)
async def test_wrong_config(hass, config_to_try):
"""Test setup with wrong configuration."""
assert not await async_setup_component(
hass, "panel_iframe", {"panel_iframe": config_to_try}
)
def setUp(self):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.addCleanup(self.hass.stop)
def test_wrong_config(self): async def test_correct_config(hass):
"""Test setup with wrong configuration.""" """Test correct config."""
to_try = [ assert await async_setup_component(
{"invalid space": {"url": "https://home-assistant.io"}}, hass,
{"router": {"url": "not-a-url"}}, "panel_iframe",
] {
"panel_iframe": {
for conf in to_try: "router": {
with patch( "icon": "mdi:network-wireless",
"homeassistant.components.http.start_http_server_and_save_config" "title": "Router",
): "url": "http://192.168.1.1",
assert not setup.setup_component( "require_admin": True,
self.hass, "panel_iframe", {"panel_iframe": conf}
)
def test_correct_config(self):
"""Test correct config."""
with patch("homeassistant.components.http.start_http_server_and_save_config"):
assert setup.setup_component(
self.hass,
"panel_iframe",
{
"panel_iframe": {
"router": {
"icon": "mdi:network-wireless",
"title": "Router",
"url": "http://192.168.1.1",
"require_admin": True,
},
"weather": {
"icon": "mdi:weather",
"title": "Weather",
"url": "https://www.wunderground.com/us/ca/san-diego",
"require_admin": True,
},
"api": {"icon": "mdi:weather", "title": "Api", "url": "/api"},
"ftp": {
"icon": "mdi:weather",
"title": "FTP",
"url": "ftp://some/ftp",
},
}
}, },
) "weather": {
"icon": "mdi:weather",
"title": "Weather",
"url": "https://www.wunderground.com/us/ca/san-diego",
"require_admin": True,
},
"api": {"icon": "mdi:weather", "title": "Api", "url": "/api"},
"ftp": {
"icon": "mdi:weather",
"title": "FTP",
"url": "ftp://some/ftp",
},
}
},
)
panels = self.hass.data[frontend.DATA_PANELS] panels = hass.data[frontend.DATA_PANELS]
assert panels.get("router").to_response() == { assert panels.get("router").to_response() == {
"component_name": "iframe", "component_name": "iframe",
"config": {"url": "http://192.168.1.1"}, "config": {"url": "http://192.168.1.1"},
"icon": "mdi:network-wireless", "icon": "mdi:network-wireless",
"title": "Router", "title": "Router",
"url_path": "router", "url_path": "router",
"require_admin": True, "require_admin": True,
} }
assert panels.get("weather").to_response() == { assert panels.get("weather").to_response() == {
"component_name": "iframe", "component_name": "iframe",
"config": {"url": "https://www.wunderground.com/us/ca/san-diego"}, "config": {"url": "https://www.wunderground.com/us/ca/san-diego"},
"icon": "mdi:weather", "icon": "mdi:weather",
"title": "Weather", "title": "Weather",
"url_path": "weather", "url_path": "weather",
"require_admin": True, "require_admin": True,
} }
assert panels.get("api").to_response() == { assert panels.get("api").to_response() == {
"component_name": "iframe", "component_name": "iframe",
"config": {"url": "/api"}, "config": {"url": "/api"},
"icon": "mdi:weather", "icon": "mdi:weather",
"title": "Api", "title": "Api",
"url_path": "api", "url_path": "api",
"require_admin": False, "require_admin": False,
} }
assert panels.get("ftp").to_response() == { assert panels.get("ftp").to_response() == {
"component_name": "iframe", "component_name": "iframe",
"config": {"url": "ftp://some/ftp"}, "config": {"url": "ftp://some/ftp"},
"icon": "mdi:weather", "icon": "mdi:weather",
"title": "FTP", "title": "FTP",
"url_path": "ftp", "url_path": "ftp",
"require_admin": False, "require_admin": False,
} }