additional api refactor
This commit is contained in:
1 parent
8792ed6ed8
commit
7cb1a5d907
11 files changed
+415
-271
No files matched your search
@@ -5,8 +5,8 @@ import yaml
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from src.api.app import make_app
|
||||
from src.automations.types import ExtenededAutomation
|
||||
from tests.utils import HA_CONFIG2_EXAMPLE, get_example_automation_loader
|
||||
from src.automations.types import Automation, ExtenededAutomation
|
||||
from tests.utils import HA_CONFIG2_EXAMPLE, HA_CONFIG6_EXAMPLE, get_example_automation_loader
|
||||
|
||||
|
||||
class BaseTestCase(TestCase):
|
||||
@@ -19,7 +19,7 @@ class BaseTestCase(TestCase):
|
||||
self.client = TestClient(make_app(self.automation_loader))
|
||||
|
||||
|
||||
class automation_list_tests(BaseTestCase):
|
||||
class automation_pagination_tests(BaseTestCase):
|
||||
|
||||
test_params = {"total": 32, "subtest2_found_items": 7, "subtest1_found_items": 10}
|
||||
|
||||
@@ -84,7 +84,7 @@ class automation_list_tests(BaseTestCase):
|
||||
self.assertEqual(len(d["data"]), self.test_params["subtest2_found_items"])
|
||||
|
||||
|
||||
class automation2_lists_tests(automation_list_tests):
|
||||
class automation_pagination_tests_2(automation_pagination_tests):
|
||||
test_params = {
|
||||
"total": 1,
|
||||
"subtest2_found_items": 0,
|
||||
@@ -101,27 +101,189 @@ class automation2_lists_tests(automation_list_tests):
|
||||
|
||||
|
||||
class automation_update_tests(BaseTestCase):
|
||||
test_params = {
|
||||
"total": 32,
|
||||
"modify_item": 20,
|
||||
"consitency_tests": {
|
||||
1: {
|
||||
"offset": 15,
|
||||
"file_offset": 15,
|
||||
"expected_change": {
|
||||
"metadata": {
|
||||
"id": "1629422438707",
|
||||
"alias": "I CHANGED!",
|
||||
"description": "",
|
||||
"mode": "single",
|
||||
},
|
||||
"trigger": [{"platform": "time", "at": "input_datetime.vita_night_pump_time"}],
|
||||
"condition": [
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "time",
|
||||
"after": "input_datetime.vita_night_pump_time",
|
||||
"before": "00:00:00",
|
||||
},
|
||||
{"condition": "time", "after": "00:00:00", "before": "06:00:00"},
|
||||
],
|
||||
"condition": "and",
|
||||
},
|
||||
],
|
||||
"action": [
|
||||
{
|
||||
"service": "light.turn_on",
|
||||
"target": {
|
||||
"entity_id": "light.bulb_staircase",
|
||||
"area_id": "living_room",
|
||||
},
|
||||
"data": {"brightness_pct": 40, "transition": 15},
|
||||
}
|
||||
],
|
||||
},
|
||||
},
|
||||
2: {
|
||||
"offset": 27,
|
||||
"file_offset": 27,
|
||||
"expected_change": {
|
||||
"metadata": {
|
||||
"id": "1649947692702",
|
||||
"alias": "I CHANGED!",
|
||||
"description": "",
|
||||
"mode": "single",
|
||||
},
|
||||
"trigger": [
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "turn_on",
|
||||
"id": "turn_on",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "turn_off",
|
||||
"id": "turn_off",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "dim_up",
|
||||
"id": "dim_up",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "dim_down",
|
||||
"id": "dim_down",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_double_press",
|
||||
"subtype": "turn_on",
|
||||
"id": "up_dbclk",
|
||||
},
|
||||
],
|
||||
"condition": [],
|
||||
"action": [
|
||||
{
|
||||
"choose": [
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "turn_off"}],
|
||||
"sequence": [
|
||||
{
|
||||
"type": "turn_off",
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"entity_id": "light.bulb_ari_lamp",
|
||||
"domain": "light",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "turn_on"}],
|
||||
"sequence": [
|
||||
{
|
||||
"service": "light.turn_on",
|
||||
"data": {"color_temp": 153, "brightness_pct": 100},
|
||||
"target": {"device_id": "das2qdasdasdasadaasd"},
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "dim_up"}],
|
||||
"sequence": [
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "light",
|
||||
"entity_id": "light.bulb_ari_lamp",
|
||||
"type": "brightness_increase",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "dim_down"}],
|
||||
"sequence": [
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "light",
|
||||
"entity_id": "light.bulb_ari_lamp",
|
||||
"type": "brightness_decrease",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "up_dbclk"}],
|
||||
"sequence": [
|
||||
{
|
||||
"service": "light.turn_on",
|
||||
"data": {"color_temp": 500, "brightness_pct": 100},
|
||||
"target": {"device_id": "das2qdasdasdasadaasd"},
|
||||
}
|
||||
],
|
||||
},
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
def test_insert_new(self):
|
||||
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0})
|
||||
resp = self.client.post(
|
||||
"/automations/list", json={"limit": self.test_params["total"] + 1, "offset": 0}
|
||||
)
|
||||
autos = resp.json()["data"]
|
||||
self.assertEqual(resp.json()["totalItems"], 32)
|
||||
self.assertEqual(resp.json()["totalItems"], self.test_params["total"])
|
||||
self.assertNotIn("i am new", set([a["id"] for a in autos]))
|
||||
resp = self.client.post(
|
||||
"/automations/item",
|
||||
json=ExtenededAutomation(
|
||||
json=Automation(
|
||||
id="i am new",
|
||||
configuration_key="",
|
||||
source_file_type="list",
|
||||
source_file=autos[0]["source_file"],
|
||||
).to_json(),
|
||||
).dict(),
|
||||
)
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 32})
|
||||
resp = self.client.post(
|
||||
"/automations/list", json={"limit": self.test_params["total"] + 1, "offset": 0}
|
||||
)
|
||||
data = resp.json()
|
||||
self.assertEqual(data["totalItems"], 33)
|
||||
self.assertEqual(data["data"][0]["id"], "i am new")
|
||||
self.assertEqual(data["totalItems"], self.test_params["total"] + 1)
|
||||
self.assertIn("i am new", set([a["id"] for a in data["data"]]))
|
||||
|
||||
def test_update_some(self):
|
||||
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 20})
|
||||
resp = self.client.post(
|
||||
"/automations/list", json={"limit": 1, "offset": self.test_params["modify_item"]}
|
||||
)
|
||||
original_auto = resp.json()["data"][0]
|
||||
changed_to = ExtenededAutomation(
|
||||
id=original_auto["id"],
|
||||
@@ -130,12 +292,14 @@ class automation_update_tests(BaseTestCase):
|
||||
source_file_type=original_auto["source_file_type"],
|
||||
source_file=original_auto["source_file"],
|
||||
)
|
||||
resp = self.client.post(
|
||||
resp = self.client.put(
|
||||
"/automations/item",
|
||||
json=changed_to.to_json(),
|
||||
)
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 20})
|
||||
resp = self.client.post(
|
||||
"/automations/list", json={"limit": 1, "offset": self.test_params["modify_item"]}
|
||||
)
|
||||
new_auto = resp.json()["data"][0]
|
||||
self.assertNotEqual(original_auto, new_auto)
|
||||
self.assertEqual(
|
||||
@@ -144,180 +308,78 @@ class automation_update_tests(BaseTestCase):
|
||||
)
|
||||
|
||||
def test_consistent_standard_1(self):
|
||||
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 15})
|
||||
original_auto = resp.json()["data"][0]
|
||||
original_auto["alias"] = "I CHANGED!"
|
||||
resp = self.client.post(
|
||||
"/automations/item",
|
||||
json=original_auto,
|
||||
)
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
with (self.config_folder / "automations.yaml").open("r") as fp:
|
||||
disk_data = dict(yaml.safe_load(fp)[15])
|
||||
trigger = list(map(dict, disk_data.pop("trigger")))
|
||||
condition = disk_data.pop("condition")
|
||||
action = list(map(dict, disk_data.pop("action")))
|
||||
self.assertDictEqual(
|
||||
disk_data,
|
||||
{"id": "1629422438707", "alias": "I CHANGED!", "description": "", "mode": "single"},
|
||||
)
|
||||
self.assertListEqual(
|
||||
trigger, [{"platform": "time", "at": "input_datetime.vita_night_pump_time"}]
|
||||
)
|
||||
self.assertListEqual(
|
||||
condition,
|
||||
[
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"condition": "time",
|
||||
"after": "input_datetime.vita_night_pump_time",
|
||||
"before": "00:00:00",
|
||||
},
|
||||
{"condition": "time", "after": "00:00:00", "before": "06:00:00"},
|
||||
],
|
||||
"condition": "and",
|
||||
},
|
||||
],
|
||||
)
|
||||
self.assertListEqual(
|
||||
action,
|
||||
[
|
||||
{
|
||||
"service": "light.turn_on",
|
||||
"target": {"entity_id": "light.bulb_staircase", "area_id": "living_room"},
|
||||
"data": {"brightness_pct": 40, "transition": 15},
|
||||
}
|
||||
],
|
||||
)
|
||||
for test_id, test_params in self.test_params["consitency_tests"].items():
|
||||
with self.subTest(test_id=test_id):
|
||||
resp = self.client.post(
|
||||
"/automations/list",
|
||||
json={"limit": 1, "offset": test_params["offset"]},
|
||||
)
|
||||
original_auto = resp.json()["data"][0]
|
||||
original_auto["alias"] = "I CHANGED!"
|
||||
resp = self.client.post(
|
||||
"/automations/item",
|
||||
json=original_auto,
|
||||
)
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
with (self.config_folder / original_auto["source_file"]).open("r") as fp:
|
||||
disk_data = dict(yaml.safe_load(fp)[test_params["file_offset"]])
|
||||
trigger = list(map(dict, disk_data.pop("trigger")))
|
||||
condition = disk_data.pop("condition")
|
||||
action = list(map(dict, disk_data.pop("action")))
|
||||
self.assertDictEqual(
|
||||
disk_data,
|
||||
test_params["expected_change"]["metadata"],
|
||||
)
|
||||
self.assertListEqual(
|
||||
trigger,
|
||||
test_params["expected_change"]["trigger"],
|
||||
)
|
||||
self.assertListEqual(
|
||||
condition,
|
||||
test_params["expected_change"]["condition"],
|
||||
)
|
||||
self.assertListEqual(
|
||||
action,
|
||||
test_params["expected_change"]["action"],
|
||||
)
|
||||
|
||||
def test_consistent_standard_2(self):
|
||||
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 27})
|
||||
original_auto = resp.json()["data"][0]
|
||||
original_auto["alias"] = "I CHANGED!"
|
||||
resp = self.client.post(
|
||||
"/automations/item",
|
||||
json=original_auto,
|
||||
)
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
with (self.config_folder / "automations.yaml").open("r") as fp:
|
||||
disk_data = dict(yaml.safe_load(fp)[27])
|
||||
trigger = list(map(dict, disk_data.pop("trigger")))
|
||||
condition = disk_data.pop("condition")
|
||||
action = list(map(dict, disk_data.pop("action")))
|
||||
self.assertDictEqual(
|
||||
disk_data,
|
||||
{"id": "1649947692702", "alias": "I CHANGED!", "description": "", "mode": "single"},
|
||||
)
|
||||
self.assertListEqual(
|
||||
trigger,
|
||||
[
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "turn_on",
|
||||
"id": "turn_on",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "turn_off",
|
||||
"id": "turn_off",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "dim_up",
|
||||
"id": "dim_up",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_short_press",
|
||||
"subtype": "dim_down",
|
||||
"id": "dim_down",
|
||||
},
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "zha",
|
||||
"platform": "device",
|
||||
"type": "remote_button_double_press",
|
||||
"subtype": "turn_on",
|
||||
"id": "up_dbclk",
|
||||
},
|
||||
],
|
||||
)
|
||||
self.assertListEqual(condition, [])
|
||||
self.assertListEqual(
|
||||
action,
|
||||
[
|
||||
{
|
||||
"choose": [
|
||||
|
||||
class automation_update_split_config_tests(automation_update_tests):
|
||||
test_params = {
|
||||
"total": 12,
|
||||
"modify_item": 3,
|
||||
"consitency_tests": {
|
||||
1: {
|
||||
"offset": 3,
|
||||
"file_offset": 0,
|
||||
"expected_change": {
|
||||
"metadata": {
|
||||
"alias": "I CHANGED!",
|
||||
"description": "Example",
|
||||
"id": "ui1",
|
||||
"mode": "single",
|
||||
},
|
||||
"trigger": [{"platform": "homeassistant", "event": "start"}],
|
||||
"condition": [],
|
||||
"action": [
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "turn_off"}],
|
||||
"sequence": [
|
||||
{
|
||||
"type": "turn_off",
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"entity_id": "light.bulb_ari_lamp",
|
||||
"domain": "light",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "turn_on"}],
|
||||
"sequence": [
|
||||
{
|
||||
"service": "light.turn_on",
|
||||
"data": {"color_temp": 153, "brightness_pct": 100},
|
||||
"target": {"device_id": "das2qdasdasdasadaasd"},
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "dim_up"}],
|
||||
"sequence": [
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "light",
|
||||
"entity_id": "light.bulb_ari_lamp",
|
||||
"type": "brightness_increase",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "dim_down"}],
|
||||
"sequence": [
|
||||
{
|
||||
"device_id": "das2qdasdasdasadaasd",
|
||||
"domain": "light",
|
||||
"entity_id": "light.bulb_ari_lamp",
|
||||
"type": "brightness_decrease",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"conditions": [{"condition": "trigger", "id": "up_dbclk"}],
|
||||
"sequence": [
|
||||
{
|
||||
"service": "light.turn_on",
|
||||
"data": {"color_temp": 500, "brightness_pct": 100},
|
||||
"target": {"device_id": "das2qdasdasdasadaasd"},
|
||||
}
|
||||
],
|
||||
},
|
||||
"service": "counter.increment",
|
||||
"data": {},
|
||||
"target": {"entity_id": "counter.up_times"},
|
||||
}
|
||||
],
|
||||
"default": [],
|
||||
}
|
||||
],
|
||||
)
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
def setUp(self) -> None:
|
||||
(
|
||||
self.config_folder,
|
||||
self.hass_config,
|
||||
self.automation_loader,
|
||||
) = get_example_automation_loader(HA_CONFIG6_EXAMPLE)
|
||||
self.client = TestClient(make_app(self.automation_loader))
|
||||
|
||||
|
||||
class automation_delete_tests(BaseTestCase):
|
||||
|
||||
Reference in new issue
Block a user