using sync process

This commit is contained in:
ari committed 2022-08-15 15:43:18 -04:00
1 parent 7aadfbc175
commit b0d924f63a
17 files changed
+384 -496

No files matched your search

+17 -9
View File
@@ -1,5 +1,5 @@
from pathlib import Path
from src.automations_v2.types import ExtenededAutomationData
from src.automations_v2.types import ExtenededAutomation
from .utils import TestWithDB
@@ -17,18 +17,20 @@ class db_tests(TestWithDB):
def test_insert_automations(self):
originals = [
ExtenededAutomationData(
ExtenededAutomation(
id="test",
alias="test",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
configuration_key="automation",
),
ExtenededAutomationData(
ExtenededAutomation(
id="test2",
alias="test",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
configuration_key="automation",
),
]
self.db.upsert_automations(originals)
@@ -40,18 +42,20 @@ class db_tests(TestWithDB):
def test_insert_repeat_automations(self):
originals = [
ExtenededAutomationData(
ExtenededAutomation(
id="test",
alias="test",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
configuration_key="automation",
),
ExtenededAutomationData(
ExtenededAutomation(
id="test",
alias="test",
source_file=Path("automation.yaml").absolute(),
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
configuration_key="automation",
),
]
self.db.upsert_automations(originals)
@@ -61,18 +65,20 @@ class db_tests(TestWithDB):
def test_delete_automations(self):
originals = [
ExtenededAutomationData(
ExtenededAutomation(
id="test",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
configuration_key="automation",
),
ExtenededAutomationData(
ExtenededAutomation(
id="test2",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
configuration_key="automation",
),
]
self.assertEqual(self.db.count_automations(), 0)
@@ -83,18 +89,20 @@ class db_tests(TestWithDB):
def test_delete_automations_by_source_type(self):
originals = [
ExtenededAutomationData(
ExtenededAutomation(
id="test",
alias="test",
source_file="automation2.yaml",
source_file_type="obj",
configuration_key="automation",
),
ExtenededAutomationData(
ExtenededAutomation(
id="test2",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
configuration_key="automation",
),
]
self.assertEqual(self.db.count_automations(), 0)
@@ -3,7 +3,7 @@ from tempfile import mktemp
from unittest import TestCase
from src.automations_v2.loader import extract_automation_paths, load_automation_path
from src.automations_v2.types import ExtenededAutomationData
from src.automations_v2.types import ExtenededAutomation
from src.hass_config.loader import HassConfig
from tests.utils import (
HA_CONFIG2_EXAMPLE,
@@ -17,14 +17,19 @@ from tests.utils import (
class loader_tests(TestCase):
def test_load_automation_list_file(self):
automations = list(load_automation_path(HA_CONFIG2_EXAMPLE / "automations.yaml"))
automations = list(
load_automation_path(
HA_CONFIG2_EXAMPLE / "automations.yaml", configuration_key="automation"
)
)
self.assertEqual(len(automations), 1)
self.assertEqual(
automations[0],
ExtenededAutomationData(
ExtenededAutomation(
id="1652069225859",
source_file=str(HA_CONFIG2_EXAMPLE / "automations.yaml"),
source_file_type="list",
configuration_key="automation",
alias="Climate - Pref temperature ",
description="",
mode="single",
@@ -53,15 +58,19 @@ class loader_tests(TestCase):
def test_load_automation_obj_file(self):
automations = list(
load_automation_path(HA_CONFIG4_EXAMPLE / "automations" / "notify_washer.yaml")
load_automation_path(
HA_CONFIG4_EXAMPLE / "automations" / "notify_washer.yaml",
configuration_key="automation base",
)
)
self.assertEqual(len(automations), 1)
self.assertEqual(
automations[0],
ExtenededAutomationData(
ExtenededAutomation(
id="1659114647067",
source_file=str(HA_CONFIG4_EXAMPLE / "automations" / "notify_washer.yaml"),
source_file_type="obj",
configuration_key="automation base",
alias="Notify Washer",
description="Example",
mode="single",
@@ -85,13 +94,13 @@ class loader_tests(TestCase):
def test_load_automation_empty_file(self):
file_path = Path(mktemp())
file_path.touch()
automations = list(load_automation_path(file_path))
automations = list(load_automation_path(file_path, configuration_key="automation ui"))
self.assertEqual(len(automations), 0)
def test_load_automation_none_existing_file(self):
file_path = Path(mktemp())
file_path.unlink(missing_ok=True)
automations = list(load_automation_path(file_path))
automations = list(load_automation_path(file_path, configuration_key="automation manual"))
self.assertEqual(len(automations), 0)
def test_extract_all_automation_files(self):
@@ -1,19 +1,32 @@
from time import sleep
from pathlib import Path
from src import yaml_serializer
from src.automations_v2.errors import (
AttemptingToOverwriteAnIncompatibleFileError,
DBNoAutomationFound,
InvalidAutomationFile,
)
from src.automations_v2.types import ExtenededAutomation
from src.errors import ErrorSet
from src.hass_config.loader import HassConfig
from src.automations_v2.manager import AutomationManager
from tests.utils import HA_CONFIG6_EXAMPLE, create_copy_of_example_config
from tests.utils import HA_CONFIG6_EXAMPLE, HA_CONFIG_EXAMPLE, create_copy_of_example_config
from .utils import TestWithDB
class manager_tests(TestWithDB):
def test_load_complex_read_modify_delete(self):
config_path = create_copy_of_example_config(HA_CONFIG6_EXAMPLE)
automation_manager = AutomationManager(
HassConfig(config_path),
def get_manager(self, ha_config_example: Path):
self.config_path = create_copy_of_example_config(ha_config_example)
self.automation_manager = AutomationManager(
HassConfig(self.config_path),
self.db_file,
)
self.assertEqual(automation_manager.config_watcher.wait_until_next_event(True), "loaded")
return self.config_path, self.automation_manager
def test_load_complex_read_modify_delete(self):
_, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
self.assertEqual(automation_manager.count(), 0)
automation_manager.reload()
self.assertEqual(automation_manager.count(), 12)
automation = automation_manager.get("sublist3-mlist2")
self.assertEqual(automation.description, "That was correct!")
@@ -27,14 +40,18 @@ class manager_tests(TestWithDB):
}
],
)
self.assertEqual(automation.configuration_key, "automation cools")
automation.trigger.append(
{"platform": "zone", "entity_id": "person.thor", "zone": "zone.azguard"}
)
automation_manager.update(automation)
sleep(2)
automation = automation_manager.get("sublist3-mlist2")
automation_manager.upsert(automation)
self.assertEqual(
automation.trigger,
automation_manager.get("sublist3-mlist2").trigger,
[],
)
automation_manager.reload()
self.assertEqual(
automation_manager.get("sublist3-mlist2").trigger,
[
{
"platform": "zone",
@@ -43,3 +60,191 @@ class manager_tests(TestWithDB):
}
],
)
def test_find_invalid(self):
_, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
automation_manager.reload()
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("invalid")
def test_bad_automations_file(self):
config_path, automation_manager = self.get_manager(HA_CONFIG_EXAMPLE)
(config_path / "automations.yaml").write_text("bob is an uncle")
with self.assertRaises(ErrorSet) as err:
automation_manager.reload()
self.assertEqual(len(err.exception.errors), 1)
self.assertIsInstance(err.exception.errors[0], InvalidAutomationFile)
self.assertEqual(automation_manager.count(), 0)
def test_one_bad_automations_file(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
(config_path / "automations" / "ui.yaml").write_text("bob is an uncle")
with self.assertRaises(ErrorSet) as err:
automation_manager.reload()
self.assertEqual(len(err.exception.errors), 1)
self.assertIsInstance(err.exception.errors[0], InvalidAutomationFile)
self.assertEqual(automation_manager.count(), 10)
def test_delete_automation_in_single_file(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
self.assertTrue((config_path / "automations/include_dir_list/home/down.yaml").exists())
automation_manager.reload()
automation = automation_manager.get("1659114822642")
self.assertEqual(automation.configuration_key, "automation manual")
automation_manager.delete(automation)
automation_manager.reload()
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("1659114822642")
self.assertFalse((config_path / "automations/include_dir_list/home/down.yaml").exists())
def test_delete_automation_in_list(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
self.assertTrue(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").exists()
)
automation_manager.reload()
automation = automation_manager.get("mlist2")
self.assertEqual(automation.configuration_key, "automation cools")
automation_manager.delete(automation)
automation_manager.reload()
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("mlist2")
self.assertTrue(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").exists()
)
def test_create_new_in_single_file(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
automation_manager.reload()
self.assertFalse((config_path / "automations/include_dir_list/home/left.yaml").exists())
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("newlymade")
automation_manager.upsert(
ExtenededAutomation(
id="newlymade",
configuration_key="automation manual",
source_file=config_path / "automations/include_dir_list/home/left.yaml",
source_file_type="obj",
)
)
self.assertTrue((config_path / "automations/include_dir_list/home/left.yaml").exists())
automation_manager.reload()
self.assertEqual(
automation_manager.get("newlymade"),
ExtenededAutomation(
id="newlymade",
configuration_key="automation manual",
source_file=config_path / "automations/include_dir_list/home/left.yaml",
source_file_type="obj",
),
)
def test_create_new_in_existing_list_file(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
automation_manager.reload()
self.assertTrue(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").exists()
)
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("newlymade")
automation_manager.upsert(
ExtenededAutomation(
id="newlymade",
configuration_key="automation cools",
source_file=config_path / "automations/include_dir_merge_list/sub/list2.yaml",
source_file_type="list",
)
)
self.assertTrue(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").exists()
)
automation_manager.reload()
self.assertEqual(
automation_manager.get("newlymade"),
ExtenededAutomation(
id="newlymade",
configuration_key="automation cools",
source_file=config_path / "automations/include_dir_merge_list/sub/list2.yaml",
source_file_type="list",
),
)
def test_create_new_in_new_list_file(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
automation_manager.reload()
self.assertFalse(
(config_path / "automations/include_dir_merge_list/sub/list4.yaml").exists()
)
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("newlymade")
automation_manager.upsert(
ExtenededAutomation(
id="newlymade",
configuration_key="automation cools",
source_file=config_path / "automations/include_dir_merge_list/sub/list4.yaml",
source_file_type="list",
)
)
self.assertTrue(
(config_path / "automations/include_dir_merge_list/sub/list4.yaml").exists()
)
automation_manager.reload()
self.assertEqual(
automation_manager.get("newlymade"),
ExtenededAutomation(
id="newlymade",
configuration_key="automation cools",
source_file=config_path / "automations/include_dir_merge_list/sub/list4.yaml",
source_file_type="list",
),
)
def test_save_improper_types_list_to_obj(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
automation_manager.reload()
self.assertTrue(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").exists()
)
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("newlymade")
with self.assertRaises(AttemptingToOverwriteAnIncompatibleFileError):
automation_manager.upsert(
ExtenededAutomation(
id="newlymade",
configuration_key="automation cools",
source_file=config_path / "automations/include_dir_merge_list/sub/list2.yaml",
source_file_type="obj",
)
)
automation_manager.reload()
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("newlymade")
data = yaml_serializer.load_yaml(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").open("r")
)
self.assertIsInstance(data, list)
def test_save_improper_types_obj_to_list(self):
config_path, automation_manager = self.get_manager(HA_CONFIG6_EXAMPLE)
automation_manager.reload()
self.assertTrue((config_path / "automations/include_dir_list/home/down.yaml").exists())
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("newlymade")
with self.assertRaises(AttemptingToOverwriteAnIncompatibleFileError):
automation_manager.upsert(
ExtenededAutomation(
id="newlymade",
configuration_key="automation cools",
source_file=config_path / "automations/include_dir_list/home/down.yaml",
source_file_type="list",
)
)
automation_manager.reload()
with self.assertRaises(DBNoAutomationFound):
automation_manager.get("newlymade")
data = yaml_serializer.load_yaml(
(config_path / "automations/include_dir_list/home/down.yaml").open("r")
)
self.assertIsInstance(data, dict)
@@ -1,74 +0,0 @@
from pathlib import Path
from time import sleep
from src.automations_v2.watchers.automation import AutomationFileWatcher
from tests.utils import HA_CONFIG6_EXAMPLE, HA_CONFIG_EXAMPLE, create_copy_of_example_config
from .utils import TestWithDB
class automation_watcher_tests(TestWithDB):
def make_safe_watcher(self, automation_file: Path):
self.watcher = AutomationFileWatcher(automation_file, self.db_file)
return self.watcher
def tearDown(self) -> None:
if hasattr(self, "watcher"):
getattr(self, "watcher").join()
super().tearDown()
def test_bad_file(self):
example_folder = create_copy_of_example_config()
watcher = self.make_safe_watcher(example_folder / "automations.yaml")
total = self.db.count_automations()
self.assertEqual(total, 0)
watcher.start()
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 32)
(example_folder / "automations.yaml").write_text("haha!")
watcher.wait_until_next_reload(True, timeout=60 * 10)
self.assertEqual(self.db.count_automations(), 0)
watcher.join()
def test_load_empty_file(self):
example_folder = create_copy_of_example_config()
(example_folder / "automations.yaml").write_text("")
watcher = self.make_safe_watcher(example_folder / "automations.yaml")
total = self.db.count_automations()
self.assertEqual(total, 0)
watcher.start()
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 0)
watcher.join()
def test_watch_folder(self):
example_folder = create_copy_of_example_config(HA_CONFIG6_EXAMPLE)
watcher = self.make_safe_watcher(example_folder / "automations")
watcher.start()
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 12)
watcher.join()
def test_rapid_changes(self):
example_folder = create_copy_of_example_config()
watcher = self.make_safe_watcher(example_folder / "automations.yaml")
watcher.start()
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 32)
(example_folder / "automations.yaml").write_text("")
(example_folder / "automations.yaml").write_text(
(HA_CONFIG_EXAMPLE / "automations.yaml").read_text()
)
(example_folder / "automations.yaml").write_text("")
(example_folder / "automations.yaml").write_text(
(HA_CONFIG_EXAMPLE / "automations.yaml").read_text()
)
(example_folder / "automations.yaml").write_text("")
(example_folder / "automations.yaml").write_text(
(HA_CONFIG_EXAMPLE / "automations.yaml").read_text()
)
sleep(60)
self.assertEqual(self.db.count_automations(), 32)
watcher.join()
@@ -1,38 +0,0 @@
from src.automations_v2.watchers.config import ConfigFileWatcher
from src.hass_config.loader import HassConfig
from .utils import TestWithDB, make_temp_db
from tests.utils import (
HA_CONFIG_EXAMPLE,
HA_CONFIG2_EXAMPLE,
HA_CONFIG3_EXAMPLE,
HA_CONFIG4_EXAMPLE,
HA_CONFIG5_EXAMPLE,
HA_CONFIG6_EXAMPLE,
create_copy_of_example_config,
)
class config_watcher_tests(TestWithDB):
def test_simple_config_loading(self):
for p, count in [
(HA_CONFIG_EXAMPLE, 32),
(HA_CONFIG2_EXAMPLE, 1),
(HA_CONFIG3_EXAMPLE, 1032),
(HA_CONFIG4_EXAMPLE, 3),
(HA_CONFIG5_EXAMPLE, 10),
(HA_CONFIG6_EXAMPLE, 12),
]:
with self.subTest(config=p.name):
db = make_temp_db()
db.reset()
config_path = create_copy_of_example_config(p)
config_watcher = ConfigFileWatcher(
config_path=HassConfig(config_path).get_configuration_path(),
automation_db_path=db.db_file,
)
self.assertEqual(db.count_automations(), 0)
config_watcher.start()
self.assertEqual(config_watcher.wait_until_next_event(True), "loaded")
self.assertEqual(db.count_automations(), count)
config_watcher.join()