home · contact · privacy
Refactor Process/ProcessStep setting and saving.
[plomtask] / tests / conditions.py
1 """Test Conditions module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer
3 from plomtask.conditions import Condition
4 from plomtask.exceptions import NotFoundException
5
6
7 class TestsWithDB(TestCaseWithDB):
8     """Tests requiring DB, but not server setup."""
9
10     def test_Condition_by_id(self) -> None:
11         """Test creation and findability."""
12         condition = Condition(None, False)
13         condition.save(self.db_conn)
14         self.assertEqual(Condition.by_id(self.db_conn, 1), condition)
15         with self.assertRaises(NotFoundException):
16             self.assertEqual(Condition.by_id(self.db_conn, 0), condition)
17         with self.assertRaises(NotFoundException):
18             self.assertEqual(Condition.by_id(self.db_conn, 2), condition)
19
20     def test_Condition_all(self) -> None:
21         """Test .all()."""
22         self.assertEqual(Condition.all(self.db_conn), [])
23         condition_1 = Condition(None, False)
24         condition_1.save(self.db_conn)
25         self.assertEqual(Condition.all(self.db_conn), [condition_1])
26         condition_2 = Condition(None, False)
27         condition_2.save(self.db_conn)
28         self.assertEqual(Condition.all(self.db_conn), [condition_1,
29                                                        condition_2])
30
31     def test_Condition_singularity(self) -> None:
32         """Test pointers made for single object keep pointing to it."""
33         condition_1 = Condition(None, False)
34         condition_1.save(self.db_conn)
35         condition_1.is_active = True
36         condition_retrieved = Condition.by_id(self.db_conn, 1)
37         self.assertEqual(True, condition_retrieved.is_active)
38
39
40 class TestsWithServer(TestCaseWithServer):
41     """Module tests against our HTTP server/handler (and database)."""
42
43     def test_do_POST_condition(self) -> None:
44         """Test POST /condition and its effect on the database."""
45         form_data = {'title': 'foo', 'description': 'foo'}
46         self.check_post(form_data, '/condition', 302, '/')
47         self.assertEqual(1, len(Condition.all(self.db_conn)))
48
49     def test_do_GET(self) -> None:
50         """Test /condition and /conditions response codes."""
51         self.check_get('/condition', 200)
52         self.check_get('/condition?id=', 200)
53         self.check_get('/condition?id=0', 400)
54         self.check_get('/condition?id=FOO', 400)
55         self.check_get('/conditions', 200)