v2 initial work

This commit is contained in:
ari committed 2022-08-14 00:24:44 -04:00
1 parent 2bc7e11bb1
commit 3a89fe5efc
36 files changed
+830 -4

No files matched your search

Whitespace-only changes.
@@ -0,0 +1,81 @@
from src.automations_v2.types import ExtenededAutomationData
from .utils import TestWithDB
class db_tests(TestWithDB):
def test_db_creation(self):
with self.db.get_cur() as cur:
cur.execute("SELECT name FROM sqlite_schema WHERE type ='table'")
tables = cur.fetchall()
self.assertListEqual(tables, [("autos",)])
self.db.create_db()
with self.db.get_cur() as cur:
cur.execute("SELECT name FROM sqlite_schema WHERE type ='table'")
tables = cur.fetchall()
self.assertListEqual(tables, [("autos",)])
def test_insert_automations(self):
originals = [
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
),
ExtenededAutomationData(
id="test2",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
),
]
self.db.upsert_automations(originals)
for original in originals:
with self.subTest(original=original):
self.assertEqual(self.db.get_automation(original.id), original)
autos = list(self.db.list_automations(0, 10))
self.assertEqual(len(autos), 2)
def test_insert_repeat_automations(self):
originals = [
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
),
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
),
]
self.db.upsert_automations(originals)
autos = list(self.db.list_automations(0, 10))
self.assertEqual(len(autos), 1)
self.assertTrue(autos[0] == originals[1])
def test_delete_automations(self):
originals = [
ExtenededAutomationData(
id="test",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
),
ExtenededAutomationData(
id="test2",
alias="test",
source_file="automation.yaml",
source_file_type="obj",
action=[{"event": {"nice": "one"}, "event_type": "custom"}],
),
]
self.assertEqual(self.db.count_automations(), 0)
self.db.upsert_automations(originals)
self.assertEqual(self.db.count_automations(), 2)
self.db.delete_automations(originals)
self.assertEqual(self.db.count_automations(), 0)
Whitespace-only changes.
@@ -0,0 +1,50 @@
from src.automations_v2.watcher import AutomationFileWatcher
from tests.utils import create_copy_of_example_config
from .utils import TestWithDB
class watcher_tests(TestWithDB):
def test_bad_file(self):
example_folder = create_copy_of_example_config()
watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
total = self.db.count_automations()
self.assertEqual(total, 0)
watcher.start()
watcher.wait_until_next_reload(True)
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 = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
total = self.db.count_automations()
self.assertEqual(total, 0)
watcher.start()
watcher.wait_until_next_reload(True)
self.assertEqual(self.db.count_automations(), 0)
# def test_valid_changes(self):
# example_folder = create_copy_of_example_config()
# watcher = AutomationFileWatcher(example_folder / "automations.yaml", self.db_file)
# total = self.db.count_automations()
# self.assertEqual(total, 0)
# watcher.start()
# watcher.wait_until_next_reload(True)
# self.assertEqual(self.db.count_automations(), 32)
# autos = self.db.list_automations(0, 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()
@@ -0,0 +1,15 @@
from pathlib import Path
from unittest import TestCase
from tempfile import mktemp
from src.automations_v2.db import AutomationDBConnection
class TestWithDB(TestCase):
def setUp(self) -> None:
self.db_file = Path(mktemp() + ".db")
self.db = AutomationDBConnection(self.db_file)
self.db.create_db()
def tearDown(self) -> None:
self.db_file.unlink()