1 """Test Todos module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer
3 from plomtask.todos import Todo
4 from plomtask.days import Day
5 from plomtask.processes import Process
6 from plomtask.exceptions import (NotFoundException, BadFormatException,
10 class TestsWithDB(TestCaseWithDB):
11 """Tests requiring DB, but not server setup."""
13 def test_Todo_by_id(self) -> None:
14 """Test creation and findability of Todos."""
15 day = Day('2024-01-01')
16 process = Process(None)
17 todo = Todo(None, process, False, day)
18 with self.assertRaises(NotFoundException):
19 todo.save(self.db_conn)
20 process.save_without_steps(self.db_conn)
21 todo.save(self.db_conn)
22 self.assertEqual(Todo.by_id(self.db_conn, 1), todo)
23 with self.assertRaises(NotFoundException):
24 self.assertEqual(Todo.by_id(self.db_conn, 0), todo)
25 with self.assertRaises(NotFoundException):
26 self.assertEqual(Todo.by_id(self.db_conn, 2), todo)
28 def test_Todo_by_date(self) -> None:
29 """Test findability of Todos by date."""
30 day1 = Day('2024-01-01')
31 day2 = Day('2024-01-02')
32 process = Process(None)
33 process.save_without_steps(self.db_conn)
34 todo1 = Todo(None, process, False, day1)
35 todo1.save(self.db_conn)
36 todo2 = Todo(None, process, False, day1)
37 todo2.save(self.db_conn)
38 self.assertEqual(Todo.by_date(self.db_conn, day1.date), [todo1, todo2])
39 self.assertEqual(Todo.by_date(self.db_conn, day2.date), [])
40 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
42 def test_Todo_children(self) -> None:
43 """Test Todo.children relations."""
44 day = Day('2024-01-01')
45 process = Process(None)
46 process.save_without_steps(self.db_conn)
47 todo_1 = Todo(None, process, False, day)
48 todo_2 = Todo(None, process, False, day)
49 with self.assertRaises(HandledException):
50 todo_1.add_child(todo_2)
51 todo_1.save(self.db_conn)
52 with self.assertRaises(HandledException):
53 todo_1.add_child(todo_2)
54 todo_2.save(self.db_conn)
55 todo_1.add_child(todo_2)
56 todo_1.save(self.db_conn)
57 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
58 self.assertEqual(todo_retrieved.children, [todo_2])
59 with self.assertRaises(BadFormatException):
60 todo_2.add_child(todo_1)
62 def test_Todo_singularity(self) -> None:
63 """Test pointers made for single object keep pointing to it."""
64 day = Day('2024-01-01')
65 day.save(self.db_conn)
66 process = Process(None)
67 process.save_without_steps(self.db_conn)
68 todo = Todo(None, process, False, day)
69 todo.save(self.db_conn)
70 retrieved_todo = Todo.by_id(self.db_conn, 1)
72 self.assertEqual(retrieved_todo.is_done, True)
73 retrieved_todo = Todo.by_date(self.db_conn, '2024-01-01')[0]
74 retrieved_todo.is_done = False
75 self.assertEqual(todo.is_done, False)
78 class TestsWithServer(TestCaseWithServer):
79 """Tests against our HTTP server/handler (and database)."""
81 def test_do_POST_day(self) -> None:
82 """Test Todo posting of POST /day."""
83 form_data = {'title': '', 'description': '', 'effort': 1}
84 self.check_post(form_data, '/process?id=', 302, '/')
85 self.check_post(form_data, '/process?id=', 302, '/')
86 process1 = Process.by_id(self.db_conn, 1)
87 process2 = Process.by_id(self.db_conn, 2)
88 form_data = {'comment': ''}
89 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
90 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
91 form_data['new_todo'] = str(process1.id_)
92 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
93 todos = Todo.by_date(self.db_conn, '2024-01-01')
94 self.assertEqual(1, len(todos))
96 self.assertEqual(todo1.id_, 1)
97 self.assertEqual(todo1.process.id_, process1.id_)
98 self.assertEqual(todo1.is_done, False)
99 form_data['new_todo'] = str(process2.id_)
100 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
101 todos = Todo.by_date(self.db_conn, '2024-01-01')
103 self.assertEqual(todo1.id_, 2)
104 self.assertEqual(todo1.process.id_, process2.id_)
105 self.assertEqual(todo1.is_done, False)
107 def test_do_POST_todo(self) -> None:
108 """Test POST /todo."""
109 def post_and_reload(form_data: dict[str, object],
110 status: int = 302) -> Todo:
111 self.check_post(form_data, '/todo?id=1', status, '/')
112 self.db_conn.cached_todos = {}
113 return Todo.by_date(self.db_conn, '2024-01-01')[0]
114 form_data = {'title': '', 'description': '', 'effort': 1}
115 self.check_post(form_data, '/process', 302, '/')
116 form_data = {'comment': '', 'new_todo': 1}
117 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
119 self.check_post(form_data, '/todo=', 404)
120 self.check_post(form_data, '/todo?id=', 404)
121 self.check_post(form_data, '/todo?id=FOO', 400)
122 self.check_post(form_data, '/todo?id=0', 404)
123 todo1 = post_and_reload(form_data)
124 self.assertEqual(todo1.children, [])
125 self.assertEqual(todo1.parents, [])
126 self.assertEqual(todo1.is_done, False)
127 form_data = {'done': ''}
128 todo1 = post_and_reload(form_data)
129 self.assertEqual(todo1.is_done, True)
131 todo1 = post_and_reload(form_data)
132 self.assertEqual(todo1.is_done, False)
133 form_data = {'adopt': 'foo'}
134 self.check_post(form_data, '/todo?id=1', 400)
135 form_data = {'adopt': 1}
136 self.check_post(form_data, '/todo?id=1', 400)
137 form_data = {'adopt': 2}
138 self.check_post(form_data, '/todo?id=1', 404)
139 form_data = {'comment': '', 'new_todo': 1}
140 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
141 form_data = {'adopt': 2}
142 todo1 = post_and_reload(form_data)
143 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
144 self.assertEqual(todo1.children, [todo2])
145 self.assertEqual(todo1.parents, [])
146 self.assertEqual(todo2.children, [])
147 self.assertEqual(todo2.parents, [todo1])
148 self.check_post(form_data, '/todo?id=1', 400, '/')
149 form_data = {'done': ''}
150 todo1 = post_and_reload(form_data, 400)
151 self.assertEqual(todo1.is_done, False)
153 def test_do_GET_todo(self) -> None:
154 """Test GET /todo response codes."""
155 form_data = {'title': '', 'description': '', 'effort': 1}
156 self.check_post(form_data, '/process?id=', 302, '/')
157 form_data = {'comment': '', 'new_todo': 1}
158 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
159 self.check_get('/todo', 404)
160 self.check_get('/todo?id=', 404)
161 self.check_get('/todo?id=foo', 400)
162 self.check_get('/todo?id=0', 404)
163 self.check_get('/todo?id=1', 200)