X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/cards/%7B%7Bcard_id%7D%7D/form?a=blobdiff_plain;ds=inline;f=tests%2Fprocesses.py;h=1b20e217d077d826765f5a83c9a2b3250de38ba2;hb=HEAD;hp=481d2d4a691ab586035a05a6b938a33e0f48d612;hpb=c021152e6566c8374170de916c69d6b5c816cd54;p=plomtask diff --git a/tests/processes.py b/tests/processes.py index 481d2d4..422c283 100644 --- a/tests/processes.py +++ b/tests/processes.py @@ -1,30 +1,27 @@ """Test Processes module.""" -from tests.utils import TestCaseWithDB, TestCaseWithServer, TestCaseSansDB -from plomtask.processes import Process, ProcessStep, ProcessStepsNode +from typing import Any +from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer, + Expected) +from plomtask.processes import Process, ProcessStep from plomtask.conditions import Condition -from plomtask.exceptions import HandledException, NotFoundException -from plomtask.todos import Todo +from plomtask.exceptions import NotFoundException class TestsSansDB(TestCaseSansDB): """Module tests not requiring DB setup.""" checked_class = Process - do_id_test = True - versioned_defaults_to_test = {'title': 'UNNAMED', 'description': '', - 'effort': 1.0} class TestsSansDBProcessStep(TestCaseSansDB): """Module tests not requiring DB setup.""" checked_class = ProcessStep - do_id_test = True - default_init_args = [2, 3, 4] + default_init_kwargs = {'owner_id': 2, 'step_process_id': 3, + 'parent_step_id': 4} class TestsWithDB(TestCaseWithDB): """Module tests requiring DB setup.""" checked_class = Process - test_versioneds = {'title': str, 'description': str, 'effort': float} def three_processes(self) -> tuple[Process, Process, Process]: """Return three saved processes.""" @@ -46,12 +43,10 @@ class TestsWithDB(TestCaseWithDB): set_1 = [c1, c2] set_2 = [c2, c3] set_3 = [c1, c3] - p.set_conditions(self.db_conn, [c.id_ for c in set_1 - if isinstance(c.id_, int)]) - p.set_enables(self.db_conn, [c.id_ for c in set_2 - if isinstance(c.id_, int)]) - p.set_disables(self.db_conn, [c.id_ for c in set_3 - if isinstance(c.id_, int)]) + conds = [c.id_ for c in set_1 if isinstance(c.id_, int)] + enables = [c.id_ for c in set_2 if isinstance(c.id_, int)] + disables = [c.id_ for c in set_3 if isinstance(c.id_, int)] + p.set_condition_relations(self.db_conn, conds, [], enables, disables) p.save(self.db_conn) return p, set_1, set_2, set_3 @@ -77,107 +72,114 @@ class TestsWithDB(TestCaseWithDB): self.assertEqual(sorted(r.enables), sorted(set2)) self.assertEqual(sorted(r.disables), sorted(set3)) - def test_Process_steps(self) -> None: - """Test addition, nesting, and non-recursion of ProcessSteps""" - # pylint: disable=too-many-locals - # pylint: disable=too-many-statements - p1, p2, p3 = self.three_processes() - assert isinstance(p1.id_, int) - assert isinstance(p2.id_, int) - assert isinstance(p3.id_, int) - steps_p1: list[ProcessStep] = [] - # add step of process p2 as first (top-level) step to p1 - s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None) - steps_p1 += [s_p2_to_p1] - p1.set_steps(self.db_conn, steps_p1) - p1_dict: dict[int, ProcessStepsNode] = {} - p1_dict[1] = ProcessStepsNode(p2, None, True, {}) - self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - # add step of process p3 as second (top-level) step to p1 - s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None) - steps_p1 += [s_p3_to_p1] - p1.set_steps(self.db_conn, steps_p1) - p1_dict[2] = ProcessStepsNode(p3, None, True, {}) - self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - # add step of process p3 as first (top-level) step to p2, - steps_p2: list[ProcessStep] = [] - s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None) - steps_p2 += [s_p3_to_p2] - p2.set_steps(self.db_conn, steps_p2) - # expect it as implicit sub-step of p1's second (p3) step - p2_dict = {3: ProcessStepsNode(p3, None, False, {})} - p1_dict[1].steps[3] = p2_dict[3] - self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - # add step of process p2 as explicit sub-step to p1's second sub-step - s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_) - steps_p1 += [s_p2_to_p1_first] - p1.set_steps(self.db_conn, steps_p1) - seen_3 = ProcessStepsNode(p3, None, False, {}, False) - p1_dict[1].steps[3].seen = True - p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True, - {3: seen_3}) - self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - # add step of process p3 as explicit sub-step to non-existing p1 - # sub-step (of id=999), expect it to become another p1 top-level step - s_p3_to_p1_999 = ProcessStep(None, p1.id_, p3.id_, 999) - steps_p1 += [s_p3_to_p1_999] - p1.set_steps(self.db_conn, steps_p1) - p1_dict[5] = ProcessStepsNode(p3, None, True, {}) - self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - # add step of process p3 as explicit sub-step to p1's implicit p3 - # sub-step, expect it to become another p1 top-level step - s_p3_to_p1_impl_p3 = ProcessStep(None, p1.id_, p3.id_, s_p3_to_p2.id_) - steps_p1 += [s_p3_to_p1_impl_p3] - p1.set_steps(self.db_conn, steps_p1) - p1_dict[6] = ProcessStepsNode(p3, None, True, {}) - self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - self.assertEqual(p1.used_as_step_by(self.db_conn), []) - self.assertEqual(p2.used_as_step_by(self.db_conn), [p1]) - self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2]) - # # add step of process p3 as explicit sub-step to p1's first sub-step, - # # expect it to eliminate implicit p3 sub-step - # s_p3_to_p1_first_explicit = ProcessStep(None, p1.id_, p3.id_, - # s_p2_to_p1.id_) - # p1_dict[1].steps = {7: ProcessStepsNode(p3, 1, True, {})} - # p1_dict[2].steps[4].steps[3].seen = False - # steps_p1 += [s_p3_to_p1_first_explicit] - # p1.set_steps(self.db_conn, steps_p1) - # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - # ensure implicit steps non-top explicit steps are shown - s_p3_to_p2_first = ProcessStep(None, p2.id_, p3.id_, s_p3_to_p2.id_) - steps_p2 += [s_p3_to_p2_first] - p2.set_steps(self.db_conn, steps_p2) - p1_dict[1].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, {}, True) - p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, - {}, False) - self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) - # ensure suppressed step nodes are hidden - assert isinstance(s_p3_to_p2.id_, int) - p1.set_step_suppressions(self.db_conn, [s_p3_to_p2.id_]) - p1_dict[1].steps[3].steps = {} - p1_dict[1].steps[3].is_suppressed = True - p1_dict[2].steps[4].steps[3].steps = {} - p1_dict[2].steps[4].steps[3].is_suppressed = True - self.assertEqual(p1.get_steps(self.db_conn), p1_dict) + # def test_Process_steps(self) -> None: + # """Test addition, nesting, and non-recursion of ProcessSteps""" + # # pylint: disable=too-many-locals + # # pylint: disable=too-many-statements + # p1, p2, p3 = self.three_processes() + # assert isinstance(p1.id_, int) + # assert isinstance(p2.id_, int) + # assert isinstance(p3.id_, int) + # steps_p1: list[ProcessStep] = [] + # # add step of process p2 as first (top-level) step to p1 + # s_p2_to_p1 = ProcessStep(None, p1.id_, p2.id_, None) + # steps_p1 += [s_p2_to_p1] + # p1.set_steps(self.db_conn, steps_p1) + # p1_dict: dict[int, ProcessStepsNode] = {} + # p1_dict[1] = ProcessStepsNode(p2, None, True, {}) + # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # # add step of process p3 as second (top-level) step to p1 + # s_p3_to_p1 = ProcessStep(None, p1.id_, p3.id_, None) + # steps_p1 += [s_p3_to_p1] + # p1.set_steps(self.db_conn, steps_p1) + # p1_dict[2] = ProcessStepsNode(p3, None, True, {}) + # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # # add step of process p3 as first (top-level) step to p2, + # steps_p2: list[ProcessStep] = [] + # s_p3_to_p2 = ProcessStep(None, p2.id_, p3.id_, None) + # steps_p2 += [s_p3_to_p2] + # p2.set_steps(self.db_conn, steps_p2) + # # expect it as implicit sub-step of p1's second (p3) step + # p2_dict = {3: ProcessStepsNode(p3, None, False, {})} + # p1_dict[1].steps[3] = p2_dict[3] + # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # # add step of process p2 as explicit sub-step to p1's second sub-step + # s_p2_to_p1_first = ProcessStep(None, p1.id_, p2.id_, s_p3_to_p1.id_) + # steps_p1 += [s_p2_to_p1_first] + # p1.set_steps(self.db_conn, steps_p1) + # seen_3 = ProcessStepsNode(p3, None, False, {}, False) + # p1_dict[1].steps[3].seen = True + # p1_dict[2].steps[4] = ProcessStepsNode(p2, s_p3_to_p1.id_, True, + # {3: seen_3}) + # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # # add step of process p3 as explicit sub-step to non-existing p1 + # # sub-step (of id=999), expect it to become another p1 top-level step + # s_p3_to_p1_999 = ProcessStep(None, p1.id_, p3.id_, 999) + # steps_p1 += [s_p3_to_p1_999] + # p1.set_steps(self.db_conn, steps_p1) + # p1_dict[5] = ProcessStepsNode(p3, None, True, {}) + # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # # add step of process p3 as explicit sub-step to p1's implicit p3 + # # sub-step, expect it to become another p1 top-level step + # s_p3_to_p1_impl_p3 = ProcessStep(None, p1.id_, p3.id_, + # s_p3_to_p2.id_) + # steps_p1 += [s_p3_to_p1_impl_p3] + # p1.set_steps(self.db_conn, steps_p1) + # p1_dict[6] = ProcessStepsNode(p3, None, True, {}) + # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # self.assertEqual(p1.used_as_step_by(self.db_conn), []) + # self.assertEqual(p2.used_as_step_by(self.db_conn), [p1]) + # self.assertEqual(p3.used_as_step_by(self.db_conn), [p1, p2]) + # # # add step of process p3 as explicit sub-step to p1's first + # # # sub-step, expect it to eliminate implicit p3 sub-step + # # s_p3_to_p1_first_explicit = ProcessStep(None, p1.id_, p3.id_, + # # s_p2_to_p1.id_) + # # p1_dict[1].steps = {7: ProcessStepsNode(p3, 1, True, {})} + # # p1_dict[2].steps[4].steps[3].seen = False + # # steps_p1 += [s_p3_to_p1_first_explicit] + # # p1.set_steps(self.db_conn, steps_p1) + # # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # # ensure implicit steps non-top explicit steps are shown + # s_p3_to_p2_first = ProcessStep(None, p2.id_, p3.id_, s_p3_to_p2.id_) + # steps_p2 += [s_p3_to_p2_first] + # p2.set_steps(self.db_conn, steps_p2) + # p1_dict[1].steps[3].steps[7] = ProcessStepsNode(p3, 3, False, {}, + # True) + # p1_dict[2].steps[4].steps[3].steps[7] = ProcessStepsNode( + # p3, 3, False, {}, False) + # self.assertEqual(p1.get_steps(self.db_conn, None), p1_dict) + # # ensure suppressed step nodes are hidden + # assert isinstance(s_p3_to_p2.id_, int) + # p1.set_step_suppressions(self.db_conn, [s_p3_to_p2.id_]) + # p1_dict[1].steps[3].steps = {} + # p1_dict[1].steps[3].is_suppressed = True + # p1_dict[2].steps[4].steps[3].steps = {} + # p1_dict[2].steps[4].steps[3].is_suppressed = True + # self.assertEqual(p1.get_steps(self.db_conn), p1_dict) def test_Process_conditions(self) -> None: """Test setting Process.conditions/enables/disables.""" p = Process(None) p.save(self.db_conn) - for target in ('conditions', 'enables', 'disables'): - method = getattr(p, f'set_{target}') + targets = ['conditions', 'blockers', 'enables', 'disables'] + for i, target in enumerate(targets): c1, c2 = Condition(None), Condition(None) c1.save(self.db_conn) c2.save(self.db_conn) assert isinstance(c1.id_, int) assert isinstance(c2.id_, int) - method(self.db_conn, []) + args: list[list[int]] = [[], [], [], []] + args[i] = [] + p.set_condition_relations(self.db_conn, *args) self.assertEqual(getattr(p, target), []) - method(self.db_conn, [c1.id_]) + args[i] = [c1.id_] + p.set_condition_relations(self.db_conn, *args) self.assertEqual(getattr(p, target), [c1]) - method(self.db_conn, [c2.id_]) + args[i] = [c2.id_] + p.set_condition_relations(self.db_conn, *args) self.assertEqual(getattr(p, target), [c2]) - method(self.db_conn, [c1.id_, c2.id_]) + args[i] = [c1.id_, c2.id_] + p.set_condition_relations(self.db_conn, *args) self.assertEqual(getattr(p, target), [c1, c2]) def test_remove(self) -> None: @@ -190,26 +192,21 @@ class TestsWithDB(TestCaseWithDB): step = ProcessStep(None, p2.id_, p1.id_, None) p2.set_steps(self.db_conn, [step]) step_id = step.id_ - with self.assertRaises(HandledException): - p1.remove(self.db_conn) p2.set_steps(self.db_conn, []) with self.assertRaises(NotFoundException): + # check unset ProcessSteps actually cannot be found anymore assert step_id is not None ProcessStep.by_id(self.db_conn, step_id) p1.remove(self.db_conn) step = ProcessStep(None, p2.id_, p3.id_, None) p2.set_steps(self.db_conn, [step]) step_id = step.id_ + # check _can_ remove Process pointed to by ProcessStep.owner_id, and … p2.remove(self.db_conn) with self.assertRaises(NotFoundException): + # … being dis-owned eliminates ProcessStep assert step_id is not None ProcessStep.by_id(self.db_conn, step_id) - todo = Todo(None, p3, False, '2024-01-01') - todo.save(self.db_conn) - with self.assertRaises(HandledException): - p3.remove(self.db_conn) - todo.remove(self.db_conn) - p3.remove(self.db_conn) class TestsWithDBForProcessStep(TestCaseWithDB): @@ -236,155 +233,238 @@ class TestsWithDBForProcessStep(TestCaseWithDB): self.check_identity_with_cache_and_db([]) +class ExpectedGetProcess(Expected): + """Builder of expectations for GET /processes.""" + _default_dict = {'is_new': False, 'preset_top_step': None, 'n_todos': 0} + _on_empty_make_temp = ('Process', 'proc_as_dict') + + def __init__(self, + proc_id: int, + *args: Any, **kwargs: Any) -> None: + self._fields = {'process': proc_id, 'steps': []} + super().__init__(*args, **kwargs) + + @staticmethod + def stepnode_as_dict(step_id: int, + proc_id: int, + seen: bool = False, + steps: None | list[dict[str, object]] = None, + is_explicit: bool = True, + is_suppressed: bool = False) -> dict[str, object]: + # pylint: disable=too-many-arguments + """Return JSON of ProcessStepNode to expect.""" + return {'step': step_id, + 'process': proc_id, + 'seen': seen, + 'steps': steps if steps else [], + 'is_explicit': is_explicit, + 'is_suppressed': is_suppressed} + + def recalc(self) -> None: + """Update internal dictionary by subclass-specific rules.""" + super().recalc() + self._fields['process_candidates'] = self.as_ids( + self.lib_all('Process')) + self._fields['condition_candidates'] = self.as_ids( + self.lib_all('Condition')) + self._fields['owners'] = [ + s['owner_id'] for s in self.lib_all('ProcessStep') + if s['step_process_id'] == self._fields['process']] + + +class ExpectedGetProcesses(Expected): + """Builder of expectations for GET /processes.""" + _default_dict = {'sort_by': 'title', 'pattern': ''} + + def recalc(self) -> None: + """Update internal dictionary by subclass-specific rules.""" + super().recalc() + self._fields['processes'] = self.as_ids(self.lib_all('Process')) + + class TestsWithServer(TestCaseWithServer): """Module tests against our HTTP server/handler (and database).""" - def test_do_POST_process(self) -> None: + def _post_process(self, id_: int = 1, + form_data: dict[str, Any] | None = None + ) -> dict[str, Any]: + """POST basic Process.""" + if not form_data: + form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.1} + self.check_post(form_data, f'/process?id={id_}', + redir=f'/process?id={id_}') + return form_data + + def test_fail_POST_process(self) -> None: """Test POST /process and its effect on the database.""" - self.assertEqual(0, len(Process.all(self.db_conn))) - form_data = self.post_process() - self.assertEqual(1, len(Process.all(self.db_conn))) - self.check_post(form_data, '/process?id=FOO', 400) - self.check_post(form_data | {'effort': 'foo'}, '/process?id=', 400) - self.check_post({}, '/process?id=', 400) - self.check_post({'title': '', 'description': ''}, '/process?id=', 400) - self.check_post({'title': '', 'effort': 1.1}, '/process?id=', 400) - self.check_post({'description': '', 'effort': 1.0}, - '/process?id=', 400) - self.assertEqual(1, len(Process.all(self.db_conn))) - form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0} - self.post_process(2, form_data | {'condition': []}) - self.check_post(form_data | {'condition': [1]}, '/process?id=', 404) - self.check_post({'title': 'foo', 'description': 'foo', - 'is_active': False}, - '/condition', 302, '/condition?id=1') - self.post_process(3, form_data | {'condition': [1]}) - self.post_process(4, form_data | {'disables': [1]}) - self.post_process(5, form_data | {'enables': [1]}) - form_data['delete'] = '' - self.check_post(form_data, '/process?id=', 404) - self.check_post(form_data, '/process?id=6', 404) - self.check_post(form_data, '/process?id=5', 302, '/processes') - - def test_do_POST_process_steps(self) -> None: + valid_post = {'title': '', 'description': '', 'effort': 1.0} + # check payloads lacking minimum expecteds + self.check_post({}, '/process', 400) + self.check_post({'title': '', 'description': ''}, '/process', 400) + self.check_post({'title': '', 'effort': 1}, '/process', 400) + self.check_post({'description': '', 'effort': 1}, '/process', 400) + # check payloads of bad data types + self.check_post(valid_post | {'effort': ''}, '/process', 400) + # check references to non-existant items + self.check_post(valid_post | {'conditions': [1]}, '/process', 404) + self.check_post(valid_post | {'disables': [1]}, '/process', 404) + self.check_post(valid_post | {'blockers': [1]}, '/process', 404) + self.check_post(valid_post | {'enables': [1]}, '/process', 404) + self.check_post(valid_post | {'new_top_step': 2}, '/process', 404) + # check deletion of non-existant + self.check_post({'delete': ''}, '/process?id=1', 404) + + def test_basic_POST_process(self) -> None: + """Test basic GET/POST /process operations.""" + # check on un-saved + exp = ExpectedGetProcess(1) + exp.force('process_candidates', []) + self.check_json_get('/process?id=1', exp) + # check on minimal payload post + valid_post = {'title': 'foo', 'description': 'oof', 'effort': 2.3} + exp.unforce('process_candidates') + self.post_exp_process([exp], valid_post, 1) + self.check_json_get('/process?id=1', exp) + # check n_todos field + self.post_exp_day([], {'new_todo': ['1']}, '2024-01-01') + self.post_exp_day([], {'new_todo': ['1']}, '2024-01-02') + exp.set('n_todos', 2) + self.check_json_get('/process?id=1', exp) + # check cannot delete if Todos to Process + self.check_post({'delete': ''}, '/process?id=1', 500) + # check cannot delete if some ProcessStep's .step_process_id + self.post_exp_process([exp], valid_post, 2) + self.post_exp_process([exp], valid_post | {'new_top_step': 2}, 3) + self.check_post({'delete': ''}, '/process?id=2', 500) + # check successful deletion + self.post_exp_process([exp], valid_post, 4) + self.check_post({'delete': ''}, '/process?id=4', 302, '/processes') + exp = ExpectedGetProcess(4) + exp.set_proc_from_post(1, valid_post) + exp.set_proc_from_post(2, valid_post) + exp.set_proc_from_post(3, valid_post) + exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 3, 2)]) + exp.force('process_candidates', [1, 2, 3]) + self.check_json_get('/process?id=4', exp) + + def test_POST_process_steps(self) -> None: """Test behavior of ProcessStep posting.""" # pylint: disable=too-many-statements - form_data_1 = self.post_process(1) - self.post_process(2) - self.post_process(3) - # post first (top-level) step of process 2 to process 1 - form_data_1['new_top_step'] = [2] - self.post_process(1, form_data_1) - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(len(retrieved_process.explicit_steps), 1) - retrieved_step = retrieved_process.explicit_steps[0] - retrieved_step_id = retrieved_step.id_ - self.assertEqual(retrieved_step.step_process_id, 2) - self.assertEqual(retrieved_step.owner_id, 1) - self.assertEqual(retrieved_step.parent_step_id, None) - # post empty steps list to process, expect clean slate, and old step to - # completely disappear - form_data_1['new_top_step'] = [] - self.post_process(1, form_data_1) - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(retrieved_process.explicit_steps, []) - assert retrieved_step_id is not None - with self.assertRaises(NotFoundException): - ProcessStep.by_id(self.db_conn, retrieved_step_id) - # post new first (top_level) step of process 3 to process 1 - form_data_1['new_top_step'] = [3] - self.post_process(1, form_data_1) - retrieved_process = Process.by_id(self.db_conn, 1) - retrieved_step = retrieved_process.explicit_steps[0] - self.assertEqual(retrieved_step.step_process_id, 3) - self.assertEqual(retrieved_step.owner_id, 1) - self.assertEqual(retrieved_step.parent_step_id, None) - # post to process steps list without keeps, expect clean slate - form_data_1['new_top_step'] = [] - form_data_1['steps'] = [retrieved_step.id_] - self.post_process(1, form_data_1) - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(retrieved_process.explicit_steps, []) - # post to process empty steps list but keep, expect 400 - form_data_1['steps'] = [] - form_data_1['keep_step'] = [retrieved_step_id] - self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1') - # post to process steps list with keep on non-created step, expect 400 - form_data_1['steps'] = [retrieved_step_id] - form_data_1['keep_step'] = [retrieved_step_id] - self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1') - # post to process steps list with keep and process ID, expect 200 - form_data_1[f'step_{retrieved_step_id}_process_id'] = [2] - self.post_process(1, form_data_1) - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(len(retrieved_process.explicit_steps), 1) - retrieved_step = retrieved_process.explicit_steps[0] - self.assertEqual(retrieved_step.step_process_id, 2) - self.assertEqual(retrieved_step.owner_id, 1) - self.assertEqual(retrieved_step.parent_step_id, None) - # post nonsense, expect 400 and preservation of previous state - form_data_1['steps'] = ['foo'] - form_data_1['keep_step'] = [] - self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1') - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(len(retrieved_process.explicit_steps), 1) - retrieved_step = retrieved_process.explicit_steps[0] - self.assertEqual(retrieved_step.step_process_id, 2) - self.assertEqual(retrieved_step.owner_id, 1) - self.assertEqual(retrieved_step.parent_step_id, None) - # post to process steps list with keep and process ID, expect 200 - form_data_1['new_top_step'] = [3] - form_data_1['steps'] = [retrieved_step.id_] - form_data_1['keep_step'] = [retrieved_step.id_] - self.post_process(1, form_data_1) - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(len(retrieved_process.explicit_steps), 2) - retrieved_step_0 = retrieved_process.explicit_steps[0] - self.assertEqual(retrieved_step_0.step_process_id, 3) - self.assertEqual(retrieved_step_0.owner_id, 1) - self.assertEqual(retrieved_step_0.parent_step_id, None) - retrieved_step_1 = retrieved_process.explicit_steps[1] - self.assertEqual(retrieved_step_1.step_process_id, 2) - self.assertEqual(retrieved_step_1.owner_id, 1) - self.assertEqual(retrieved_step_1.parent_step_id, None) - # post to process steps list with keeps etc., but trigger recursion - form_data_1['new_top_step'] = [] - form_data_1['steps'] = [retrieved_step_0.id_, retrieved_step_1.id_] - form_data_1['keep_step'] = [retrieved_step_0.id_, retrieved_step_1.id_] - form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [2] - form_data_1[f'step_{retrieved_step_1.id_}_process_id'] = [1] - self.check_post(form_data_1, '/process?id=1', 400, '/process?id=1') - # check previous status preserved despite failed steps setting - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(len(retrieved_process.explicit_steps), 2) - retrieved_step_0 = retrieved_process.explicit_steps[0] - self.assertEqual(retrieved_step_0.step_process_id, 2) - self.assertEqual(retrieved_step_0.owner_id, 1) - self.assertEqual(retrieved_step_0.parent_step_id, None) - retrieved_step_1 = retrieved_process.explicit_steps[1] - self.assertEqual(retrieved_step_1.step_process_id, 3) - self.assertEqual(retrieved_step_1.owner_id, 1) - self.assertEqual(retrieved_step_1.parent_step_id, None) - # post sub-step to step - form_data_1[f'step_{retrieved_step_0.id_}_process_id'] = [3] - form_data_1[f'new_step_to_{retrieved_step_0.id_}'] = [3] - self.post_process(1, form_data_1) - retrieved_process = Process.by_id(self.db_conn, 1) - self.assertEqual(len(retrieved_process.explicit_steps), 3) - retrieved_step_0 = retrieved_process.explicit_steps[0] - self.assertEqual(retrieved_step_0.step_process_id, 2) - self.assertEqual(retrieved_step_0.owner_id, 1) - self.assertEqual(retrieved_step_0.parent_step_id, None) - retrieved_step_1 = retrieved_process.explicit_steps[1] - self.assertEqual(retrieved_step_1.step_process_id, 3) - self.assertEqual(retrieved_step_1.owner_id, 1) - self.assertEqual(retrieved_step_1.parent_step_id, None) - retrieved_step_2 = retrieved_process.explicit_steps[2] - self.assertEqual(retrieved_step_2.step_process_id, 3) - self.assertEqual(retrieved_step_2.owner_id, 1) - self.assertEqual(retrieved_step_2.parent_step_id, retrieved_step_1.id_) - - def test_do_GET(self) -> None: + url = '/process?id=1' + exp = ExpectedGetProcess(1) + self.post_exp_process([exp], {}, 1) + # post first (top-level) step of proc 2 to proc 1 by 'step_of' in 2 + self.post_exp_process([exp], {'step_of': 1}, 2) + exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2)]) + exp.set('steps', [exp.stepnode_as_dict(1, 2)]) + self.check_json_get(url, exp) + # post empty/absent steps list to process, expect clean slate, and old + # step to completely disappear + self.post_exp_process([exp], {}, 1) + exp.lib_wipe('ProcessStep') + exp.set('steps', []) + self.check_json_get(url, exp) + # post new step of proc2 to proc1 by 'new_top_step' + self.post_exp_process([exp], {'new_top_step': 2}, 1) + exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 2)]) + self.post_exp_process([exp], {'kept_steps': [1]}, 1) + exp.set('steps', [exp.stepnode_as_dict(1, 2)]) + self.check_json_get(url, exp) + # fail on single- and multi-step recursion + p_min = {'title': '', 'description': '', 'effort': 0} + self.check_post(p_min | {'new_top_step': 1}, url, 400) + self.check_post(p_min | {'step_of': 1}, url, 400) + self.post_exp_process([exp], {'new_top_step': 1}, 2) + self.check_post(p_min | {'step_of': 2, 'new_top_step': 2}, url, 400) + self.post_exp_process([exp], {}, 3) + self.post_exp_process([exp], {'step_of': 3}, 4) + self.check_post(p_min | {'new_top_step': 3, 'step_of': 4}, url, 400) + # post sibling steps + self.post_exp_process([exp], {}, 4) + self.post_exp_process([exp], {'new_top_step': 4}, 1) + self.post_exp_process([exp], {'kept_steps': [1], 'new_top_step': 4}, 1) + exp.lib_set('ProcessStep', [exp.procstep_as_dict(1, 1, 4), + exp.procstep_as_dict(2, 1, 4)]) + exp.set('steps', [exp.stepnode_as_dict(1, 4), + exp.stepnode_as_dict(2, 4)]) + self.check_json_get(url, exp) + # post sub-step chain + p = {'kept_steps': [1, 2], 'new_step_to_2': 4} + self.post_exp_process([exp], p, 1) + exp.lib_set('ProcessStep', [exp.procstep_as_dict(3, 1, 4, 2)]) + exp.set('steps', [exp.stepnode_as_dict(1, 4), + exp.stepnode_as_dict(2, 4, steps=[ + exp.stepnode_as_dict(3, 4)])]) + self.check_json_get(url, exp) + # fail posting sub-step that would cause recursion + self.post_exp_process([exp], {}, 6) + self.post_exp_process([exp], {'new_top_step': 6}, 5) + p = p_min | {'kept_steps': [1, 2, 3], 'new_step_to_2': 5, 'step_of': 6} + self.check_post(p, url, 400) + + def test_GET(self) -> None: """Test /process and /processes response codes.""" + self.check_get('/process', 200) + self.check_get('/process?id=', 200) + self.check_get('/process?id=1', 200) self.check_get_defaults('/process') self.check_get('/processes', 200) + + def test_fail_GET_process(self) -> None: + """Test invalid GET /process params.""" + # check for invalid IDs + self.check_get('/process?id=foo', 400) + self.check_get('/process?id=0', 500) + # check we catch invalid base64 + self.check_get('/process?title_b64=foo', 400) + # check failure on references to unknown processes; we create Process + # of ID=1 here so we know the 404 comes from step_to=2 etc. (that tie + # the Process displayed by /process to others), not from not finding + # the main Process itself + self.post_exp_process([], {}, 1) + self.check_get('/process?id=1&step_to=2', 404) + self.check_get('/process?id=1&has_step=2', 404) + + def test_GET_processes(self) -> None: + """Test GET /processes.""" + # pylint: disable=too-many-statements + # test empty result on empty DB, default-settings on empty params + exp = ExpectedGetProcesses() + self.check_json_get('/processes', exp) + # 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') + url = '/processes?sort_by=foo&pattern=bar&foo=x' + self.check_json_get(url, exp) + # test non-empty result, automatic (positive) sorting by title + proc1_post = {'title': 'foo', 'description': 'oof', 'effort': 1.0} + self.post_exp_process([exp], proc1_post, 1) + proc2_post = {'title': 'bar', 'description': 'rab', 'effort': 1.1} + self.post_exp_process([exp], proc2_post | {'new_top_step': [1]}, 2) + proc3_post = {'title': 'baz', 'description': 'zab', 'effort': 0.9} + self.post_exp_process([exp], proc3_post | {'new_top_step': [1]}, 3) + proc3_post = proc3_post | {'new_top_step': [2], 'kept_steps': [2]} + self.post_exp_process([exp], proc3_post, 3) + 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.set('pattern', '') + self.check_filter(exp, 'processes', 'sort_by', 'title', [2, 3, 1]) + # test other sortings + 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('sort_by', 'title') + exp.lib_del('Process', '1') + self.check_filter(exp, 'processes', 'pattern', 'ba', [2, 3]) + # test pattern matching on description + exp.lib_wipe('Process') + exp.lib_wipe('ProcessStep') + self.post_exp_process([exp], {'description': 'oof', 'effort': 1.0}, 1) + self.check_filter(exp, 'processes', 'pattern', 'of', [1])