home · contact · privacy
Overhaul as_dict generation to avoid endless nesting of objects.
[plomtask] / tests / utils.py
index f473c180ba565355a3b6ac0a03acb85d2fa307de..3b259b2e3aaa7c202f8b581b6b9167d1e04f1128 100644 (file)
@@ -327,6 +327,51 @@ class TestCaseWithServer(TestCaseWithDB):
         self.server_thread.join()
         super().tearDown()
 
+    @staticmethod
+    def as_id_list(items: list[dict[str, object]]) -> list[int | str]:
+        """Return list of only 'id' fields of items."""
+        id_list = []
+        for item in items:
+            assert isinstance(item['id'], (int, str))
+            id_list += [item['id']]
+        return id_list
+
+    @staticmethod
+    def as_refs(items: list[dict[str, object]]
+                ) -> dict[str, dict[str, object]]:
+        """Return dictionary of items by their 'id' fields."""
+        refs = {}
+        for item in items:
+            refs[str(item['id'])] = item
+        return refs
+
+    @staticmethod
+    def proc_as_dict(id_: int = 1,
+                     title: str = 'A',
+                     description: str = '',
+                     effort: float = 1.0,
+                     enables: None | list[dict[str, object]] = None,
+                     disables: None | list[dict[str, object]] = None,
+                     conditions: None | list[dict[str, object]] = None,
+                     blockers: None | list[dict[str, object]] = None
+                     ) -> dict[str, object]:
+        """Return JSON of Process to expect."""
+        # pylint: disable=too-many-arguments
+        d = {'id': id_,
+             'calendarize': False,
+             'suppressed_steps': [],
+             'explicit_steps': [],
+             '_versioned': {
+                 'title': {0: title},
+                 'description': {0: description},
+                 'effort': {0: effort}
+                 },
+             'conditions': [c['id'] for c in conditions] if conditions else [],
+             'disables': [c['id'] for c in disables] if disables else [],
+             'enables': [c['id'] for c in enables] if enables else [],
+             'blockers': [c['id'] for c in blockers] if blockers else []}
+        return d
+
     def check_redirect(self, target: str) -> None:
         """Check that self.conn answers with a 302 redirect to target."""
         response = self.conn.getresponse()