mirror of
https://github.com/esphome/esphome.git
synced 2026-10-20 13:06:45 +00:00
42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
"""Tests for display.add_writer."""
|
|
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from esphome import codegen as cg
|
|
from esphome.components import display
|
|
from esphome.const import CONF_LAMBDA
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("kwargs", "ref"),
|
|
[({}, display.DisplayRef), ({"ref": "MyDisplayRef"}, "MyDisplayRef")],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_add_writer_sets_the_lambda(kwargs: dict, ref: object) -> None:
|
|
var = MagicMock(name="var")
|
|
lambda_config = MagicMock(name="lambda")
|
|
with (
|
|
patch.object(cg, "process_lambda", AsyncMock(return_value="fn")) as process,
|
|
patch.object(cg, "add") as add,
|
|
):
|
|
await display.add_writer(var, {CONF_LAMBDA: lambda_config}, **kwargs)
|
|
|
|
process.assert_awaited_once_with(lambda_config, [(ref, "it")], return_type=cg.void)
|
|
var.set_writer.assert_called_once_with("fn")
|
|
add.assert_called_once_with(var.set_writer.return_value)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_add_writer_without_lambda_does_nothing() -> None:
|
|
var = MagicMock(name="var")
|
|
with (
|
|
patch.object(cg, "process_lambda", AsyncMock()) as process,
|
|
patch.object(cg, "add") as add,
|
|
):
|
|
await display.add_writer(var, {})
|
|
|
|
process.assert_not_awaited()
|
|
add.assert_not_called()
|