home · contact · privacy
Minor tests refactoring.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 8 Aug 2024 08:13:42 +0000 (10:13 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 8 Aug 2024 08:13:42 +0000 (10:13 +0200)
tests/conditions.py
tests/processes.py
tests/utils.py

index 3edafd67eeb9707286d2f0b22f66ac84ba2b6d6f..6feda94fb8bc9007773bb0f47f08ed6dfd49d545 100644 (file)
@@ -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])
index 649aee5f96663780ed91f8e447f7bee47d8c50b0..422c28324a9b04c79c5ee96cb50ec1032f7ead96 100644 (file)
@@ -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])
index 1a306d10a8286a264db22fbf9663f28eba3b2e1b..e04fba6ac84ef66dfe02eb5611d9dd115c0be848 100644 (file)
@@ -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()