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
9 class TestsWithDB(TestCaseWithDB):
10 """Tests not requiring DB setup."""
12 def test_Todo_by_id(self) -> None:
13 """Test creation and findability of Todos."""
14 day = Day('2024-01-01')
15 process = Process(None)
16 todo = Todo(None, process, False, day)
17 with self.assertRaises(NotFoundException):
18 todo.save(self.db_conn)
19 process.save_without_steps(self.db_conn)
20 todo.save(self.db_conn)
21 with self.assertRaises(NotFoundException):
22 _ = Todo.by_id(self.db_conn, 1)
23 day.save(self.db_conn)
24 self.assertEqual(Todo.by_id(self.db_conn, 1), todo)
25 with self.assertRaises(NotFoundException):
26 self.assertEqual(Todo.by_id(self.db_conn, 0), todo)
27 with self.assertRaises(NotFoundException):
28 self.assertEqual(Todo.by_id(self.db_conn, 2), todo)
30 def test_Todo_by_date(self) -> None:
31 """Test findability of Todos by date."""
32 day1 = Day('2024-01-01')
33 day2 = Day('2024-01-02')
34 process = Process(None)
35 process.save_without_steps(self.db_conn)
36 todo1 = Todo(None, process, False, day1)
37 todo1.save(self.db_conn)
38 todo2 = Todo(None, process, False, day1)
39 todo2.save(self.db_conn)
40 with self.assertRaises(NotFoundException):
41 _ = Todo.by_date(self.db_conn, day1.date)
42 day1.save(self.db_conn)
43 day2.save(self.db_conn)
44 self.assertEqual(Todo.by_date(self.db_conn, day1.date), [todo1, todo2])
45 self.assertEqual(Todo.by_date(self.db_conn, day2.date), [])
46 self.assertEqual(Todo.by_date(self.db_conn, 'foo'), [])
49 class TestsWithServer(TestCaseWithServer):
50 """Tests against our HTTP server/handler (and database)."""
52 def test_do_POST_todo(self) -> None:
53 """Test Todo posting of POST /day."""
54 form_data = {'title': '', 'description': '', 'effort': 1}
55 self.check_post(form_data, '/process?id=', 302, '/')
56 self.check_post(form_data, '/process?id=', 302, '/')
57 process1 = Process.by_id(self.db_conn, 1)
58 process2 = Process.by_id(self.db_conn, 2)
59 form_data = {'comment': ''}
60 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
61 self.assertEqual(Todo.by_date(self.db_conn, '2024-01-01'), [])
62 form_data['new_todo'] = str(process1.id_)
63 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
64 todos = Todo.by_date(self.db_conn, '2024-01-01')
65 self.assertEqual(1, len(todos))
67 self.assertEqual(todo1.id_, 1)
68 self.assertEqual(todo1.process.id_, process1.id_)
69 self.assertEqual(todo1.is_done, False)
70 form_data['new_todo'] = str(process2.id_)
71 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
72 todos = Todo.by_date(self.db_conn, '2024-01-01')
74 self.assertEqual(todo1.id_, 2)
75 self.assertEqual(todo1.process.id_, process2.id_)
76 self.assertEqual(todo1.is_done, False)
78 def test_do_GET_todo(self) -> None:
79 """Test GET /todo response codes."""
80 form_data = {'title': '', 'description': '', 'effort': 1}
81 self.check_post(form_data, '/process?id=', 302, '/')
82 form_data = {'comment': '', 'new_todo': 1}
83 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
84 self.check_get('/todo', 404)
85 self.check_get('/todo?id=', 404)
86 self.check_get('/todo?id=foo', 400)
87 self.check_get('/todo?id=0', 404)
88 self.check_get('/todo?id=1', 200)