home · contact · privacy
93b34d197f33aebbd5b096619c3804ca0b8f75ce
[plomtask] / tests / todos.py
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
7
8
9 class TestsWithDB(TestCaseWithDB):
10     """Tests not requiring DB setup."""
11
12     def test_Todo_by_date(self) -> None:
13         """Test creation and findability of Todos."""
14         day1 = Day('2024-01-01')
15         day2 = Day('2024-01-02')
16         process1 = Process(None)
17         todo1 = Todo(None, process1, False, day1)
18         with self.assertRaises(NotFoundException):
19             todo1.save(self.db_conn)
20         process1.save_without_steps(self.db_conn)
21         todo1.save(self.db_conn)
22         todo2 = Todo(None, process1, False, day1)
23         todo2.save(self.db_conn)
24         with self.assertRaises(NotFoundException):
25             Todo.by_date(self.db_conn, day1.date),
26         day1.save(self.db_conn)
27         day2.save(self.db_conn)
28         self.assertEqual(Todo.by_date(self.db_conn, day1.date), [todo1, todo2])
29         self.assertEqual(Todo.by_date(self.db_conn, day2.date), [])
30         self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
31
32
33 class TestsWithServer(TestCaseWithServer):
34     """Tests against our HTTP server/handler (and database)."""
35
36     def test_do_POST_todo(self) -> None:
37         """Test Todo posting of POST /day."""
38         form_data = {'title': '', 'description': '', 'effort': 1}
39         self.check_post(form_data, '/process?id=', 302, '/')
40         self.check_post(form_data, '/process?id=', 302, '/')
41         process1 = Process.by_id(self.db_conn, 1)
42         process2 = Process.by_id(self.db_conn, 2)
43         form_data = {'comment': ''}
44         self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
45         self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
46         form_data['new_todo'] = str(process1.id_)
47         self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
48         todos = Todo.by_date(self.db_conn, '2024-01-01')
49         self.assertEqual(1, len(todos))
50         todo1 = todos[0]
51         self.assertEqual(todo1.id_, 1)
52         self.assertEqual(todo1.process.id_, process1.id_)
53         self.assertEqual(todo1.is_done, False)
54         form_data['new_todo'] = str(process2.id_)
55         self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
56         todos = Todo.by_date(self.db_conn, '2024-01-01')
57         todo1 = todos[1]
58         self.assertEqual(todo1.id_, 2)
59         self.assertEqual(todo1.process.id_, process2.id_)
60         self.assertEqual(todo1.is_done, False)