diff --git a/.vscode/settings.json b/.vscode/settings.json index 163c984..703af2a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,12 @@ { - "python.formatting.provider": "black" -} + "python.formatting.provider": "black", + "python.testing.unittestArgs": [ + "-v", + "-s", + "./api/tests", + "-p", + "*test_*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/api/src/automations/tags.py b/api/src/automations/tags.py new file mode 100644 index 0000000..3a56272 --- /dev/null +++ b/api/src/automations/tags.py @@ -0,0 +1,15 @@ + + + +from pathlib import Path +from typing import Dict, List +from src.yaml_serializer import dump_yaml, load_yaml + +class TagManager(Dict[str, List[str]]): + def save(self, path: Path): + path.write_text(dump_yaml(self)) + + @classmethod + def load(cls, path: Path): + with path.open('r') as fp: + return cls(load_yaml(fp)) \ No newline at end of file diff --git a/api/tests/unittests/api/test_automations.py b/api/tests/unittests/api/test_automations.py index eb7b7d1..21dc11d 100644 --- a/api/tests/unittests/api/test_automations.py +++ b/api/tests/unittests/api/test_automations.py @@ -33,8 +33,8 @@ class automation_list_tests(BaseTestCase): "limit": 50, }, ) - self.assertEqual(d["totalItems"], 31) - self.assertEqual(len(d["data"]), 31) + self.assertEqual(d["totalItems"], 28) + self.assertEqual(len(d["data"]), 28) def test_subset1(self): data = self.client.post( @@ -53,7 +53,7 @@ class automation_list_tests(BaseTestCase): "limit": 10, }, ) - self.assertEqual(d["totalItems"], 31) + self.assertEqual(d["totalItems"], 28) self.assertEqual(len(d["data"]), 10) def test_subset2(self): @@ -73,25 +73,25 @@ class automation_list_tests(BaseTestCase): "limit": 10, }, ) - self.assertEqual(d["totalItems"], 31) - self.assertEqual(len(d["data"]), 6) + self.assertEqual(d["totalItems"], 28) + self.assertEqual(len(d["data"]), 3) class automation_update_tests(BaseTestCase): def test_insert_new(self): resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0}) - self.assertEqual(resp.json()["totalItems"], 31) + self.assertEqual(resp.json()["totalItems"], 28) resp = self.client.post( "/automations", json={ - "index": 33, + "index": 29, "data": AutomationData(metadata=AutomationMetdata(id="testme")).dict(), }, ) self.assertEqual(resp.status_code, 200) - resp = self.client.post("/automations/list", json={"limit": 1, "offset": 31}) + resp = self.client.post("/automations/list", json={"limit": 1, "offset": 28}) data = resp.json() - self.assertEqual(data["totalItems"], 32) + self.assertEqual(data["totalItems"], 29) self.assertEqual(data["data"][0]["metadata"]["id"], "testme") def test_update_some(self): @@ -114,7 +114,7 @@ class automation_update_tests(BaseTestCase): class automation_delete_tests(BaseTestCase): def test_delete_some(self): resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0}) - self.assertEqual(resp.json()["totalItems"], 31) + self.assertEqual(resp.json()["totalItems"], 28) resp = self.client.delete( "/automations", json={ @@ -123,4 +123,4 @@ class automation_delete_tests(BaseTestCase): ) self.assertEqual(resp.status_code, 200) resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0}) - self.assertEqual(resp.json()["totalItems"], 30) + self.assertEqual(resp.json()["totalItems"], 27) diff --git a/api/tests/unittests/automations/test_manager.py b/api/tests/unittests/automations/test_manager.py index b6e4f54..7f734b4 100644 --- a/api/tests/unittests/automations/test_manager.py +++ b/api/tests/unittests/automations/test_manager.py @@ -27,7 +27,7 @@ class manager_tests(TestCase): def test_length(self): _, _, loader = get_example_automation_loader() - self.assertEqual(loader.get_total_items(), 31) + self.assertEqual(loader.get_total_items(), 28) def test_bad_automations_file(self): folder, _, manager = get_dummy_automation_loader( @@ -50,11 +50,11 @@ class manager_tests(TestCase): def test_create_automation_with_example_folder(self): _, _, manager = get_example_automation_loader() - self.assertEqual(manager.get_total_items(), 31) + self.assertEqual(manager.get_total_items(), 28) manager.update(32, AutomationData(metadata=AutomationMetdata(id="test1"))) - auto = manager.get(31) + auto = manager.get(28) self.assertIsNotNone(auto) - self.assertEqual(manager.get_total_items(), 32) + self.assertEqual(manager.get_total_items(), 29) self.assertEqual(auto.metadata.id, "test1") # type: ignore def test_update_automation(self): diff --git a/api/tests/unittests/automations/test_tags.py b/api/tests/unittests/automations/test_tags.py new file mode 100644 index 0000000..d2eff26 --- /dev/null +++ b/api/tests/unittests/automations/test_tags.py @@ -0,0 +1,18 @@ + + +from pathlib import Path +from tempfile import mkdtemp +from unittest import TestCase +from src.automations.tags import TagManager + +class tag_tests(TestCase): + + def test_load_and_save(self): + tmp_file = Path(mkdtemp()) + tags = TagManager() + tags['id#1'] = ['home things', 'barns', 'kitchen'] + tags['id#2'] = ['kitchen'] + + tags.save(tmp_file) + loaded_tags = TagManager.load(tmp_file) + assert loaded_tags.keys() == tags.keys() \ No newline at end of file diff --git a/api/tests/utils.py b/api/tests/utils.py index 0d14dbc..180b163 100644 --- a/api/tests/utils.py +++ b/api/tests/utils.py @@ -11,7 +11,7 @@ from src.yaml_serializer.types import IncludedYaml THIS_FOLDER = Path(__file__).parent SAMPLES_FOLDER = THIS_FOLDER / "samples" -HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "example_config_folder" +HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "config" def get_example_automation_loader() -> Tuple[Path, HassConfig, AutomationManager]: