noop yaml loader refactor

This commit is contained in:
ari committed 2022-08-24 10:57:41 -04:00
1 parent 0cf3f2706a
commit 97521097dc
17 files changed
+147 -93

No files matched your search

+5 -1
View File
@@ -6,7 +6,11 @@ from fastapi.testclient import TestClient
from src.api.app import make_app
from src.automations.types import Automation, ExtenededAutomation
from tests.utils import HA_CONFIG2_EXAMPLE, HA_CONFIG6_EXAMPLE, get_example_automation_loader
from tests.utils import (
HA_CONFIG2_EXAMPLE,
HA_CONFIG6_EXAMPLE,
get_example_automation_loader,
)
class BaseTestCase(TestCase):
+2 -1
View File
@@ -1,7 +1,8 @@
from pathlib import Path
from src.automations.types import ExtenededAutomation
from src.automations.errors import DBDataError
from src.automations.types import ExtenededAutomation
from .utils import TestWithDB
@@ -229,7 +229,8 @@ class manager_tests(TestWithDB):
automation_manager.get("newlymade")
data = yaml_serializer.load_yaml(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").open("r")
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").open("r"),
root_path=config_path,
)
self.assertIsInstance(data, list)
@@ -254,7 +255,8 @@ class manager_tests(TestWithDB):
automation_manager.get("newlymade")
data = yaml_serializer.load_yaml(
(config_path / "automations/include_dir_list/home/down.yaml").open("r")
(config_path / "automations/include_dir_list/home/down.yaml").open("r"),
config_path,
)
self.assertIsInstance(data, dict)
+29 -19
View File
@@ -1,11 +1,13 @@
import io
from pathlib import Path
from unittest import TestCase
from src.yaml_serializer import dump_yaml, load_yaml
from src.yaml_serializer import (
IncludedYaml,
IncludedYamlDir,
SecretValue,
dump_yaml,
load_yaml,
)
@@ -14,23 +16,29 @@ class dumping_yamls_tests(TestCase):
yaml = dump_yaml(
{
"automations": IncludedYaml(
"automations.yaml",
Path.cwd() / "automations.yaml",
)
}
},
Path.cwd(),
)
self.assertEqual(yaml, "automations: !include 'automations.yaml'\n")
def test_dumping_secrets(self):
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")})
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")}, Path.cwd())
self.assertEqual(yaml, "google_password: !secret 'gpass'\n")
def test_dumping_include_dir_list(self):
yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml")})
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors/*yaml'\n")
yaml = dump_yaml(
{"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors")}, Path.cwd()
)
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors'\n")
def test_dumping_include_dir_merge_list(self):
yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_merge_list", "sensors/*yaml")})
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors/*yaml'\n")
yaml = dump_yaml(
{"sensor": IncludedYamlDir("include_dir_merge_list", Path.cwd() / "sensors")},
Path.cwd(),
)
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors'\n")
def test_load_complex_automation_setup(self):
yaml_obj = load_yaml(
@@ -41,19 +49,20 @@ class dumping_yamls_tests(TestCase):
automation: !include automations/base.yaml
automation cools: !include_dir_merge_list automations/include_dir_merge_list
"""
)
),
Path.cwd(),
)
self.assertDictEqual(
dict(yaml_obj),
{
"automation manual": IncludedYamlDir(
"include_dir_list", "./automations/include_dir_list"
"include_dir_list", Path.cwd() / "automations/include_dir_list"
),
"automation ui": IncludedYaml("automations/ui.yaml"),
"automation ui": IncludedYaml(Path.cwd() / "automations/ui.yaml"),
"automation cools": IncludedYamlDir(
"include_dir_merge_list", "automations/include_dir_merge_list"
"include_dir_merge_list", Path.cwd() / "automations/include_dir_merge_list"
),
"automation": IncludedYaml("automations/base.yaml"),
"automation": IncludedYaml(Path.cwd() / "automations/base.yaml"),
},
)
@@ -71,10 +80,11 @@ class dumping_yamls_tests(TestCase):
bobIsNull:
bobIsNull2: null
google_password: !secret gpass
sensor: !include_dir_list sensors/*yaml
sensor: !include_dir_list sensors
automations: !include automations.yaml
"""
)
),
Path.cwd(),
)
self.assertDictEqual(
dict(yaml_obj),
@@ -85,12 +95,12 @@ class dumping_yamls_tests(TestCase):
"bobIsNull": None,
"bobIsNull2": None,
"google_password": SecretValue("gpass", "n/a"),
"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"),
"automations": IncludedYaml("automations.yaml"),
"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors"),
"automations": IncludedYaml(Path.cwd() / "automations.yaml"),
},
)
self.assertEqual(
dump_yaml(yaml_obj),
dump_yaml(yaml_obj, Path.cwd()),
"""name: cool
date: '10:00'
bools:
@@ -101,7 +111,7 @@ bools:
bobIsNull: null
bobIsNull2: null
google_password: !secret 'gpass'
sensor: !include_dir_list 'sensors/*yaml'
sensor: !include_dir_list 'sensors'
automations: !include 'automations.yaml'
""",
)
+7 -5
View File
@@ -6,7 +6,7 @@ from typing import List, Literal, Optional, Tuple
from src.automations.manager import AutomationManager
from src.automations.types import BaseAutomation
from src.hass_config.loader import HassConfig
from src.yaml_serializer import dump_yaml, IncludedYaml
from src.yaml_serializer import IncludedYaml, dump_yaml
THIS_FOLDER = Path(__file__).parent
SAMPLES_FOLDER = THIS_FOLDER / "samples"
@@ -96,11 +96,13 @@ def create_dummy_config_folder(
configuration_yaml["automation"] = auto_prims
elif automation_in_conifguration_mode == "include":
configuration_yaml["automation"] = IncludedYaml(
"automations.yaml",
root_folder / "automations.yaml",
)
if automation_in_conifguration_mode != "inline":
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims))
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims, root_path=root_folder))
if secrets is not None:
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets))
(root_folder / "configuration.yaml").write_text(dump_yaml(configuration_yaml))
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets, root_path=root_folder))
(root_folder / "configuration.yaml").write_text(
dump_yaml(configuration_yaml, root_path=root_folder)
)
return root_folder