X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=tests%2Ftodos.py;h=6ce5d6955be068355285c1532e12cd3ffb9e4204;hb=HEAD;hp=419d3dba122cb274110e30a6697cb5f40f8849d2;hpb=e06d3b744f88b2976399b0cbe08b526bb7e88907;p=plomtask diff --git a/tests/todos.py b/tests/todos.py index 419d3db..dd57ee4 100644 --- a/tests/todos.py +++ b/tests/todos.py @@ -1,15 +1,24 @@ """Test Todos module.""" -from tests.utils import TestCaseWithDB, TestCaseWithServer +from tests.utils import TestCaseSansDB, TestCaseWithDB, TestCaseWithServer from plomtask.todos import Todo, TodoNode -from plomtask.processes import Process +from plomtask.processes import Process, ProcessStep from plomtask.conditions import Condition from plomtask.exceptions import (NotFoundException, BadFormatException, HandledException) -class TestsWithDB(TestCaseWithDB): - """Tests requiring DB, but not server setup.""" +class TestsWithDB(TestCaseWithDB, TestCaseSansDB): + """Tests requiring DB, but not server setup. + + NB: We subclass TestCaseSansDB too, to pull in its .test_id_validation, + which for Todo wouldn't run without a DB being set up due to the need for + Processes with set IDs. + """ checked_class = Todo + default_init_kwargs = {'process': None, 'is_done': False, + 'date': '2024-01-01'} + # solely used for TestCaseSansDB.test_id_setting + default_init_args = [None, False, '2024-01-01'] def setUp(self) -> None: super().setUp() @@ -21,6 +30,8 @@ class TestsWithDB(TestCaseWithDB): self.cond1.save(self.db_conn) self.cond2 = Condition(None) self.cond2.save(self.db_conn) + self.default_init_kwargs['process'] = self.proc + self.default_init_args[0] = self.proc def test_Todo_init(self) -> None: """Test creation of Todo and what they default to.""" @@ -42,27 +53,6 @@ class TestsWithDB(TestCaseWithDB): self.assertEqual(todo_yes_id.enables, []) self.assertEqual(todo_yes_id.disables, []) - def test_Todo_saving_and_caching(self) -> None: - """Test .save.""" - kwargs = {'id_': 1, - 'process': self.proc, - 'is_done': False, - 'date': self.date1} - self.check_saving_and_caching(**kwargs) - todo = Todo(None, self.proc, False, self.date1) - todo.save(self.db_conn) - self.assertEqual(todo.id_, 2) - - def test_Todo_by_id(self) -> None: - """Test findability of Todos.""" - todo = Todo(1, self.proc, False, self.date1) - todo.save(self.db_conn) - self.assertEqual(Todo.by_id(self.db_conn, 1), todo) - with self.assertRaises(NotFoundException): - Todo.by_id(self.db_conn, 0) - with self.assertRaises(NotFoundException): - Todo.by_id(self.db_conn, 2) - def test_Todo_by_date(self) -> None: """Test findability of Todos by date.""" t1 = Todo(None, self.proc, False, self.date1) @@ -71,7 +61,8 @@ class TestsWithDB(TestCaseWithDB): 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, 'foo'), []) + with self.assertRaises(BadFormatException): + self.assertEqual(Todo.by_date(self.db_conn, 'foo'), []) def test_Todo_on_conditions(self) -> None: """Test effect of Todos on Conditions.""" @@ -134,10 +125,11 @@ class TestsWithDB(TestCaseWithDB): assert isinstance(todo_1.id_, int) # test minimum node_0 = TodoNode(todo_1, False, []) - self.assertEqual(todo_1.get_step_tree(set()), node_0) + self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict) # test non_emtpy seen_todo does something node_0.seen = True - self.assertEqual(todo_1.get_step_tree({todo_1.id_}), node_0) + self.assertEqual(todo_1.get_step_tree({todo_1.id_}).as_dict, + node_0.as_dict) # test child shows up todo_2 = Todo(None, self.proc, False, self.date1) todo_2.save(self.db_conn) @@ -146,7 +138,7 @@ class TestsWithDB(TestCaseWithDB): node_2 = TodoNode(todo_2, False, []) node_0.children = [node_2] node_0.seen = False - self.assertEqual(todo_1.get_step_tree(set()), node_0) + self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict) # test child shows up with child todo_3 = Todo(None, self.proc, False, self.date1) todo_3.save(self.db_conn) @@ -154,18 +146,16 @@ class TestsWithDB(TestCaseWithDB): todo_2.add_child(todo_3) node_3 = TodoNode(todo_3, False, []) node_2.children = [node_3] - self.assertEqual(todo_1.get_step_tree(set()), node_0) + self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_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] - self.assertEqual(todo_1.get_step_tree(set()), node_0) + self.assertEqual(todo_1.get_step_tree(set()).as_dict, node_0.as_dict) - def test_Todo_unsatisfied_steps(self) -> None: - """Test options of satisfying unfulfilled Process.explicit_steps.""" + def test_Todo_create_with_children(self) -> None: + """Test parenthood guaranteeds of Todo.create_with_children.""" assert isinstance(self.proc.id_, int) - todo_1 = Todo(None, self.proc, False, self.date1) - todo_1.save(self.db_conn) proc2 = Process(None) proc2.save(self.db_conn) assert isinstance(proc2.id_, int) @@ -175,57 +165,72 @@ class TestsWithDB(TestCaseWithDB): proc4 = Process(None) proc4.save(self.db_conn) assert isinstance(proc4.id_, int) - proc3.set_steps(self.db_conn, [(None, proc4.id_, None)]) - proc2.set_steps(self.db_conn, [(None, self.proc.id_, None), - (None, self.proc.id_, None), - (None, proc3.id_, None)]) - todo_2 = Todo(None, proc2, False, self.date1) - todo_2.save(self.db_conn) - # test empty adoption does nothing - todo_2.adopt_from([]) - self.assertEqual(todo_2.children, []) - # test basic adoption - todo_2.adopt_from([todo_1]) - self.assertEqual(todo_2.children, [todo_1]) - self.assertEqual(todo_1.parents, [todo_2]) - # test making missing children - todo_2.make_missing_children(self.db_conn) - todo_3 = Todo.by_id(self.db_conn, 3) - todo_4 = Todo.by_id(self.db_conn, 4) - self.assertEqual(todo_2.children, [todo_1, todo_3, todo_4]) - self.assertEqual(todo_3.process, self.proc) - self.assertEqual(todo_3.parents, [todo_2]) - self.assertEqual(todo_3.children, []) - self.assertEqual(todo_4.process, proc3) - self.assertEqual(todo_4.parents, [todo_2]) - # test .make_missing_children doesn't further than top-level - self.assertEqual(todo_4.children, []) - # test .make_missing_children lower down the tree - todo_4.make_missing_children(self.db_conn) - todo_5 = Todo.by_id(self.db_conn, 5) - self.assertEqual(todo_5.process, proc4) - self.assertEqual(todo_4.children, [todo_5]) - self.assertEqual(todo_5.parents, [todo_4]) - - def test_Todo_singularity(self) -> None: - """Test pointers made for single object keep pointing to it.""" - self.check_singularity('is_done', True, self.proc, False, self.date1) + # 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.create_with_children(self.db_conn, self.proc.id_, + self.date1) + 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.create_with_children(self.db_conn, proc2.id_, self.date1) + 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) def test_Todo_remove(self) -> None: """Test removal.""" todo_1 = Todo(None, self.proc, False, self.date1) todo_1.save(self.db_conn) + assert todo_1.id_ is not None todo_0 = Todo(None, self.proc, False, self.date1) todo_0.save(self.db_conn) todo_0.add_child(todo_1) todo_2 = Todo(None, self.proc, False, self.date1) todo_2.save(self.db_conn) todo_1.add_child(todo_2) + todo_1_id = todo_1.id_ todo_1.remove(self.db_conn) with self.assertRaises(NotFoundException): - Todo.by_id(self.db_conn, todo_1.id_) + Todo.by_id(self.db_conn, todo_1_id) self.assertEqual(todo_0.children, []) self.assertEqual(todo_2.parents, []) + todo_2.comment = 'foo' + with self.assertRaises(HandledException): + todo_2.remove(self.db_conn) + todo_2.comment = '' + todo_2.effort = 5 + with self.assertRaises(HandledException): + todo_2.remove(self.db_conn) + + def test_Todo_autoremoval(self) -> None: + """"Test automatic removal for Todo.effort < 0.""" + todo_1 = Todo(None, self.proc, False, self.date1) + todo_1.save(self.db_conn) + todo_1.comment = 'foo' + todo_1.effort = -0.1 + todo_1.save(self.db_conn) + assert todo_1.id_ is not None + Todo.by_id(self.db_conn, todo_1.id_) + todo_1.comment = '' + todo_1_id = todo_1.id_ + todo_1.save(self.db_conn) + with self.assertRaises(NotFoundException): + Todo.by_id(self.db_conn, todo_1_id) class TestsWithServer(TestCaseWithServer): @@ -237,22 +242,28 @@ class TestsWithServer(TestCaseWithServer): self.post_process(2) proc = Process.by_id(self.db_conn, 1) proc2 = Process.by_id(self.db_conn, 2) - form_data = {'day_comment': ''} - self.check_post(form_data, '/day?date=2024-01-01', 302) + form_data = {'day_comment': '', 'make_type': 'full'} + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), []) + proc = Process.by_id(self.db_conn, 1) form_data['new_todo'] = str(proc.id_) - self.check_post(form_data, '/day?date=2024-01-01', 302) + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) todos = Todo.by_date(self.db_conn, '2024-01-01') self.assertEqual(1, len(todos)) todo1 = todos[0] self.assertEqual(todo1.id_, 1) + proc = Process.by_id(self.db_conn, 1) self.assertEqual(todo1.process.id_, proc.id_) self.assertEqual(todo1.is_done, False) + proc2 = Process.by_id(self.db_conn, 2) form_data['new_todo'] = str(proc2.id_) - self.check_post(form_data, '/day?date=2024-01-01', 302) + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) todos = Todo.by_date(self.db_conn, '2024-01-01') todo1 = todos[1] self.assertEqual(todo1.id_, 2) + proc2 = Process.by_id(self.db_conn, 1) + todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0] + self.assertEqual(todo1.id_, 1) self.assertEqual(todo1.process.id_, proc2.id_) self.assertEqual(todo1.is_done, False) @@ -264,11 +275,12 @@ class TestsWithServer(TestCaseWithServer): return Todo.by_date(self.db_conn, '2024-01-01')[0] # test minimum self.post_process() - self.check_post({'day_comment': '', 'new_todo': 1}, - '/day?date=2024-01-01', 302) + self.check_post({'day_comment': '', 'new_todo': 1, + 'make_type': 'full'}, + '/day?date=2024-01-01&make_type=full', 302) # test posting to bad URLs self.check_post({}, '/todo=', 404) - self.check_post({}, '/todo?id=', 400) + self.check_post({}, '/todo?id=', 404) self.check_post({}, '/todo?id=FOO', 400) self.check_post({}, '/todo?id=0', 404) # test posting naked entity @@ -287,8 +299,9 @@ class TestsWithServer(TestCaseWithServer): self.check_post({'adopt': 1}, '/todo?id=1', 400) self.check_post({'adopt': 2}, '/todo?id=1', 404) # test posting second todo of same process - self.check_post({'day_comment': '', 'new_todo': 1}, - '/day?date=2024-01-01', 302) + self.check_post({'day_comment': '', 'new_todo': 1, + 'make_type': 'full'}, + '/day?date=2024-01-01&make_type=full', 302) # test todo 1 adopting todo 2 todo1 = post_and_reload({'adopt': 2}) todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1] @@ -313,10 +326,10 @@ class TestsWithServer(TestCaseWithServer): """Test Todos posted to Day view may adopt existing Todos.""" form_data = self.post_process() form_data = self.post_process(2, form_data | {'new_top_step': 1}) - form_data = {'day_comment': '', 'new_todo': 1} - self.check_post(form_data, '/day?date=2024-01-01', 302) + form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'} + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) form_data['new_todo'] = 2 - self.check_post(form_data, '/day?date=2024-01-01', 302) + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0] todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1] self.assertEqual(todo1.children, []) @@ -328,8 +341,9 @@ class TestsWithServer(TestCaseWithServer): """Test multiple Todos can be posted to Day view.""" form_data = self.post_process() form_data = self.post_process(2) - form_data = {'day_comment': '', 'new_todo': [1, 2]} - self.check_post(form_data, '/day?date=2024-01-01', 302) + form_data = {'day_comment': '', 'new_todo': [1, 2], + 'make_type': 'full'} + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0] todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1] self.assertEqual(todo1.process.id_, 1) @@ -337,46 +351,79 @@ class TestsWithServer(TestCaseWithServer): def test_do_POST_day_todo_multiple_inner_adoption(self) -> None: """Test multiple Todos can be posted to Day view w. inner adoption.""" + + def key_order_func(t: Todo) -> int: + assert isinstance(t.process.id_, int) + return t.process.id_ + + def check_adoption(date: str, new_todos: list[int]) -> None: + form_data = {'day_comment': '', 'new_todo': new_todos, + 'make_type': 'full'} + self.check_post(form_data, f'/day?date={date}&make_type=full', 302) + day_todos = Todo.by_date(self.db_conn, date) + day_todos.sort(key=key_order_func) + todo1 = day_todos[0] + todo2 = day_todos[1] + self.assertEqual(todo1.children, []) + self.assertEqual(todo1.parents, [todo2]) + self.assertEqual(todo2.children, [todo1]) + self.assertEqual(todo2.parents, []) + + def check_nesting_adoption(process_id: int, date: str, + new_top_steps: list[int]) -> None: + form_data = {'title': '', 'description': '', 'effort': 1, + 'step_of': [2]} + form_data = self.post_process(1, form_data) + form_data['new_top_step'] = new_top_steps + form_data['step_of'] = [] + form_data = self.post_process(process_id, form_data) + form_data = {'day_comment': '', 'new_todo': [process_id], + 'make_type': 'full'} + self.check_post(form_data, f'/day?date={date}&make_type=full', 302) + day_todos = Todo.by_date(self.db_conn, date) + day_todos.sort(key=key_order_func, reverse=True) + self.assertEqual(len(day_todos), 3) + todo1 = day_todos[0] # process of process_id + todo2 = day_todos[1] # process 2 + todo3 = day_todos[2] # process 1 + self.assertEqual(sorted(todo1.children), sorted([todo2, todo3])) + self.assertEqual(todo1.parents, []) + self.assertEqual(todo2.children, [todo3]) + self.assertEqual(todo2.parents, [todo1]) + self.assertEqual(todo3.children, []) + self.assertEqual(sorted(todo3.parents), sorted([todo2, todo1])) + form_data = self.post_process() form_data = self.post_process(2, form_data | {'new_top_step': 1}) - form_data = {'day_comment': '', 'new_todo': [1, 2]} - self.check_post(form_data, '/day?date=2024-01-01', 302) - todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0] - todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1] - self.assertEqual(todo1.children, []) - self.assertEqual(todo1.parents, [todo2]) - self.assertEqual(todo2.children, [todo1]) - self.assertEqual(todo2.parents, []) - # check process ID order does not affect end result - form_data = {'day_comment': '', 'new_todo': [2, 1]} - self.check_post(form_data, '/day?date=2024-01-02', 302) - todo1 = Todo.by_date(self.db_conn, '2024-01-02')[1] - todo2 = Todo.by_date(self.db_conn, '2024-01-02')[0] - self.assertEqual(todo1.children, []) - self.assertEqual(todo1.parents, [todo2]) - self.assertEqual(todo2.children, [todo1]) - self.assertEqual(todo2.parents, []) + check_adoption('2024-01-01', [1, 2]) + check_adoption('2024-01-02', [2, 1]) + check_nesting_adoption(3, '2024-01-03', [1, 2]) + check_nesting_adoption(4, '2024-01-04', [2, 1]) def test_do_POST_day_todo_doneness(self) -> None: """Test Todo doneness can be posted to Day view.""" - form_data = self.post_process() - form_data = {'day_comment': '', 'new_todo': [1]} - self.check_post(form_data, '/day?date=2024-01-01', 302) + self.post_process() + form_data = {'day_comment': '', 'new_todo': [1], 'make_type': 'full'} + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) + todo = Todo.by_date(self.db_conn, '2024-01-01')[0] + form_data = {'day_comment': '', 'todo_id': [1], 'make_type': 'full', + 'comment': [''], 'done': [], 'effort': ['']} + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) todo = Todo.by_date(self.db_conn, '2024-01-01')[0] - form_data = {'day_comment': '', 'todo_id': [1]} - self.check_post(form_data, '/day?date=2024-01-01', 302) self.assertEqual(todo.is_done, False) - form_data = {'day_comment': '', 'todo_id': [1], 'done': [1]} - self.check_post(form_data, '/day?date=2024-01-01', 302) + form_data = {'day_comment': '', 'todo_id': [1], 'done': [1], + 'make_type': 'full', 'comment': [''], 'effort': ['']} + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) + todo = Todo.by_date(self.db_conn, '2024-01-01')[0] self.assertEqual(todo.is_done, True) def test_do_GET_todo(self) -> None: """Test GET /todo response codes.""" self.post_process() - form_data = {'day_comment': '', 'new_todo': 1} - self.check_post(form_data, '/day?date=2024-01-01', 302) - self.check_get('/todo', 400) - self.check_get('/todo?id=', 400) + form_data = {'day_comment': '', 'new_todo': 1, 'make_type': 'full'} + self.check_post(form_data, '/day?date=2024-01-01&make_type=full', 302) + self.check_get('/todo', 404) + self.check_get('/todo?id=', 404) self.check_get('/todo?id=foo', 400) self.check_get('/todo?id=0', 404) self.check_get('/todo?id=1', 200)