functioning manager
This commit is contained in:
1 parent
ef5715ea6c
commit
00505c0faa
8 files changed
+239
-54
No files matched your search
@@ -1,23 +1,92 @@
|
||||
from unittest import TestCase
|
||||
from tests.utils import HA_CONFIG_EXAMPLE, create_dummy_config_folder
|
||||
from src.hass_config.loader import HassConfig
|
||||
from src.automations.manager import AutomationManager
|
||||
|
||||
example_hass_config = HassConfig(HA_CONFIG_EXAMPLE)
|
||||
example_automation_loader = AutomationManager(example_hass_config)
|
||||
from src.automations.errors import FailedDeletion, InvalidAutomationFile
|
||||
from src.automations.types import AutomationData, AutomationMetdata
|
||||
from tests.utils import (
|
||||
get_dummy_automation_loader,
|
||||
get_example_automation_loader,
|
||||
)
|
||||
|
||||
|
||||
class manager_tests(TestCase):
|
||||
def test_find_limit_and_get(self):
|
||||
first_auto_with_find = list(example_automation_loader.find(limit=1))
|
||||
first_auto_with_get = example_automation_loader.get(0)
|
||||
_, _, loader = get_example_automation_loader()
|
||||
first_auto_with_find = list(loader.find(limit=1))
|
||||
first_auto_with_get = loader.get(0)
|
||||
self.assertEqual(first_auto_with_find[0], first_auto_with_get)
|
||||
|
||||
def test_find_at_most_with_offset(self):
|
||||
autos = list(example_automation_loader.find(limit=10, offset=10))
|
||||
_, _, loader = get_example_automation_loader()
|
||||
autos = list(loader.find(limit=10, offset=10))
|
||||
self.assertTrue(len(autos), 10)
|
||||
|
||||
def test_find_invalid(self):
|
||||
self.assertIsNone(example_automation_loader.get(-1))
|
||||
self.assertIsNone(example_automation_loader.get(1_000))
|
||||
self.assertIsNone(example_automation_loader.get(31))
|
||||
_, _, loader = get_example_automation_loader()
|
||||
self.assertIsNone(loader.get(-1))
|
||||
self.assertIsNone(loader.get(1_000))
|
||||
self.assertIsNone(loader.get(31))
|
||||
|
||||
def test_length(self):
|
||||
_, _, loader = get_example_automation_loader()
|
||||
self.assertEqual(loader.get_total_items(), 31)
|
||||
|
||||
def test_bad_automations_file(self):
|
||||
folder, _, manager = get_dummy_automation_loader(
|
||||
[],
|
||||
automation_in_conifguration_mode="none",
|
||||
)
|
||||
(folder / "automations.yaml").write_text("bob is an uncle")
|
||||
with self.assertRaises(InvalidAutomationFile):
|
||||
manager.get(0)
|
||||
|
||||
def test_create_automation(self):
|
||||
_, _, manager = get_dummy_automation_loader(
|
||||
[],
|
||||
automation_in_conifguration_mode="none",
|
||||
)
|
||||
manager.update(1, AutomationData(metadata=AutomationMetdata(id="test1")))
|
||||
auto = manager.get(0)
|
||||
self.assertIsNotNone(auto)
|
||||
self.assertEqual(auto.metadata.id, "test1") # type: ignore
|
||||
|
||||
def test_update_automation(self):
|
||||
_, _, manager = get_dummy_automation_loader(
|
||||
[
|
||||
AutomationData(metadata=AutomationMetdata(id="test1")),
|
||||
AutomationData(metadata=AutomationMetdata(id="test2")),
|
||||
AutomationData(metadata=AutomationMetdata(id="test3")),
|
||||
],
|
||||
automation_in_conifguration_mode="none",
|
||||
)
|
||||
auto = manager.get(0)
|
||||
self.assertEqual(auto.metadata.id, "test1") # type: ignore
|
||||
manager.update(0, AutomationData(metadata=AutomationMetdata(id="toaster-7")))
|
||||
auto = manager.get(0)
|
||||
self.assertEqual(auto.metadata.id, "toaster-7") # type: ignore
|
||||
|
||||
def test_delete_automation(self):
|
||||
_, _, manager = get_dummy_automation_loader(
|
||||
[
|
||||
AutomationData(metadata=AutomationMetdata(id="test1")),
|
||||
AutomationData(metadata=AutomationMetdata(id="test2")),
|
||||
AutomationData(metadata=AutomationMetdata(id="test3")),
|
||||
],
|
||||
automation_in_conifguration_mode="none",
|
||||
)
|
||||
self.assertEqual(manager.get_total_items(), 3)
|
||||
manager.delete(1)
|
||||
self.assertEqual(manager.get_total_items(), 2)
|
||||
for auto in manager.find(0, 1000):
|
||||
self.assertNotEqual(auto.metadata.id, "test2")
|
||||
|
||||
def test_failed_deletion_automation(self):
|
||||
_, _, manager = get_dummy_automation_loader(
|
||||
[
|
||||
AutomationData(metadata=AutomationMetdata(id="test1")),
|
||||
AutomationData(metadata=AutomationMetdata(id="test2")),
|
||||
AutomationData(metadata=AutomationMetdata(id="test3")),
|
||||
],
|
||||
automation_in_conifguration_mode="none",
|
||||
)
|
||||
self.assertEqual(manager.get_total_items(), 3)
|
||||
with self.assertRaises(FailedDeletion):
|
||||
manager.delete(10)
|
||||
@@ -221,27 +221,6 @@ class parser_tests(TestCase):
|
||||
for expected, parsed in zip(expected_process, _parse_actions(raw_conditions)):
|
||||
self.assertEqual(parsed, expected)
|
||||
|
||||
# def test_invalid_automations(self):
|
||||
# with self.assertRaises(AutomationLoaderException):
|
||||
# AutomationLoader(
|
||||
# HassConfig(
|
||||
# secrets={},
|
||||
# config={
|
||||
# "automation": "bob",
|
||||
# },
|
||||
# root_config_path=Path("."),
|
||||
# )
|
||||
# )
|
||||
|
||||
# with self.assertRaises(AutomationLoaderException):
|
||||
# AutomationLoader(
|
||||
# HassConfig(
|
||||
# secrets={},
|
||||
# config={},
|
||||
# root_config_path=Path("."),
|
||||
# )
|
||||
# )
|
||||
|
||||
# def test_overwriting_one_auto_in_separate_file(self):
|
||||
# # create dummy data
|
||||
# config_path = create_dummy_config_folder(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from unittest import TestCase
|
||||
from src.automations.types import AutomationData, AutomationMetdata
|
||||
from src.hass_config.errors import MissingFile
|
||||
from src.hass_config.loader import HassConfig
|
||||
from tests.utils import create_dummy_config_folder
|
||||
|
||||
@@ -34,3 +35,14 @@ class loader_tests(TestCase):
|
||||
self.assertIsInstance(hass_config.configurations, dict)
|
||||
self.assertFalse("automation" in hass_config.configurations)
|
||||
self.assertEqual(hass_config.automations[0]["id"], "test")
|
||||
|
||||
def test_missing_automations(self):
|
||||
folder = create_dummy_config_folder(
|
||||
[AutomationData(metadata=AutomationMetdata(id="test"))],
|
||||
automation_in_conifguration_mode="none",
|
||||
)
|
||||
(folder / "automations.yaml").unlink()
|
||||
hass_config = HassConfig(folder)
|
||||
self.assertIsInstance(hass_config.configurations, dict)
|
||||
with self.assertRaises(MissingFile):
|
||||
list(hass_config.automations)
|
||||
+56
-1
@@ -1,7 +1,10 @@
|
||||
from shutil import copytree
|
||||
from tempfile import mkdtemp
|
||||
from pathlib import Path
|
||||
from typing import List, Literal, Optional
|
||||
from typing import List, Literal, Optional, Tuple
|
||||
from src.automations.manager import AutomationManager
|
||||
from src.automations.types import AutomationData
|
||||
from src.hass_config.loader import HassConfig
|
||||
|
||||
from src.yaml_serializer import dump_yaml
|
||||
from src.yaml_serializer.types import IncludedYaml
|
||||
@@ -11,6 +14,58 @@ SAMPLES_FOLDER = THIS_FOLDER / "samples"
|
||||
HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "example_config_folder"
|
||||
|
||||
|
||||
def get_example_automation_loader() -> Tuple[Path, HassConfig, AutomationManager]:
|
||||
"""Creates an example automation loader from the samples folder
|
||||
|
||||
Returns:
|
||||
Tuple[Path, HassConfig, AutomationManager]
|
||||
"""
|
||||
root_folder = create_copy_of_example_config()
|
||||
hass_config = HassConfig(root_folder)
|
||||
automation_loader = AutomationManager(hass_config)
|
||||
return root_folder, hass_config, automation_loader
|
||||
|
||||
|
||||
def create_copy_of_example_config() -> Path:
|
||||
"""Creates a copy of the example config folder
|
||||
|
||||
Returns:
|
||||
Path
|
||||
"""
|
||||
root_folder = Path(mkdtemp())
|
||||
root_folder.rmdir()
|
||||
copytree(HA_CONFIG_EXAMPLE, root_folder)
|
||||
return root_folder
|
||||
|
||||
|
||||
def get_dummy_automation_loader(
|
||||
auto: List[AutomationData],
|
||||
secrets: Optional[dict] = None,
|
||||
other_config: Optional[dict] = None,
|
||||
automation_in_conifguration_mode: Literal["include", "inline", "none"] = "include",
|
||||
) -> Tuple[Path, HassConfig, AutomationManager]:
|
||||
"""Creates a dummy /config structure for testing and return the loader and config
|
||||
|
||||
Args:
|
||||
auto (List[AutomationData]): automations
|
||||
secrets (Optional[dict], optional): dictionary of secrets
|
||||
other_config (Optional[dict], optional): some config stuff to place into configuration.yaml. Defaults to None.
|
||||
automation_in_conifguration_mode (Literal['include', 'inline', 'none'], optional): whether to !include automation as a separete file or in the configuration yaml. Defaults to inline.
|
||||
|
||||
Returns:
|
||||
Path: [description]
|
||||
"""
|
||||
root_folder = create_dummy_config_folder(
|
||||
auto,
|
||||
secrets,
|
||||
other_config,
|
||||
automation_in_conifguration_mode,
|
||||
)
|
||||
hass_config = HassConfig(root_folder)
|
||||
automation_loader = AutomationManager(hass_config)
|
||||
return root_folder, hass_config, automation_loader
|
||||
|
||||
|
||||
def create_dummy_config_folder(
|
||||
auto: List[AutomationData],
|
||||
secrets: Optional[dict] = None,
|
||||
|
||||
Reference in new issue
Block a user