From f8e615304cc9a1bcfdd88ca92a3c9537cfaba56c Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Tue, 4 Jan 2022 21:53:19 -0500 Subject: [PATCH] simple api tests --- api/src/api/routes/automations.py | 4 +-- api/tests/unittests/api/test_automations.py | 36 +++++++++++++++++-- .../unittests/automations/test_manager.py | 9 +++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/api/src/api/routes/automations.py b/api/src/api/routes/automations.py index 49e7aa5..89d971b 100644 --- a/api/src/api/routes/automations.py +++ b/api/src/api/routes/automations.py @@ -24,11 +24,11 @@ def make_automation_router(automations: AutomationManager) -> APIRouter: ), ) - @router.post("/") + @router.post("") def upsert_auto(body: UpdateAutoRequest): automations.update(body.index, body.data) - @router.delete("/") + @router.delete("") def delete_auto(body: DeleteAutoRequest): try: automations.delete(body.index) diff --git a/api/tests/unittests/api/test_automations.py b/api/tests/unittests/api/test_automations.py index a758041..eb7b7d1 100644 --- a/api/tests/unittests/api/test_automations.py +++ b/api/tests/unittests/api/test_automations.py @@ -84,11 +84,43 @@ class automation_update_tests(BaseTestCase): resp = self.client.post( "/automations", json={ - "index": 32, + "index": 33, "data": AutomationData(metadata=AutomationMetdata(id="testme")).dict(), }, ) - resp = self.client.post("/automations/list", json={"limit": 1, "offset": 32}) + self.assertEqual(resp.status_code, 200) + resp = self.client.post("/automations/list", json={"limit": 1, "offset": 31}) data = resp.json() self.assertEqual(data["totalItems"], 32) self.assertEqual(data["data"][0]["metadata"]["id"], "testme") + + def test_update_some(self): + resp = self.client.post("/automations/list", json={"limit": 1, "offset": 20}) + original_auto = resp.json()["data"][0] + resp = self.client.post( + "/automations", + json={ + "index": 20, + "data": AutomationData(metadata=AutomationMetdata(id="testme")).dict(), + }, + ) + self.assertEqual(resp.status_code, 200) + resp = self.client.post("/automations/list", json={"limit": 1, "offset": 20}) + new_auto = resp.json()["data"][0] + self.assertNotEqual(original_auto, new_auto) + self.assertEqual(AutomationData(metadata=AutomationMetdata(id="testme")).dict(), new_auto) + + +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) + resp = self.client.delete( + "/automations", + json={ + "index": 20, + }, + ) + self.assertEqual(resp.status_code, 200) + resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0}) + self.assertEqual(resp.json()["totalItems"], 30) diff --git a/api/tests/unittests/automations/test_manager.py b/api/tests/unittests/automations/test_manager.py index 1010907..b6e4f54 100644 --- a/api/tests/unittests/automations/test_manager.py +++ b/api/tests/unittests/automations/test_manager.py @@ -48,6 +48,15 @@ class manager_tests(TestCase): self.assertIsNotNone(auto) self.assertEqual(auto.metadata.id, "test1") # type: ignore + def test_create_automation_with_example_folder(self): + _, _, manager = get_example_automation_loader() + self.assertEqual(manager.get_total_items(), 31) + manager.update(32, AutomationData(metadata=AutomationMetdata(id="test1"))) + auto = manager.get(31) + self.assertIsNotNone(auto) + self.assertEqual(manager.get_total_items(), 32) + self.assertEqual(auto.metadata.id, "test1") # type: ignore + def test_update_automation(self): _, _, manager = get_dummy_automation_loader( [