home · contact · privacy
Extend Conditions POST test to use new JSON interface.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 17 Jun 2024 18:33:51 +0000 (20:33 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 17 Jun 2024 18:33:51 +0000 (20:33 +0200)
plomtask/versioned_attributes.py
tests/conditions.py
tests/utils.py

index b04c56465a74e1c7626736a0b7bb85f3ef5b8962..cc42bbc40f761c35715f11b0ca4f61ba67dbe579 100644 (file)
@@ -28,9 +28,7 @@ class VersionedAttribute:
     @property
     def as_dict(self) -> dict[str, object]:
         """Return self as (json.dumps-coompatible) dict."""
-        d = {'parent_id': self.parent.id_,
-             'table_name': self.table_name,
-             'history': self.history}
+        d = {'parent_id': self.parent.id_, 'history': self.history}
         return d
 
     @property
index a316d010b46ea0b648c2d725958586ddbdc026fa..2b522e65abbd4c1ef160f5bd1d9191b21e53d102 100644 (file)
@@ -68,14 +68,45 @@ class TestsWithServer(TestCaseWithServer):
 
     def test_do_POST_condition(self) -> None:
         """Test POST /condition and its effect on the database."""
-        form_data = {'title': 'foo', 'description': 'foo', 'is_active': False}
-        self.check_post(form_data, '/condition', 302, '/condition?id=1')
-        self.assertEqual(1, len(Condition.all(self.db_conn)))
-        form_data['delete'] = ''
-        self.check_post(form_data, '/condition?id=', 404)
-        self.check_post(form_data, '/condition?id=2', 404)
-        self.check_post(form_data, '/condition?id=1', 302, '/conditions')
-        self.assertEqual(0, len(Condition.all(self.db_conn)))
+
+        def check(path: str, expected: dict[str, object]) -> None:
+            self.conn.request('GET', path)
+            response = self.conn.getresponse()
+            self.assertEqual(response.status, 200)
+            retrieved = json_loads(response.read().decode())
+            self.blank_history_keys_in(retrieved)
+            self.assertEqual(expected, retrieved)
+
+        # check empty POST fails
+        self.check_post({}, '/condition', 400)
+        # test valid POST's effect on …
+        post = {'title': 'foo', 'description': 'oof', 'is_active': False}
+        self.check_post(post, '/condition', 302, '/condition?id=1')
+        # … single /condition
+        cond = {'id': 1, 'is_active': False,
+                'title': {'parent_id': 1, 'history': {'[0]': 'foo'}},
+                'description': {'parent_id': 1, 'history': {'[0]': 'oof'}}}
+        expected_single = {'is_new': False,
+                           'enabled_processes': [],
+                           'disabled_processes': [],
+                           'enabling_processes': [],
+                           'disabling_processes': [],
+                           'condition': cond}
+        check('/condition?id=1', expected_single)
+        # … full /conditions
+        expected_all = {'conditions': [cond], 'sort_by': 'title', 'pattern': ''}
+        check('/conditions', expected_all)
+        # test effect of invalid POST to existing Condition on /condition
+        self.check_post({}, '/condition?id=1', 400)
+        check('/condition?id=1', expected_single)
+        # test deletion POST's effect on …
+        self.check_post({'delete': ''}, '/condition?id=1', 302, '/conditions')
+        cond['title']['history'] = {}
+        cond['description']['history'] = {}
+        check('/condition?id=1', expected_single)
+        # … full /conditions
+        expected_all['conditions'] = []
+        check('/conditions', expected_all)
 
     def test_do_GET_condition(self) -> None:
         """Test GET /condition."""
@@ -91,12 +122,7 @@ class TestsWithServer(TestCaseWithServer):
             response = self.conn.getresponse()
             self.assertEqual(response.status, 200)
             retrieved_json = json_loads(response.read().decode())
-            # ignore history timestamps (too difficult too anticipate)
-            for cond in retrieved_json['conditions']:
-                for k in [k for k in cond.keys() if isinstance(cond[k], dict)]:
-                    history = cond[k]['history']
-                    history = {'[IGNORE]': list(history.values())[0]}
-                    cond[k]['history'] = history
+            self.blank_history_keys_in(retrieved_json)
             self.assertEqual(expected_json, retrieved_json)
 
         # test empty result on empty DB, default-settings on empty params
@@ -117,19 +143,19 @@ class TestsWithServer(TestCaseWithServer):
         post_3 = {'title': 'baz', 'description': 'zab', 'is_active': True}
         self.check_post(post_3, '/condition', 302, '/condition?id=3')
         cond_1 = {'id': 1, 'is_active': False,
-                  'title': {'history': {'[IGNORE]': 'foo'},
+                  'title': {'history': {'[0]': 'foo'},
                             'parent_id': 1},
-                           'description': {'history': {'[IGNORE]': 'oof'},
+                           'description': {'history': {'[0]': 'oof'},
                                            'parent_id': 1}}
         cond_2 = {'id': 2, 'is_active': False,
-                  'title': {'history': {'[IGNORE]': 'bar'},
+                  'title': {'history': {'[0]': 'bar'},
                             'parent_id': 2},
-                  'description': {'history': {'[IGNORE]': 'rab'},
+                  'description': {'history': {'[0]': 'rab'},
                                   'parent_id': 2}}
         cond_3 = {'id': 3, 'is_active': True,
-                  'title': {'history': {'[IGNORE]': 'baz'},
+                  'title': {'history': {'[0]': 'baz'},
                             'parent_id': 3},
-                  'description': {'history': {'[IGNORE]': 'zab'},
+                  'description': {'history': {'[0]': 'zab'},
                                   'parent_id': 3}}
         cons = [cond_2, cond_3, cond_1]
         expected_json = {'conditions': cons, 'sort_by': 'title', 'pattern': ''}
index 15a53ae0ddc0b78835b5baacea15f43d3a81cba0..13e4f949d177a7d73456d2546db9f49de3e33fb1 100644 (file)
@@ -268,3 +268,21 @@ class TestCaseWithServer(TestCaseWithDB):
         self.check_post(form_data, f'/process?id={id_}', 302,
                         f'/process?id={id_}')
         return form_data
+
+    @staticmethod
+    def blank_history_keys_in(d: dict[str, object]) -> None:
+        """Re-write "history" object keys to bracketed integer strings."""
+        def walk_tree(d: Any) -> Any:
+            if isinstance(d, dict):
+                if 'history' in d.keys():
+                    vals = d['history'].values()
+                    history = {}
+                    for i, val in enumerate(vals):
+                        history[f'[{i}]'] = val
+                    d['history'] = history
+                for k in list(d.keys()):
+                    walk_tree(d[k])
+            elif isinstance(d, list):
+                d[:] = [walk_tree(i) for i in d]
+            return d
+        walk_tree(d)