home · contact · privacy
Extend Conditions POST test to use new JSON interface.
[plomtask] / tests / conditions.py
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': ''}