home · contact · privacy
Refactor and extend tests.
[plomtask] / tests / conditions.py
1 """Test Conditions module."""
2 from tests.utils import TestCaseWithDB, TestCaseWithServer, TestCaseSansDB
3 from plomtask.conditions import Condition
4 from plomtask.processes import Process
5 from plomtask.todos import Todo
6 from plomtask.exceptions import HandledException
7
8
9 class TestsSansDB(TestCaseSansDB):
10     """Tests requiring no DB setup."""
11     checked_class = Condition
12
13     def test_Condition_id_setting(self) -> None:
14         """Test .id_ being set and its legal range being enforced."""
15         self.check_id_setting()
16
17     def test_Condition_versioned_defaults(self) -> None:
18         """Test defaults of VersionedAttributes."""
19         self.check_versioned_defaults({
20             'title': 'UNNAMED',
21             'description': ''})
22
23
24 class TestsWithDB(TestCaseWithDB):
25     """Tests requiring DB, but not server setup."""
26     checked_class = Condition
27
28     def test_Condition_saving_and_caching(self) -> None:
29         """Test .save/.save_core."""
30         kwargs = {'id_': 1, 'is_active': False}
31         self.check_saving_and_caching(**kwargs)
32         # check .id_ set if None, and versioned attributes too
33         c = Condition(None)
34         c.save(self.db_conn)
35         self.assertEqual(c.id_, 2)
36         self.check_saving_of_versioned('title', str)
37         self.check_saving_of_versioned('description', str)
38
39     def test_Condition_from_table_row(self) -> None:
40         """Test .from_table_row() properly reads in class from DB"""
41         self.check_from_table_row()
42         self.check_versioned_from_table_row('title', str)
43         self.check_versioned_from_table_row('description', str)
44
45     def test_Condition_by_id(self) -> None:
46         """Test .by_id(), including creation."""
47         self.check_by_id()
48
49     def test_Condition_all(self) -> None:
50         """Test .all()."""
51         self.check_all()
52
53     def test_Condition_singularity(self) -> None:
54         """Test pointers made for single object keep pointing to it."""
55         self.check_singularity('is_active', True)
56
57     def test_Condition_versioned_attributes_singularity(self) -> None:
58         """Test behavior of VersionedAttributes on saving (with .title)."""
59         self.check_versioned_singularity()
60
61     def test_Condition_remove(self) -> None:
62         """Test .remove() effects on DB and cache."""
63         self.check_remove()
64         c = Condition(None)
65         proc = Process(None)
66         proc.save(self.db_conn)
67         todo = Todo(None, proc, False, '2024-01-01')
68         for depender in (proc, todo):
69             assert hasattr(depender, 'save')
70             assert hasattr(depender, 'set_conditions')
71             c.save(self.db_conn)
72             depender.save(self.db_conn)
73             depender.set_conditions(self.db_conn, [c.id_], 'conditions')
74             depender.save(self.db_conn)
75             with self.assertRaises(HandledException):
76                 c.remove(self.db_conn)
77             depender.set_conditions(self.db_conn, [], 'conditions')
78             depender.save(self.db_conn)
79             c.remove(self.db_conn)
80
81
82 class TestsWithServer(TestCaseWithServer):
83     """Module tests against our HTTP server/handler (and database)."""
84
85     def test_do_POST_condition(self) -> None:
86         """Test POST /condition and its effect on the database."""
87         form_data = {'title': 'foo', 'description': 'foo'}
88         self.check_post(form_data, '/condition', 302, '/condition?id=1')
89         self.assertEqual(1, len(Condition.all(self.db_conn)))
90         form_data['delete'] = ''
91         self.check_post(form_data, '/condition?id=', 404)
92         self.check_post(form_data, '/condition?id=2', 404)
93         self.check_post(form_data, '/condition?id=1', 302, '/conditions')
94         self.assertEqual(0, len(Condition.all(self.db_conn)))
95
96     def test_do_GET(self) -> None:
97         """Test /condition and /conditions response codes."""
98         form_data = {'title': 'foo', 'description': 'foo'}
99         self.check_post(form_data, '/condition', 302, '/condition?id=1')
100         self.check_get_defaults('/condition')
101         self.check_get('/conditions', 200)