config watcher

This commit is contained in:
ari committed 2022-08-14 15:31:42 -04:00
1 parent 586f52ba56
commit 08af01dd11
12 files changed
+316 -124

No files matched your search

@@ -99,9 +99,9 @@ class loader_tests(TestCase):
(HA_CONFIG_EXAMPLE, 1),
(HA_CONFIG2_EXAMPLE, 1),
(HA_CONFIG3_EXAMPLE, 1),
(HA_CONFIG4_EXAMPLE, 3),
(HA_CONFIG5_EXAMPLE, 7),
(HA_CONFIG6_EXAMPLE, 8),
(HA_CONFIG4_EXAMPLE, 1),
(HA_CONFIG5_EXAMPLE, 3),
(HA_CONFIG6_EXAMPLE, 4),
]:
with self.subTest(ha_path=ha_path, expected=expected):
hass_config = HassConfig(ha_path)
@@ -1,9 +1,9 @@
from src.automations_v2.automation_watcher import AutomationFileWatcher
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 watcher_tests(TestWithDB):
class automation_watcher_tests(TestWithDB):
def test_bad_file(self):
example_folder = create_copy_of_example_config()
watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
@@ -12,7 +12,7 @@ class watcher_tests(TestWithDB):
self.assertEqual(total, 0)
watcher.start()
watcher.wait_until_next_reload(True)
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 32)
(example_folder / "automations.yaml").write_text("haha!")
@@ -28,7 +28,7 @@ class watcher_tests(TestWithDB):
self.assertEqual(total, 0)
watcher.start()
watcher.wait_until_next_reload(True)
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 0)
watcher.join()
@@ -36,7 +36,7 @@ class watcher_tests(TestWithDB):
example_folder = create_copy_of_example_config(HA_CONFIG6_EXAMPLE)
watcher = AutomationFileWatcher(example_folder / "automations", self.db_file)
watcher.start()
watcher.wait_until_next_reload(True)
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 12)
watcher.join()
@@ -44,7 +44,7 @@ class watcher_tests(TestWithDB):
example_folder = create_copy_of_example_config()
watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
watcher.start()
watcher.wait_until_next_reload(True)
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(
@@ -58,7 +58,7 @@ class watcher_tests(TestWithDB):
(example_folder / "automations.yaml").write_text(
(HA_CONFIG_EXAMPLE / "automations.yaml").read_text()
)
watcher.wait_until_next_reload(True)
self.assertEqual(watcher.wait_until_next_event(True), "loaded")
self.assertEqual(self.db.count_automations(), 32)
watcher.join()
@@ -70,7 +70,7 @@ class watcher_tests(TestWithDB):
# self.assertEqual(total, 0)
# watcher.start()
# watcher.wait_until_next_reload(True)
# self.assertEqual(watcher.wait_until_next_event(True), 'loaded')
# self.assertEqual(self.db.count_automations(), 32)
# autos = self.db.list_automations(0, 32)
@@ -0,0 +1,37 @@
from src.automations_v2.watchers.config import ConfigFileWatcher
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=config_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()
+6 -2
View File
@@ -5,11 +5,15 @@ from tempfile import mktemp
from src.automations_v2.db import AutomationDBConnection
def make_temp_db():
return AutomationDBConnection(Path(mktemp() + ".db"))
class TestWithDB(TestCase):
def setUp(self) -> None:
self.db_file = Path(mktemp() + ".db")
self.db = AutomationDBConnection(self.db_file)
self.db = make_temp_db()
self.db.create_db()
self.db_file = self.db.db_file
def tearDown(self) -> None:
self.db_file.unlink()