From ef5e66a8f127c19a90830a10b007db64c06e03d6 Mon Sep 17 00:00:00 2001 From: Ari Date: Mon, 16 May 2022 10:29:05 -0400 Subject: [PATCH] fixed test cases --- api/src/hass_config/__init__.py | 6 ++-- api/src/json_serializer.py | 1 + api/tests/samples/config-2/automations.yaml | 15 ++++++++ api/tests/samples/config-2/configuration.yaml | 2 ++ api/tests/unittests/api/test_automations.py | 36 +++++++++++++++---- api/tests/utils.py | 9 ++--- 6 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 api/tests/samples/config-2/automations.yaml create mode 100644 api/tests/samples/config-2/configuration.yaml diff --git a/api/src/hass_config/__init__.py b/api/src/hass_config/__init__.py index b1eab3c..0c73c15 100644 --- a/api/src/hass_config/__init__.py +++ b/api/src/hass_config/__init__.py @@ -1,5 +1,4 @@ import os -from typing import cast from decouple import config from pathlib import Path from .loader import HassConfig @@ -15,8 +14,9 @@ def find_hass_config() -> HassConfig: print("|-> ", path) return HassConfig(path) - if "HASSIO_TOKEN" in os.environ: - return HassConfig(Path("/config")) + config_dir = Path("/config") + if config_dir.is_dir(): + return HassConfig(config_dir) """Put together the default configuration directory based on the OS.""" data_dir = Path(config("APPDATA", None) if os.name == "nt" else os.path.expanduser("~")) diff --git a/api/src/json_serializer.py b/api/src/json_serializer.py index e919d03..023bcf1 100644 --- a/api/src/json_serializer.py +++ b/api/src/json_serializer.py @@ -34,6 +34,7 @@ def normalize_obj(obj: Any) -> Any: or isinstance(obj, int) or isinstance(obj, float) or isinstance(obj, bool) + or obj is None ): return obj else: diff --git a/api/tests/samples/config-2/automations.yaml b/api/tests/samples/config-2/automations.yaml new file mode 100644 index 0000000..ca4cd65 --- /dev/null +++ b/api/tests/samples/config-2/automations.yaml @@ -0,0 +1,15 @@ +- id: '1652069225859' + alias: 'Climate - Pref temperature ' + description: '' + trigger: + - platform: state + entity_id: input_number.preferred_temperature + condition: [] + action: + - service: climate.set_temperature + data: + temperature: + '[object Object]': + target: + entity_id: climate.main_floor + mode: single diff --git a/api/tests/samples/config-2/configuration.yaml b/api/tests/samples/config-2/configuration.yaml new file mode 100644 index 0000000..2707225 --- /dev/null +++ b/api/tests/samples/config-2/configuration.yaml @@ -0,0 +1,2 @@ +# empty configuration file +cloud: diff --git a/api/tests/unittests/api/test_automations.py b/api/tests/unittests/api/test_automations.py index ea3fbd5..ad423a1 100644 --- a/api/tests/unittests/api/test_automations.py +++ b/api/tests/unittests/api/test_automations.py @@ -3,7 +3,7 @@ from unittest import TestCase from fastapi.testclient import TestClient from src.api.app import make_app from src.automations.types import AutomationData, AutomationMetdata, ExtenededAutomationData -from tests.utils import get_example_automation_loader +from tests.utils import HA_CONFIG2_EXAMPLE, get_example_automation_loader class BaseTestCase(TestCase): @@ -17,6 +17,13 @@ class BaseTestCase(TestCase): class automation_list_tests(BaseTestCase): + + test_params = { + "total": 28, + "subtest2_found_items": 3, + "subtest1_found_items": 10 + } + def test_get_list_of_everything(self): data = self.client.post( "/automations/list", @@ -34,8 +41,8 @@ class automation_list_tests(BaseTestCase): "limit": 50, }, ) - self.assertEqual(d["totalItems"], 28) - self.assertEqual(len(d["data"]), 28) + self.assertEqual(d["totalItems"], self.test_params['total']) + self.assertEqual(len(d["data"]), self.test_params['total']) def test_subset1(self): data = self.client.post( @@ -54,8 +61,8 @@ class automation_list_tests(BaseTestCase): "limit": 10, }, ) - self.assertEqual(d["totalItems"], 28) - self.assertEqual(len(d["data"]), 10) + self.assertEqual(d["totalItems"], self.test_params['total']) + self.assertEqual(len(d["data"]), self.test_params["subtest1_found_items"]) def test_subset2(self): data = self.client.post( @@ -74,8 +81,23 @@ class automation_list_tests(BaseTestCase): "limit": 10, }, ) - self.assertEqual(d["totalItems"], 28) - self.assertEqual(len(d["data"]), 3) + self.assertEqual(d["totalItems"], self.test_params['total']) + self.assertEqual(len(d["data"]), self.test_params["subtest2_found_items"]) + +class automation2_lists_tests(automation_list_tests): + test_params = { + "total": 1, + "subtest2_found_items": 0, + "subtest1_found_items": 1, + } + def setUp(self) -> None: + ( + self.config_folder, + self.hass_config, + self.automation_loader, + ) = get_example_automation_loader(HA_CONFIG2_EXAMPLE) + self.client = TestClient(make_app(self.automation_loader)) + class automation_update_tests(BaseTestCase): diff --git a/api/tests/utils.py b/api/tests/utils.py index 180b163..efb01bc 100644 --- a/api/tests/utils.py +++ b/api/tests/utils.py @@ -12,21 +12,22 @@ from src.yaml_serializer.types import IncludedYaml THIS_FOLDER = Path(__file__).parent SAMPLES_FOLDER = THIS_FOLDER / "samples" HA_CONFIG_EXAMPLE = SAMPLES_FOLDER / "config" +HA_CONFIG2_EXAMPLE = SAMPLES_FOLDER / "config-2" -def get_example_automation_loader() -> Tuple[Path, HassConfig, AutomationManager]: +def get_example_automation_loader(config_to_copy: Path = HA_CONFIG_EXAMPLE) -> Tuple[Path, HassConfig, AutomationManager]: """Creates an example automation loader from the samples folder Returns: Tuple[Path, HassConfig, AutomationManager] """ - root_folder = create_copy_of_example_config() + root_folder = create_copy_of_example_config(config_to_copy) hass_config = HassConfig(root_folder) automation_loader = AutomationManager(hass_config) return root_folder, hass_config, automation_loader -def create_copy_of_example_config() -> Path: +def create_copy_of_example_config(config_to_copy: Path = HA_CONFIG_EXAMPLE) -> Path: """Creates a copy of the example config folder Returns: @@ -34,7 +35,7 @@ def create_copy_of_example_config() -> Path: """ root_folder = Path(mkdtemp()) root_folder.rmdir() - copytree(HA_CONFIG_EXAMPLE, root_folder) + copytree(config_to_copy, root_folder) return root_folder