Files
homeassistant-core/tests/components/generic/conftest.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

120 lines
3.4 KiB
Python
Raw Normal View History

2022-01-02 21:26:44 +00:00
"""Test fixtures for the generic component."""
from io import BytesIO
from unittest.mock import AsyncMock, Mock, patch
2022-01-02 21:26:44 +00:00
from PIL import Image
import pytest
2022-03-28 20:08:00 +01:00
import respx
2022-06-17 06:07:21 +01:00
from homeassistant import config_entries
2022-03-28 20:08:00 +01:00
from homeassistant.components.generic.const import DOMAIN
2022-01-02 21:26:44 +00:00
from tests.common import MockConfigEntry
2022-01-02 21:26:44 +00:00
@pytest.fixture(scope="package")
def fakeimgbytes_png():
"""Fake image in RAM for testing."""
buf = BytesIO()
Image.new("RGB", (1, 1)).save(buf, format="PNG")
2023-01-27 13:57:06 +01:00
return bytes(buf.getbuffer())
2022-01-02 21:26:44 +00:00
@pytest.fixture(scope="package")
def fakeimgbytes_jpg():
"""Fake image in RAM for testing."""
buf = BytesIO() # fake image in ram for testing.
Image.new("RGB", (1, 1)).save(buf, format="jpeg")
2023-01-27 13:57:06 +01:00
return bytes(buf.getbuffer())
2022-01-02 21:26:44 +00:00
@pytest.fixture(scope="package")
def fakeimgbytes_svg():
"""Fake image in RAM for testing."""
2023-01-27 13:57:06 +01:00
return bytes(
2022-01-02 21:26:44 +00:00
'<svg xmlns="http://www.w3.org/2000/svg"><circle r="50"/></svg>',
encoding="utf-8",
)
2022-03-28 20:08:00 +01:00
@pytest.fixture(scope="package")
def fakeimgbytes_gif():
"""Fake image in RAM for testing."""
buf = BytesIO() # fake image in ram for testing.
Image.new("RGB", (1, 1)).save(buf, format="gif")
2023-01-27 13:57:06 +01:00
return bytes(buf.getbuffer())
2022-03-28 20:08:00 +01:00
@pytest.fixture
def fakeimg_png(fakeimgbytes_png):
"""Set up respx to respond to test url with fake image bytes."""
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
@pytest.fixture
def fakeimg_gif(fakeimgbytes_gif):
"""Set up respx to respond to test url with fake image bytes."""
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_gif)
2022-03-28 20:08:00 +01:00
@pytest.fixture(scope="package")
def mock_create_stream():
"""Mock create stream."""
mock_stream = Mock()
mock_provider = Mock()
mock_provider.part_recv = AsyncMock()
mock_provider.part_recv.return_value = True
mock_stream.add_provider.return_value = mock_provider
mock_stream.start = AsyncMock()
mock_stream.stop = AsyncMock()
2024-04-06 11:07:37 +02:00
return patch(
"homeassistant.components.generic.config_flow.create_stream",
return_value=mock_stream,
2022-03-28 20:08:00 +01:00
)
@pytest.fixture
async def user_flow(hass):
"""Initiate a user flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
return result
@pytest.fixture(name="config_entry")
def config_entry_fixture(hass):
"""Define a config entry fixture."""
entry = MockConfigEntry(
domain=DOMAIN,
title="Test Camera",
unique_id="abc123",
data={},
options={
"still_image_url": "http://joebloggs:letmein1@example.com/secret1/file.jpg?pw=qwerty",
"stream_source": "http://janebloggs:letmein2@example.com/stream",
"username": "johnbloggs",
"password": "letmein123",
"limit_refetch_to_url_change": False,
"authentication": "basic",
"framerate": 2.0,
"verify_ssl": True,
"content_type": "image/jpeg",
},
version=1,
)
entry.add_to_hass(hass)
return entry
@pytest.fixture
async def setup_entry(hass, config_entry):
"""Set up a config entry ready to be used in tests."""
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry