Use config flow in color extractor tests (#101524)

pull/101547/head
Joost Lekkerkerker 2023-10-06 12:02:53 +02:00 committed by Franck Nijhof
parent 7f6506cfcf
commit 81f582eeb7
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
5 changed files with 57 additions and 27 deletions

View File

@ -0,0 +1,21 @@
"""Common fixtures for the Color extractor tests."""
import pytest
from homeassistant.components.color_extractor.const import DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.fixture
async def config_entry() -> MockConfigEntry:
"""Mock config entry."""
return MockConfigEntry(domain=DOMAIN, data={})
@pytest.fixture
async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Add config entry for color extractor."""
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

View File

@ -0,0 +1,17 @@
"""Test Color extractor component setup process."""
from homeassistant.components.color_extractor import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
async def test_legacy_migration(hass: HomeAssistant) -> None:
"""Test migration from yaml to config flow."""
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].state is ConfigEntryState.LOADED
issue_registry = ir.async_get(hass)
assert len(issue_registry.issues) == 1

View File

@ -1,6 +1,7 @@
"""Tests for color_extractor component service calls."""
import base64
import io
from typing import Any
from unittest.mock import Mock, mock_open, patch
import aiohttp
@ -92,15 +93,8 @@ async def setup_light(hass: HomeAssistant):
assert state.state == STATE_OFF
async def test_missing_url_and_path(hass: HomeAssistant) -> None:
async def test_missing_url_and_path(hass: HomeAssistant, setup_integration) -> None:
"""Test that nothing happens when url and path are missing."""
# Load our color_extractor component
await async_setup_component(
hass,
DOMAIN,
{},
)
await hass.async_block_till_done()
# Validate pre service call
state = hass.states.get(LIGHT_ENTITY)
@ -124,15 +118,7 @@ async def test_missing_url_and_path(hass: HomeAssistant) -> None:
assert state.state == STATE_OFF
async def _async_load_color_extractor_url(hass, service_data):
# Load our color_extractor component
await async_setup_component(
hass,
DOMAIN,
{},
)
await hass.async_block_till_done()
async def _async_execute_service(hass: HomeAssistant, service_data: dict[str, Any]):
# Validate pre service call
state = hass.states.get(LIGHT_ENTITY)
assert state
@ -145,7 +131,7 @@ async def _async_load_color_extractor_url(hass, service_data):
async def test_url_success(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, setup_integration
) -> None:
"""Test that a successful image GET translate to light RGB."""
service_data = {
@ -158,13 +144,15 @@ async def test_url_success(
# Mock the HTTP Response with a base64 encoded 1x1 pixel
aioclient_mock.get(
url=service_data[ATTR_URL],
content=base64.b64decode(load_fixture("color_extractor_url.txt")),
content=base64.b64decode(
load_fixture("color_extractor/color_extractor_url.txt")
),
)
# Allow access to this URL using the proper mechanism
hass.config.allowlist_external_urls.add("http://example.com/images/")
await _async_load_color_extractor_url(hass, service_data)
await _async_execute_service(hass, service_data)
state = hass.states.get(LIGHT_ENTITY)
assert state
@ -180,7 +168,7 @@ async def test_url_success(
async def test_url_not_allowed(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, setup_integration
) -> None:
"""Test that a not allowed external URL fails to turn light on."""
service_data = {
@ -188,7 +176,7 @@ async def test_url_not_allowed(
ATTR_ENTITY_ID: LIGHT_ENTITY,
}
await _async_load_color_extractor_url(hass, service_data)
await _async_execute_service(hass, service_data)
# Light has not been modified due to failure
state = hass.states.get(LIGHT_ENTITY)
@ -197,7 +185,7 @@ async def test_url_not_allowed(
async def test_url_exception(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, setup_integration
) -> None:
"""Test that a HTTPError fails to turn light on."""
service_data = {
@ -211,7 +199,7 @@ async def test_url_exception(
# Mock the HTTP Response with an HTTPError
aioclient_mock.get(url=service_data[ATTR_URL], exc=aiohttp.ClientError)
await _async_load_color_extractor_url(hass, service_data)
await _async_execute_service(hass, service_data)
# Light has not been modified due to failure
state = hass.states.get(LIGHT_ENTITY)
@ -220,7 +208,7 @@ async def test_url_exception(
async def test_url_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, setup_integration
) -> None:
"""Test that a HTTP Error (non 200) doesn't turn light on."""
service_data = {
@ -234,7 +222,7 @@ async def test_url_error(
# Mock the HTTP Response with a 400 Bad Request error
aioclient_mock.get(url=service_data[ATTR_URL], status=400)
await _async_load_color_extractor_url(hass, service_data)
await _async_execute_service(hass, service_data)
# Light has not been modified due to failure
state = hass.states.get(LIGHT_ENTITY)
@ -244,7 +232,11 @@ async def test_url_error(
@patch(
"builtins.open",
mock_open(read_data=base64.b64decode(load_fixture("color_extractor_file.txt"))),
mock_open(
read_data=base64.b64decode(
load_fixture("color_extractor/color_extractor_file.txt")
)
),
create=True,
)
def _get_file_mock(file_path):