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(
|
||||
|
||||
Reference in new issue
Block a user