1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-24 18:59:22 +01:00

Add NodeStrClass.__voluptuous_compile__ (#104808)

This commit is contained in:
Erik Montnemery
2023-11-30 17:45:27 +01:00
committed by GitHub
parent b7bf1e9f3f
commit 12902b8a68
2 changed files with 25 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import unittest
from unittest.mock import patch
import pytest
import voluptuous as vol
import yaml as pyyaml
from homeassistant.config import YAML_CONFIG_FILE, load_yaml_config_file
@@ -615,3 +616,20 @@ def test_string_annotated(try_both_loaders) -> None:
getattr(value, "__config_file__", None) == expected_annotations[key][1][0]
)
assert getattr(value, "__line__", None) == expected_annotations[key][1][1]
def test_string_used_as_vol_schema(try_both_loaders) -> None:
"""Test the subclassed strings can be used in voluptuous schemas."""
conf = "wanted_data:\n key_1: value_1\n key_2: value_2\n"
with io.StringIO(conf) as file:
doc = yaml_loader.parse_yaml(file)
# Test using the subclassed strings in a schema
schema = vol.Schema(
{vol.Required(key): value for key, value in doc["wanted_data"].items()},
)
# Test using the subclassed strings when validating a schema
schema(doc["wanted_data"])
schema({"key_1": "value_1", "key_2": "value_2"})
with pytest.raises(vol.Invalid):
schema({"key_1": "value_2", "key_2": "value_1"})