home · contact · privacy
Extend Days test to behavior of Conditions.
[plomtask] / tests / utils.py
index ed4101a6c32a52d1e26fadb99c35fd8c44d2178a..e0b4e5f2f7c144bffe5120040e3b85a36c91258e 100644 (file)
@@ -327,6 +327,45 @@ 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 cond_as_dict(id_: int = 1,
+                     is_active: bool = False,
+                     titles: None | list[str] = None,
+                     descriptions: None | list[str] = None
+                     ) -> dict[str, object]:
+        """Return JSON of Condition to expect."""
+        d = {'id': id_,
+             'is_active': is_active,
+             '_versioned': {
+                 'title': {},
+                 'description': {}}}
+        titles = titles if titles else []
+        descriptions = descriptions if descriptions else []
+        assert isinstance(d['_versioned'], dict)
+        for i, title in enumerate(titles):
+            d['_versioned']['title'][i] = title
+        for i, description in enumerate(descriptions):
+            d['_versioned']['description'][i] = description
+        return d
+
     @staticmethod
     def proc_as_dict(id_: int = 1,
                      title: str = 'A',
@@ -339,6 +378,7 @@ class TestCaseWithServer(TestCaseWithDB):
                      ) -> dict[str, object]:
         """Return JSON of Process to expect."""
         # pylint: disable=too-many-arguments
+        as_id_list = TestCaseWithServer.as_id_list
         d = {'id': id_,
              'calendarize': False,
              'suppressed_steps': [],
@@ -346,12 +386,11 @@ class TestCaseWithServer(TestCaseWithDB):
              '_versioned': {
                  'title': {0: title},
                  'description': {0: description},
-                 'effort': {0: effort}
-                 },
-             'conditions': conditions if conditions else [],
-             'disables': disables if disables else [],
-             'enables': enables if enables else [],
-             'blockers': blockers if blockers else []}
+                 'effort': {0: effort}},
+             'conditions': as_id_list(conditions) if conditions else [],
+             'disables': as_id_list(disables) if disables else [],
+             'enables': as_id_list(enables) if enables else [],
+             'blockers': as_id_list(blockers) if blockers else []}
         return d
 
     def check_redirect(self, target: str) -> None: