From 83b1c85043b293c4e14453cbfa88a85d853c911d Mon Sep 17 00:00:00 2001 From: Ari Date: Fri, 2 Sep 2022 10:33:52 -0400 Subject: [PATCH] correct fix --- api/src/automations/loader.py | 2 ++ api/src/automations/types.py | 28 ++++++--------------- api/tests/unittests/api/test_automations.py | 4 +-- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/api/src/automations/loader.py b/api/src/automations/loader.py index 366652d..543ed49 100644 --- a/api/src/automations/loader.py +++ b/api/src/automations/loader.py @@ -330,6 +330,8 @@ def load_automation_path( def clean_automation(auto_raw: dict): return { **auto_raw, + "alias": auto_raw.get("alias", "") or "", + "description": auto_raw.get("description", "") or "", "condition": ifempty_or_null_then_list(load_conditions(auto_raw.get("condition", []))), "action": ifempty_or_null_then_list(load_conditions(auto_raw.get("action", []))), "trigger": ifempty_or_null_then_list(normalize_obj(auto_raw.get("trigger", []))), diff --git a/api/src/automations/types.py b/api/src/automations/types.py index 53266f1..43031fa 100644 --- a/api/src/automations/types.py +++ b/api/src/automations/types.py @@ -33,47 +33,35 @@ AutomationExtractedIter = Iterator[ExtractedAutomation] class BaseAutomation(BaseModel): id: Optional[str] - alias: Optional[str] = None - description: Optional[str] = None + alias: str = "" + description: str = "" trigger_variables: Optional[dict[str, Union[str, int, bool, float]]] = None mode: str = "single" trigger: list[dict] = [] condition: list[dict] = [] action: list[dict] = [] - def to_primitive(self, exclude_unset=True, exclude_none=True, **kwrgs): - return BaseModel.dict(self, exclude_unset=exclude_unset, exclude_none=exclude_none, **kwrgs) - - def dict(self, **kwrgs): - kwrgs["exclude_unset"] = True - kwrgs["exclude_none"] = True - return self.to_primitive( - **kwrgs, - ) + def to_primitive(self, **kwrgs): + return self.dict(exclude_unset=True, exclude_none=True) class Automation(BaseAutomation): tags: dict[str, str] = Field(default_factory=dict) - def to_primitive(self, include_tags: bool = False, **kwrgs): - out = super().to_primitive(**kwrgs) + def to_primitive(self, include_tags: bool = False): + out = super().to_primitive() if not include_tags and out.get("tags", None) is not None: del out["tags"] - if include_tags and out.get("tags", None) is None: - out["tags"] = {} return out - def dict(self, **kwrgs): - return BaseAutomation.dict(self, **kwrgs, include_tags=True) - class ExtenededAutomation(Automation): source_file: str source_file_type: Literal["list", "obj", "inline"] configuration_key: ConfigurationKey - def to_primitive(self, include_tags: bool = False, **kwrgs): - out = super().to_primitive(include_tags=include_tags, **kwrgs) + def to_primitive(self, include_tags: bool = False): + out = super().to_primitive(include_tags=include_tags) del out["source_file"] del out["source_file_type"] del out["configuration_key"] diff --git a/api/tests/unittests/api/test_automations.py b/api/tests/unittests/api/test_automations.py index ada40fa..19e93fa 100644 --- a/api/tests/unittests/api/test_automations.py +++ b/api/tests/unittests/api/test_automations.py @@ -249,7 +249,7 @@ class automation_update_tests(BaseTestCase): ) autos = resp.json()["data"] self.assertEqual(resp.json()["totalItems"], self.test_params["total"]) - self.assertNotIn("i am new", set([a["id"] for a in autos])) + self.assertNotIn("i am new", set([a.get("id", "") for a in autos])) resp = self.client.post( "/automations/item", json=Automation( @@ -262,7 +262,7 @@ class automation_update_tests(BaseTestCase): ) data = resp.json() self.assertEqual(data["totalItems"], self.test_params["total"] + 1) - self.assertIn("i am new", set([a["id"] for a in data["data"]])) + self.assertIn("i am new", set([a.get("id", "") for a in data["data"]])) def test_update_some(self): resp = self.client.post(