- 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)