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