From 051c62713adb93a233713589222efde640fd994d Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 8 Aug 2024 10:13:42 +0200 Subject: [PATCH] Minor tests refactoring. --- tests/conditions.py | 26 ++++++++------------------ tests/processes.py | 42 +++++++++++------------------------------- tests/utils.py | 8 ++++++++ 3 files changed, 27 insertions(+), 49 deletions(-) diff --git a/tests/conditions.py b/tests/conditions.py index 3edafd6..6feda94 100644 --- a/tests/conditions.py +++ b/tests/conditions.py @@ -138,33 +138,23 @@ class TestsWithServer(TestCaseWithServer): exp.set('sort_by', 'title') # for clarity (already default) self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x', exp) # test non-empty result, automatic (positive) sorting by title + exp.set('pattern', '') post_cond1 = {'is_active': 0, 'title': 'foo', 'description': 'oof'} post_cond2 = {'is_active': 0, 'title': 'bar', 'description': 'rab'} post_cond3 = {'is_active': 1, 'title': 'baz', 'description': 'zab'} for i, post in enumerate([post_cond1, post_cond2, post_cond3]): self.post_exp_cond([exp], i+1, post, '', f'?id={i+1}') - exp.set('pattern', '') - exp.force('conditions', [2, 3, 1]) - self.check_json_get('/conditions', exp) + self.check_filter(exp, 'conditions', 'sort_by', 'title', [2, 3, 1]) # test other sortings - exp.set('sort_by', '-title') - exp.force('conditions', [1, 3, 2]) - self.check_json_get('/conditions?sort_by=-title', exp) - exp.set('sort_by', 'is_active') - exp.force('conditions', [1, 2, 3]) - self.check_json_get('/conditions?sort_by=is_active', exp) - exp.set('sort_by', '-is_active') - exp.force('conditions', [3, 2, 1]) - self.check_json_get('/conditions?sort_by=-is_active', exp) + self.check_filter(exp, 'conditions', 'sort_by', '-title', [1, 3, 2]) + self.check_filter(exp, 'conditions', 'sort_by', 'is_active', [1, 2, 3]) + self.check_filter(exp, 'conditions', 'sort_by', '-is_active', + [3, 2, 1]) # test pattern matching on title exp.set('sort_by', 'title') - exp.set('pattern', 'ba') - exp.force('conditions', [2, 3]) exp.lib_del('Condition', 1) - self.check_json_get('/conditions?pattern=ba', exp) + self.check_filter(exp, 'conditions', 'pattern', 'ba', [2, 3]) # test pattern matching on description - exp.set('pattern', 'of') exp.lib_wipe('Condition') exp.set_cond_from_post(1, post_cond1) - exp.force('conditions', [1]) - self.check_json_get('/conditions?pattern=of', exp) + self.check_filter(exp, 'conditions', 'pattern', 'of', [1]) diff --git a/tests/processes.py b/tests/processes.py index 649aee5..422c283 100644 --- a/tests/processes.py +++ b/tests/processes.py @@ -434,7 +434,7 @@ class TestsWithServer(TestCaseWithServer): # 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 - exp.set('pattern', 'bar') # preserved despite zero effect! + exp.set('pattern', 'bar') url = '/processes?sort_by=foo&pattern=bar&foo=x' self.check_json_get(url, exp) # test non-empty result, automatic (positive) sorting by title @@ -449,42 +449,22 @@ class TestsWithServer(TestCaseWithServer): exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 2, 1), exp.procstep_as_dict(2, 3, 1), exp.procstep_as_dict(3, 3, 2)]) - exp.lib_get('Process', '') exp.set('pattern', '') - exp.force('processes', [2, 3, 1]) - self.check_json_get('/processes', exp) + self.check_filter(exp, 'processes', 'sort_by', 'title', [2, 3, 1]) # test other sortings - exp.set('sort_by', '-title') - exp.force('processes', [1, 3, 2]) - self.check_json_get('/processes?sort_by=-title', exp) - exp.set('sort_by', 'effort') - exp.force('processes', [3, 1, 2]) - self.check_json_get('/processes?sort_by=effort', exp) - exp.set('sort_by', '-effort') - exp.force('processes', [2, 1, 3]) - self.check_json_get('/processes?sort_by=-effort', exp) - exp.set('sort_by', 'steps') - exp.force('processes', [1, 2, 3]) - self.check_json_get('/processes?sort_by=steps', exp) - exp.set('sort_by', '-steps') - exp.force('processes', [3, 2, 1]) - self.check_json_get('/processes?sort_by=-steps', exp) - exp.set('sort_by', 'owners') - exp.force('processes', [3, 2, 1]) - self.check_json_get('/processes?sort_by=owners', exp) - exp.set('sort_by', '-owners') - exp.force('processes', [1, 2, 3]) - self.check_json_get('/processes?sort_by=-owners', exp) + self.check_filter(exp, 'processes', 'sort_by', '-title', [1, 3, 2]) + self.check_filter(exp, 'processes', 'sort_by', 'effort', [3, 1, 2]) + self.check_filter(exp, 'processes', 'sort_by', '-effort', [2, 1, 3]) + self.check_filter(exp, 'processes', 'sort_by', 'steps', [1, 2, 3]) + self.check_filter(exp, 'processes', 'sort_by', '-steps', [3, 2, 1]) + self.check_filter(exp, 'processes', 'sort_by', 'owners', [3, 2, 1]) + self.check_filter(exp, 'processes', 'sort_by', '-owners', [1, 2, 3]) # test pattern matching on title - exp.set('pattern', 'ba') exp.set('sort_by', 'title') exp.lib_del('Process', '1') - exp.force('processes', [2, 3]) - self.check_json_get('/processes?pattern=ba', exp) + self.check_filter(exp, 'processes', 'pattern', 'ba', [2, 3]) # test pattern matching on description - exp.set('pattern', 'of') exp.lib_wipe('Process') exp.lib_wipe('ProcessStep') self.post_exp_process([exp], {'description': 'oof', 'effort': 1.0}, 1) - exp.force('processes', [1]) - self.check_json_get('/processes?pattern=of', exp) + self.check_filter(exp, 'processes', 'pattern', 'of', [1]) diff --git a/tests/utils.py b/tests/utils.py index 1a306d1..e04fba6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -911,6 +911,14 @@ class TestCaseWithServer(TestCaseWithDB): exp.set_proc_from_post(id_, payload) return payload + def check_filter(self, exp: Expected, category: str, key: str, + val: str, list_ids: list[int]) -> None: + """Check GET /{category}?{key}={val} sorts to list_ids.""" + # pylint: disable=too-many-arguments + exp.set(key, val) + exp.force(category, list_ids) + self.check_json_get(f'/{category}?{key}={val}', exp) + def check_redirect(self, target: str) -> None: """Check that self.conn answers with a 302 redirect to target.""" response = self.conn.getresponse() -- 2.30.2