home · contact · privacy
Overhaul caching.
[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     do_id_test = True
13     versioned_defaults_to_test = {'title': 'UNNAMED', 'description': ''}
14
15
16 class TestsWithDB(TestCaseWithDB):
17     """Tests requiring DB, but not server setup."""
18     checked_class = Condition
19     default_init_kwargs = {'is_active': False}
20     test_versioneds = {'title': str, 'description': str}
21
22     def test_Condition_from_table_row(self) -> None:
23         """Test .from_table_row() properly reads in class from DB"""
24         self.check_from_table_row()
25         self.check_versioned_from_table_row('title', str)
26         self.check_versioned_from_table_row('description', str)
27
28     def test_Condition_by_id(self) -> None:
29         """Test .by_id(), including creation."""
30         self.check_by_id()
31
32     def test_Condition_all(self) -> None:
33         """Test .all()."""
34         self.check_all()
35
36     def test_Condition_singularity(self) -> None:
37         """Test pointers made for single object keep pointing to it."""
38         self.check_singularity('is_active', True)
39
40     def test_Condition_versioned_attributes_singularity(self) -> None:
41         """Test behavior of VersionedAttributes on saving (with .title)."""
42         self.check_versioned_singularity()
43
44     def test_Condition_remove(self) -> None:
45         """Test .remove() effects on DB and cache."""
46         self.check_remove()
47         proc = Process(None)
48         proc.save(self.db_conn)
49         todo = Todo(None, proc, False, '2024-01-01')
50         for depender in (proc, todo):
51             assert hasattr(depender, 'save')
52             assert hasattr(depender, 'set_conditions')
53             c = Condition(None)
54             c.save(self.db_conn)
55             depender.save(self.db_conn)
56             depender.set_conditions(self.db_conn, [c.id_], 'conditions')
57             depender.save(self.db_conn)
58             with self.assertRaises(HandledException):
59                 c.remove(self.db_conn)
60             depender.set_conditions(self.db_conn, [], 'conditions')
61             depender.save(self.db_conn)
62             c.remove(self.db_conn)
63
64
65 class TestsWithServer(TestCaseWithServer):
66     """Module tests against our HTTP server/handler (and database)."""
67
68     def test_do_POST_condition(self) -> None:
69         """Test POST /condition and its effect on the database."""
70         form_data = {'title': 'foo', 'description': 'foo'}
71         self.check_post(form_data, '/condition', 302, '/condition?id=1')
72         self.assertEqual(1, len(Condition.all(self.db_conn)))
73         form_data['delete'] = ''
74         self.check_post(form_data, '/condition?id=', 404)
75         self.check_post(form_data, '/condition?id=2', 404)
76         self.check_post(form_data, '/condition?id=1', 302, '/conditions')
77         self.assertEqual(0, len(Condition.all(self.db_conn)))
78
79     def test_do_GET(self) -> None:
80         """Test /condition and /conditions response codes."""
81         form_data = {'title': 'foo', 'description': 'foo'}
82         self.check_post(form_data, '/condition', 302, '/condition?id=1')
83         self.check_get_defaults('/condition')
84         self.check_get('/conditions', 200)