+ # make Condition and two Processes that among them establish all
+ # possible ConditionsRelations to it, …
+ cond_post = {'title': 'foo', 'description': 'oof', 'is_active': False}
+ self.check_post(cond_post, '/condition', 302, '/condition?id=1')
+ proc1_post = {'title': 'A', 'description': '', 'effort': 1.0,
+ 'conditions': [1], 'disables': [1]}
+ proc2_post = {'title': 'B', 'description': '', 'effort': 1.0,
+ 'enables': [1], 'blockers': [1]}
+ self.post_process(1, proc1_post)
+ self.post_process(2, proc2_post)
+ # … then check /condition displays all these properly.
+ cond = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
+ assert isinstance(cond['id'], int)
+ proc1 = self.proc_as_dict(conditions=[cond['id']],
+ disables=[cond['id']])
+ proc2 = self.proc_as_dict(2, 'B',
+ blockers=[cond['id']],
+ enables=[cond['id']])
+ expected = self.GET_condition_dict(cond)
+ assert isinstance(expected['_library'], dict)
+ expected['enabled_processes'] = self.as_id_list([proc1])
+ expected['disabled_processes'] = self.as_id_list([proc2])
+ expected['enabling_processes'] = self.as_id_list([proc2])
+ expected['disabling_processes'] = self.as_id_list([proc1])
+ expected['_library']['Process'] = self.as_refs([proc1, proc2])
+ self.check_json_get('/condition?id=1', expected)
+
+ def test_GET_conditions(self) -> None:
+ """Test GET /conditions."""
+ # test empty result on empty DB, default-settings on empty params
+ expected = self.GET_conditions_dict([])
+ self.check_json_get('/conditions', expected)
+ # test on meaningless non-empty params (incl. entirely un-used key),
+ # that 'sort_by' default to 'title' (even if set to something else, as
+ # long as without handler) and 'pattern' get preserved
+ expected['pattern'] = 'bar' # preserved despite zero effect!
+ url = '/conditions?sort_by=foo&pattern=bar&foo=x'
+ self.check_json_get(url, expected)
+ # test non-empty result, automatic (positive) sorting by title
+ post1 = {'is_active': False, 'title': 'foo', 'description': 'oof'}
+ post2 = {'is_active': False, 'title': 'bar', 'description': 'rab'}
+ post3 = {'is_active': True, 'title': 'baz', 'description': 'zab'}
+ self.check_post(post1, '/condition', 302, '/condition?id=1')
+ self.check_post(post2, '/condition', 302, '/condition?id=2')
+ self.check_post(post3, '/condition', 302, '/condition?id=3')
+ cond1 = self.cond_as_dict(1, False, ['foo'], ['oof'])
+ cond2 = self.cond_as_dict(2, False, ['bar'], ['rab'])
+ cond3 = self.cond_as_dict(3, True, ['baz'], ['zab'])
+ expected = self.GET_conditions_dict([cond2, cond3, cond1])
+ self.check_json_get('/conditions', expected)
+ # test other sortings
+ # (NB: by .is_active has two items of =False, their order currently
+ # is not explicitly made predictable, so mail fail until we do)
+ expected['sort_by'] = '-title'
+ expected['conditions'] = self.as_id_list([cond1, cond3, cond2])
+ self.check_json_get('/conditions?sort_by=-title', expected)
+ expected['sort_by'] = 'is_active'
+ expected['conditions'] = self.as_id_list([cond1, cond2, cond3])
+ self.check_json_get('/conditions?sort_by=is_active', expected)
+ expected['sort_by'] = '-is_active'
+ expected['conditions'] = self.as_id_list([cond3, cond1, cond2])
+ self.check_json_get('/conditions?sort_by=-is_active', expected)
+ # test pattern matching on title
+ expected = self.GET_conditions_dict([cond2, cond3])
+ expected['pattern'] = 'ba'
+ self.check_json_get('/conditions?pattern=ba', expected)
+ # test pattern matching on description
+ assert isinstance(expected['_library'], dict)
+ expected['conditions'] = self.as_id_list([cond1])
+ expected['_library']['Condition'] = self.as_refs([cond1])
+ expected['pattern'] = 'of'
+ self.check_json_get('/conditions?pattern=of', expected)