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 form_data = {'title': '', 'description': '', 'effort': 1}
110 self.check_post(form_data, '/process', 302, '/')
111 form_data = {'comment': '', 'new_todo': 1}
112 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
114 self.check_post(form_data, '/todo=', 404)
115 self.check_post(form_data, '/todo?id=', 404)
116 self.check_post(form_data, '/todo?id=FOO', 400)
117 self.check_post(form_data, '/todo?id=0', 404)
118 self.check_post(form_data, '/todo?id=1', 302, '/')
119 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
120 self.assertEqual(todo1.children, [])
121 self.assertEqual(todo1.parents, [])
122 form_data = {'adopt': 'foo'}
123 self.check_post(form_data, '/todo?id=1', 400)
124 form_data = {'adopt': 1}
125 self.check_post(form_data, '/todo?id=1', 400)
126 form_data = {'adopt': 2}
127 self.check_post(form_data, '/todo?id=1', 404)
128 form_data = {'comment': '', 'new_todo': 1}
129 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
130 form_data = {'adopt': 2}
131 self.check_post(form_data, '/todo?id=1', 302, '/')
132 self.db_conn.cached_todos = {}
133 todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
134 todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
135 self.assertEqual(todo1.children, [todo2])
136 self.assertEqual(todo1.parents, [])
137 self.assertEqual(todo2.children, [])
138 self.assertEqual(todo2.parents, [todo1])
139 self.check_post(form_data, '/todo?id=1', 400, '/')
141 def test_do_GET_todo(self) -> None:
142 """Test GET /todo response codes."""
143 form_data = {'title': '', 'description': '', 'effort': 1}
144 self.check_post(form_data, '/process?id=', 302, '/')
145 form_data = {'comment': '', 'new_todo': 1}
146 self.check_post(form_data, '/day?date=2024-01-01', 302, '/')
147 self.check_get('/todo', 404)
148 self.check_get('/todo?id=', 404)
149 self.check_get('/todo?id=foo', 400)
150 self.check_get('/todo?id=0', 404)
151 self.check_get('/todo?id=1', 200)