1 """Test Todos module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer
3 from plomtask.todos import Todo
4 from plomtask.processes import Process
5 from plomtask.conditions import Condition
6 from plomtask.exceptions import (NotFoundException, BadFormatException,
10 class TestsWithDB(TestCaseWithDB):
11 """Tests requiring DB, but not server setup."""
13 def setUp(self) -> None:
15 self.date1 = '2024-01-01'
16 self.date2 = '2024-01-02'
17 self.proc = Process(None)
18 self.proc.save(self.db_conn)
19 self.cond1 = Condition(None)
20 self.cond1.save(self.db_conn)
21 self.cond2 = Condition(None)
22 self.cond2.save(self.db_conn)
24 def test_Todo_by_id(self) -> None:
25 """Test creation and findability of Todos."""
26 process_unsaved = Process(None)
27 todo = Todo(None, process_unsaved, False, self.date1)
28 with self.assertRaises(NotFoundException):
29 todo.save(self.db_conn)
30 process_unsaved.save(self.db_conn)
31 todo.save(self.db_conn)
32 self.assertEqual(Todo.by_id(self.db_conn, 1), todo)
33 with self.assertRaises(NotFoundException):
34 self.assertEqual(Todo.by_id(self.db_conn, 0), todo)
35 with self.assertRaises(NotFoundException):
36 self.assertEqual(Todo.by_id(self.db_conn, 2), todo)
38 def test_Todo_by_date(self) -> None:
39 """Test findability of Todos by date."""
40 t1 = Todo(None, self.proc, False, self.date1)
42 t2 = Todo(None, self.proc, False, self.date1)
44 self.assertEqual(Todo.by_date(self.db_conn, self.date1), [t1, t2])
45 self.assertEqual(Todo.by_date(self.db_conn, self.date2), [])
46 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
48 def test_Todo_from_process(self) -> None:
49 """Test spawning of Todo attributes from Process."""
50 assert isinstance(self.cond1.id_, int)
51 assert isinstance(self.cond2.id_, int)
52 self.proc.set_conditions(self.db_conn, [self.cond1.id_])
53 todo = Todo(None, self.proc, False, self.date1)
54 self.assertEqual(todo.conditions, [self.cond1])
55 todo.set_conditions(self.db_conn, [self.cond2.id_])
56 self.assertEqual(todo.conditions, [self.cond2])
57 self.assertEqual(self.proc.conditions, [self.cond1])
58 self.proc.set_fulfills(self.db_conn, [self.cond1.id_])
59 todo = Todo(None, self.proc, False, self.date1)
60 self.assertEqual(todo.fulfills, [self.cond1])
61 todo.set_fulfills(self.db_conn, [self.cond2.id_])
62 self.assertEqual(todo.fulfills, [self.cond2])
63 self.assertEqual(self.proc.fulfills, [self.cond1])
64 self.proc.set_undoes(self.db_conn, [self.cond1.id_])
65 todo = Todo(None, self.proc, False, self.date1)
66 self.assertEqual(todo.undoes, [self.cond1])
67 todo.set_undoes(self.db_conn, [self.cond2.id_])
68 self.assertEqual(todo.undoes, [self.cond2])
69 self.assertEqual(self.proc.undoes, [self.cond1])
71 def test_Todo_on_conditions(self) -> None:
72 """Test effect of Todos on Conditions."""
73 assert isinstance(self.cond1.id_, int)
74 assert isinstance(self.cond2.id_, int)
75 todo = Todo(None, self.proc, False, self.date1)
76 todo.save(self.db_conn)
77 todo.set_fulfills(self.db_conn, [self.cond1.id_])
78 todo.set_undoes(self.db_conn, [self.cond2.id_])
80 self.assertEqual(self.cond1.is_active, True)
81 self.assertEqual(self.cond2.is_active, False)
83 self.assertEqual(self.cond1.is_active, True)
84 self.assertEqual(self.cond2.is_active, False)
86 def test_Todo_enablers_disablers(self) -> None:
87 """Test Todo.enablers_for_at/disablers_for_at."""
88 assert isinstance(self.cond1.id_, int)
89 assert isinstance(self.cond2.id_, int)
90 todo1 = Todo(None, self.proc, False, self.date1)
91 todo1.save(self.db_conn)
92 todo1.set_fulfills(self.db_conn, [self.cond1.id_])
93 todo1.set_undoes(self.db_conn, [self.cond2.id_])
94 todo1.save(self.db_conn)
95 todo2 = Todo(None, self.proc, False, self.date1)
96 todo2.save(self.db_conn)
97 todo2.set_fulfills(self.db_conn, [self.cond2.id_])
98 todo2.save(self.db_conn)
99 todo3 = Todo(None, self.proc, False, self.date2)
100 todo3.save(self.db_conn)
101 todo3.set_fulfills(self.db_conn, [self.cond2.id_])
102 todo3.save(self.db_conn)
103 enablers = Todo.enablers_for_at(self.db_conn, self.cond1, self.date1)
104 self.assertEqual(enablers, [todo1])
105 enablers = Todo.enablers_for_at(self.db_conn, self.cond1, self.date2)
106 self.assertEqual(enablers, [])
107 disablers = Todo.disablers_for_at(self.db_conn, self.cond1, self.date1)
108 self.assertEqual(disablers, [])
109 disablers = Todo.disablers_for_at(self.db_conn, self.cond1, self.date2)
110 self.assertEqual(disablers, [])
111 enablers = Todo.enablers_for_at(self.db_conn, self.cond2, self.date1)
112 self.assertEqual(enablers, [todo2])
113 enablers = Todo.enablers_for_at(self.db_conn, self.cond2, self.date2)
114 self.assertEqual(enablers, [todo3])
115 disablers = Todo.disablers_for_at(self.db_conn, self.cond2, self.date1)
116 self.assertEqual(disablers, [todo1])
117 disablers = Todo.disablers_for_at(self.db_conn, self.cond2, self.date2)
118 self.assertEqual(disablers, [])
120 def test_Todo_children(self) -> None:
121 """Test Todo.children relations."""
122 todo_1 = Todo(None, self.proc, False, self.date1)
123 todo_2 = Todo(None, self.proc, False, self.date1)
124 todo_2.save(self.db_conn)
125 with self.assertRaises(HandledException):
126 todo_1.add_child(todo_2)
127 todo_1.save(self.db_conn)
128 todo_3 = Todo(None, self.proc, False, self.date1)
129 with self.assertRaises(HandledException):
130 todo_1.add_child(todo_3)
131 todo_3.save(self.db_conn)
132 todo_1.add_child(todo_3)
133 todo_1.save(self.db_conn)
134 assert isinstance(todo_1.id_, int)
135 todo_retrieved = Todo.by_id(self.db_conn, todo_1.id_)
136 self.assertEqual(todo_retrieved.children, [todo_3])
137 with self.assertRaises(BadFormatException):
138 todo_3.add_child(todo_1)
140 def test_Todo_conditioning(self) -> None:
141 """Test Todo.doability conditions."""
142 assert isinstance(self.cond1.id_, int)
143 todo_1 = Todo(None, self.proc, False, self.date1)
144 todo_1.save(self.db_conn)
145 todo_2 = Todo(None, self.proc, False, self.date1)
146 todo_2.save(self.db_conn)
147 todo_2.add_child(todo_1)
148 with self.assertRaises(BadFormatException):
149 todo_2.is_done = True
150 todo_1.is_done = True
151 todo_2.is_done = True
152 todo_2.is_done = False
153 todo_2.set_conditions(self.db_conn, [self.cond1.id_])
154 with self.assertRaises(BadFormatException):
155 todo_2.is_done = True
156 self.cond1.is_active = True
157 todo_2.is_done = True
159 def test_Todo_singularity(self) -> None:
160 """Test pointers made for single object keep pointing to it."""
161 todo = Todo(None, self.proc, False, self.date1)
162 todo.save(self.db_conn)
163 retrieved_todo = Todo.by_id(self.db_conn, 1)
165 self.assertEqual(retrieved_todo.is_done, True)
166 retrieved_todo = Todo.by_date(self.db_conn, self.date1)[0]
167 retrieved_todo.is_done = False
168 self.assertEqual(todo.is_done, False)
171 class TestsWithServer(TestCaseWithServer):
172 """Tests against our HTTP server/handler (and database)."""
174 def test_do_POST_day(self) -> None:
175 """Test Todo posting of POST /day."""
176 form_data = {'title': '', 'description': '', 'effort': 1}
177 self.check_post(form_data, '/process?id=', 302, '/')
178 self.check_post(form_data, '/process?id=', 302, '/')
179 proc = Process.by_id(self.db_conn, 1)
180 proc2 = Process.by_id(self.db_conn, 2)
181 form_data = {'comment': ''}
182 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
183 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
184 form_data['new_todo'] = str(proc.id_)
185 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
186 todos = Todo.by_date(self.db_conn, '2024-01-01')
187 self.assertEqual(1, len(todos))
189 self.assertEqual(todo1.id_, 1)
190 self.assertEqual(todo1.process.id_, proc.id_)
191 self.assertEqual(todo1.is_done, False)
192 form_data['new_todo'] = str(proc2.id_)
193 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
194 todos = Todo.by_date(self.db_conn, '2024-01-01')
196 self.assertEqual(todo1.id_, 2)
197 self.assertEqual(todo1.process.id_, proc2.id_)
198 self.assertEqual(todo1.is_done, False)
200 def test_do_POST_todo(self) -> None:
201 """Test POST /todo."""
202 def post_and_reload(form_data: dict[str, object],
203 status: int = 302) -> Todo:
204 self.check_post(form_data, '/todo?id=1', status, '/')
205 self.db_conn.cached_todos = {}
206 return Todo.by_date(self.db_conn, '2024-01-01')[0]
207 form_data = {'title': '', 'description': '', 'effort': 1}
208 self.check_post(form_data, '/process', 302, '/')
209 form_data = {'comment': '', 'new_todo': 1}
210 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
212 self.check_post(form_data, '/todo=', 404)
213 self.check_post(form_data, '/todo?id=', 400)
214 self.check_post(form_data, '/todo?id=FOO', 400)
215 self.check_post(form_data, '/todo?id=0', 404)
216 todo1 = post_and_reload(form_data)
217 self.assertEqual(todo1.children, [])
218 self.assertEqual(todo1.parents, [])
219 self.assertEqual(todo1.is_done, False)
220 form_data = {'done': ''}
221 todo1 = post_and_reload(form_data)
222 self.assertEqual(todo1.is_done, True)
224 todo1 = post_and_reload(form_data)
225 self.assertEqual(todo1.is_done, False)
226 form_data = {'adopt': 'foo'}
227 self.check_post(form_data, '/todo?id=1', 400)
228 form_data = {'adopt': 1}
229 self.check_post(form_data, '/todo?id=1', 400)
230 form_data = {'adopt': 2}
231 self.check_post(form_data, '/todo?id=1', 404)
232 form_data = {'comment': '', 'new_todo': 1}
233 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
234 form_data = {'adopt': 2}
235 todo1 = post_and_reload(form_data)
236 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
237 self.assertEqual(todo1.children, [todo2])
238 self.assertEqual(todo1.parents, [])
239 self.assertEqual(todo2.children, [])
240 self.assertEqual(todo2.parents, [todo1])
241 self.check_post(form_data, '/todo?id=1', 400, '/')
242 form_data = {'done': ''}
243 todo1 = post_and_reload(form_data, 400)
244 self.assertEqual(todo1.is_done, False)
246 def test_do_GET_todo(self) -> None:
247 """Test GET /todo response codes."""
248 form_data = {'title': '', 'description': '', 'effort': 1}
249 self.check_post(form_data, '/process?id=', 302, '/')
250 form_data = {'comment': '', 'new_todo': 1}
251 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
252 self.check_get('/todo', 400)
253 self.check_get('/todo?id=', 400)
254 self.check_get('/todo?id=foo', 400)
255 self.check_get('/todo?id=0', 404)
256 self.check_get('/todo?id=1', 200)