- 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 test_do_GET_condition(self) -> None:
- """Test GET /condition."""
- form_data = {'title': 'foo', 'description': 'foo', 'is_active': False}
- self.check_post(form_data, '/condition', 302, '/condition?id=1')
+ @classmethod
+ def GET_condition_dict(cls, cond: dict[str, object]) -> dict[str, object]:
+ """Return JSON of GET /condition to expect."""
+ return {'is_new': False,
+ 'enabled_processes': [],
+ 'disabled_processes': [],
+ 'enabling_processes': [],
+ 'disabling_processes': [],
+ 'condition': cond['id'],
+ '_library': {'Condition': cls.as_refs([cond])}}
+
+ @classmethod
+ def GET_conditions_dict(cls, conds: list[dict[str, object]]
+ ) -> dict[str, object]:
+ """Return JSON of GET /conditions to expect."""
+ library = {'Condition': cls.as_refs(conds)} if conds else {}
+ d: dict[str, object] = {'conditions': cls.as_id_list(conds),
+ 'sort_by': 'title',
+ 'pattern': '',
+ '_library': library}
+ return d
+
+ def test_fail_POST_condition(self) -> None:
+ """Test malformed/illegal POST /condition requests."""
+ # check invalid POST payloads
+ url = '/condition'
+ self.check_post({}, url, 400)
+ self.check_post({'title': ''}, url, 400)
+ self.check_post({'title': '', 'description': ''}, url, 400)
+ self.check_post({'title': '', 'is_active': False}, url, 400)
+ self.check_post({'description': '', 'is_active': False}, url, 400)
+ # check valid POST payload on bad paths
+ valid_payload = {'title': '', 'description': '', 'is_active': False}
+ self.check_post(valid_payload, '/condition?id=foo', 400)
+
+ def test_POST_condition(self) -> None:
+ """Test (valid) POST /condition and its effect on GET /condition[s]."""
+ # 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 = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
+ assert isinstance(cond['_versioned'], dict)
+ expected_single = self.GET_condition_dict(cond)
+ self.check_json_get('/condition?id=1', expected_single)
+ # … full /conditions
+ expected_all = self.GET_conditions_dict([cond])
+ self.check_json_get('/conditions', expected_all)
+ # test (no) effect of invalid POST to existing Condition on /condition
+ self.check_post({}, '/condition?id=1', 400)
+ self.check_json_get('/condition?id=1', expected_single)
+ # test effect of POST changing title and activeness
+ post = {'title': 'bar', 'description': 'oof', 'is_active': True}
+ self.check_post(post, '/condition?id=1', 302)
+ cond['_versioned']['title'][1] = 'bar'
+ cond['is_active'] = True
+ self.check_json_get('/condition?id=1', expected_single)
+ # test deletion POST's effect on …
+ self.check_post({'delete': ''}, '/condition?id=1', 302, '/conditions')
+ cond = self.cond_as_dict()
+ assert isinstance(expected_single['_library'], dict)
+ expected_single['_library']['Condition'] = self.as_refs([cond])
+ self.check_json_get('/condition?id=1', expected_single)
+ # … full /conditions
+ expected_all['conditions'] = []
+ expected_all['_library'] = {}
+ self.check_json_get('/conditions', expected_all)
+
+ def test_GET_condition(self) -> None:
+ """More GET /condition testing, especially for Process relations."""
+ # check expected default status codes