From 37af8a589ecb172aa1118464e13cd9f480da99b7 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 19 Aug 2024 07:08:58 +0200 Subject: [PATCH] Some test clean-ups. --- tests/processes.py | 53 ++++++---------- tests/todos.py | 148 ++++----------------------------------------- 2 files changed, 33 insertions(+), 168 deletions(-) diff --git a/tests/processes.py b/tests/processes.py index 0fa352d..2e2f716 100644 --- a/tests/processes.py +++ b/tests/processes.py @@ -286,16 +286,6 @@ class TestsWithServer(TestCaseWithServer): """Module tests against our HTTP server/handler (and database).""" checked_class = Process - 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.""" valid_post = {'title': '', 'description': '', 'effort': 1.0} @@ -321,13 +311,12 @@ class TestsWithServer(TestCaseWithServer): self.check_json_get('/process?id=1', exp) # check on minimal payload post exp = ExpectedGetProcess(1) - valid_post = {'title': 'foo', 'description': 'oof', 'effort': 2.3} - self.post_exp_process([exp], valid_post, 1) + self.post_exp_process([exp], {}, 1) self.check_json_get('/process?id=1', exp) # check boolean 'calendarize' - self.post_exp_process([exp], valid_post | {'calendarize': True}, 1) + self.post_exp_process([exp], {'calendarize': True}, 1) self.check_json_get('/process?id=1', exp) - self.post_exp_process([exp], valid_post, 1) + self.post_exp_process([exp], {}, 1) self.check_json_get('/process?id=1', exp) # check n_todos field self.post_exp_day([], {'new_todo': ['1']}, '2024-01-01') @@ -337,19 +326,18 @@ class TestsWithServer(TestCaseWithServer): # 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.post_exp_process([exp], {}, 2) + self.post_exp_process([exp], {'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.post_exp_process([exp], {}, 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]) exp.set('is_new', True) + self.post_exp_process([exp], {}, 1) + self.post_exp_process([exp], {}, 2) + self.post_exp_process([exp], {}, 3) + exp.force('process_candidates', [1, 2, 3]) self.check_json_get('/process?id=4', exp) def test_POST_process_steps(self) -> None: @@ -434,17 +422,16 @@ class TestsWithServer(TestCaseWithServer): 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)]) + for i, t in enumerate([('foo', 'oof', 1.0, []), + ('bar', 'rab', 1.1, [1]), + ('baz', 'zab', 0.9, [1, 2])]): + payload = {'title': t[0], 'description': t[1], 'effort': t[2], + 'new_top_step': t[3]} + self.post_exp_process([exp], payload, i+1) + exp.lib_set('ProcessStep', + [exp.procstep_as_dict(1, owner_id=2, step_process_id=1), + exp.procstep_as_dict(2, owner_id=3, step_process_id=1), + exp.procstep_as_dict(3, owner_id=3, step_process_id=2)]) exp.set('pattern', '') self.check_filter(exp, 'processes', 'sort_by', 'title', [2, 3, 1]) # test other sortings diff --git a/tests/todos.py b/tests/todos.py index 834330b..5e849b0 100644 --- a/tests/todos.py +++ b/tests/todos.py @@ -2,9 +2,8 @@ from typing import Any from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer, Expected) -from plomtask.todos import Todo, TodoNode -from plomtask.processes import Process, ProcessStep -from plomtask.conditions import Condition +from plomtask.todos import Todo +from plomtask.processes import Process from plomtask.exceptions import BadFormatException, HandledException @@ -20,24 +19,19 @@ class TestsWithDB(TestCaseWithDB, TestCaseSansDB): def setUp(self) -> None: super().setUp() - self.date1 = '2024-01-01' - self.date2 = '2024-01-02' self.proc = Process(None) self.proc.save(self.db_conn) - self.cond1 = Condition(None) - self.cond1.save(self.db_conn) - self.cond2 = Condition(None) - self.cond2.save(self.db_conn) self.default_init_kwargs['process'] = self.proc def test_Todo_by_date(self) -> None: """Test findability of Todos by date.""" - t1 = Todo(None, self.proc, False, self.date1) + date1, date2 = '2024-01-01', '2024-01-02' + t1 = Todo(None, self.proc, False, date1) t1.save(self.db_conn) - t2 = Todo(None, self.proc, False, self.date1) + t2 = Todo(None, self.proc, False, date1) t2.save(self.db_conn) - self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2]) - self.assertEqual(Todo.by_date(self.db_conn, self.date2), []) + self.assertEqual(Todo.by_date(self.db_conn, date1), [t1, t2]) + self.assertEqual(Todo.by_date(self.db_conn, date2), []) with self.assertRaises(BadFormatException): self.assertEqual(Todo.by_date(self.db_conn, 'foo'), []) @@ -47,134 +41,18 @@ class TestsWithDB(TestCaseWithDB, TestCaseSansDB): def test_Todo_children(self) -> None: """Test Todo.children relations.""" - todo_1 = Todo(None, self.proc, False, self.date1) - todo_2 = Todo(None, self.proc, False, self.date1) + date1 = '2024-01-01' + todo_1 = Todo(None, self.proc, False, date1) + todo_2 = Todo(None, self.proc, False, date1) todo_2.save(self.db_conn) + # check un-saved Todo cannot parent with self.assertRaises(HandledException): todo_1.add_child(todo_2) todo_1.save(self.db_conn) - todo_3 = Todo(None, self.proc, False, self.date1) + todo_3 = Todo(None, self.proc, False, date1) + # check un-saved Todo cannot be parented with self.assertRaises(HandledException): todo_1.add_child(todo_3) - todo_3.save(self.db_conn) - todo_1.add_child(todo_3) - todo_1.save(self.db_conn) - assert isinstance(todo_1.id_, int) - todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_) - self.assertEqual(todo_retrieved.children, [todo_3]) - with self.assertRaises(BadFormatException): - todo_3.add_child(todo_1) - - def test_Todo_conditioning(self) -> None: - """Test Todo.doability conditions.""" - assert isinstance(self.cond1.id_, int) - todo_1 = Todo(None, self.proc, False, self.date1) - todo_1.save(self.db_conn) - todo_2 = Todo(None, self.proc, False, self.date1) - todo_2.save(self.db_conn) - todo_2.add_child(todo_1) - with self.assertRaises(BadFormatException): - todo_2.is_done = True - todo_1.is_done = True - todo_2.is_done = True - todo_2.is_done = False - todo_2.set_condition_relations( - self.db_conn, [self.cond1.id_], [], [], []) - with self.assertRaises(BadFormatException): - todo_2.is_done = True - self.cond1.is_active = True - todo_2.is_done = True - - def test_Todo_step_tree(self) -> None: - """Test self-configuration of TodoStepsNode tree for Day view.""" - - def todo_node_as_dict(node: TodoNode) -> dict[str, object]: - return {'todo': node.todo.id_, 'seen': node.seen, - 'children': [todo_node_as_dict(c) for c in node.children]} - - todo_1 = Todo(None, self.proc, False, self.date1) - todo_1.save(self.db_conn) - assert isinstance(todo_1.id_, int) - # test minimum - node_0 = TodoNode(todo_1, False, []) - cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set())) - cmp_1_dict = todo_node_as_dict(node_0) - self.assertEqual(cmp_0_dict, cmp_1_dict) - # test non_emtpy seen_todo does something - node_0.seen = True - cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree({todo_1.id_})) - cmp_1_dict = todo_node_as_dict(node_0) - self.assertEqual(cmp_0_dict, cmp_1_dict) - # test child shows up - todo_2 = Todo(None, self.proc, False, self.date1) - todo_2.save(self.db_conn) - assert isinstance(todo_2.id_, int) - todo_1.add_child(todo_2) - node_2 = TodoNode(todo_2, False, []) - node_0.children = [node_2] - node_0.seen = False - cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set())) - cmp_1_dict = todo_node_as_dict(node_0) - self.assertEqual(cmp_0_dict, cmp_1_dict) - # test child shows up with child - todo_3 = Todo(None, self.proc, False, self.date1) - todo_3.save(self.db_conn) - assert isinstance(todo_3.id_, int) - todo_2.add_child(todo_3) - node_3 = TodoNode(todo_3, False, []) - node_2.children = [node_3] - cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set())) - cmp_1_dict = todo_node_as_dict(node_0) - self.assertEqual(cmp_0_dict, cmp_1_dict) - # test same todo can be child-ed multiple times at different locations - todo_1.add_child(todo_3) - node_4 = TodoNode(todo_3, True, []) - node_0.children += [node_4] - cmp_0_dict = todo_node_as_dict(todo_1.get_step_tree(set())) - cmp_1_dict = todo_node_as_dict(node_0) - self.assertEqual(cmp_0_dict, cmp_1_dict) - - def test_Todo_ensure_children(self) -> None: - """Test parenthood guarantees of Todo.ensure_children.""" - assert isinstance(self.proc.id_, int) - proc2 = Process(None) - proc2.save(self.db_conn) - assert isinstance(proc2.id_, int) - proc3 = Process(None) - proc3.save(self.db_conn) - assert isinstance(proc3.id_, int) - proc4 = Process(None) - proc4.save(self.db_conn) - assert isinstance(proc4.id_, int) - # make proc4 step of proc3 - step = ProcessStep(None, proc3.id_, proc4.id_, None) - proc3.set_steps(self.db_conn, [step]) - # give proc2 three steps; 2× proc1, 1× proc3 - step1 = ProcessStep(None, proc2.id_, self.proc.id_, None) - step2 = ProcessStep(None, proc2.id_, self.proc.id_, None) - step3 = ProcessStep(None, proc2.id_, proc3.id_, None) - proc2.set_steps(self.db_conn, [step1, step2, step3]) - # test mere creation does nothing - todo_ignore = Todo(None, proc2, False, self.date1) - todo_ignore.save(self.db_conn) - self.assertEqual(todo_ignore.children, []) - # test create_with_children on step-less does nothing - todo_1 = Todo(None, self.proc, False, self.date1) - todo_1.save(self.db_conn) - todo_1.ensure_children(self.db_conn) - self.assertEqual(todo_1.children, []) - self.assertEqual(len(Todo.all(self.db_conn)), 2) - # test create_with_children adopts and creates, and down tree too - todo_2 = Todo(None, proc2, False, self.date1) - todo_2.save(self.db_conn) - todo_2.ensure_children(self.db_conn) - self.assertEqual(3, len(todo_2.children)) - self.assertEqual(todo_1, todo_2.children[0]) - self.assertEqual(self.proc, todo_2.children[2].process) - self.assertEqual(proc3, todo_2.children[1].process) - todo_3 = todo_2.children[1] - self.assertEqual(len(todo_3.children), 1) - self.assertEqual(todo_3.children[0].process, proc4) class ExpectedGetTodo(Expected): -- 2.30.2