home · contact · privacy
More refactoring.
[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 versioned_condition(self) -> Condition:
29         """Create Condition with some VersionedAttribute values."""
30         c = Condition(None)
31         c.title.set('title1')
32         c.title.set('title2')
33         c.description.set('desc1')
34         c.description.set('desc2')
35         return c
36
37     def test_Condition_saving_and_caching(self) -> None:
38         """Test .save/.save_core."""
39         kwargs = {'id_': 1, 'is_active': False}
40         self.check_saving_and_caching(**kwargs)
41         # check .id_ set if None, and versioned attributes too
42         c = self.versioned_condition()
43         c.save(self.db_conn)
44         self.assertEqual(c.id_, 2)
45         self.assertEqual(sorted(c.title.history.values()),
46                          ['title1', 'title2'])
47         self.assertEqual(sorted(c.description.history.values()),
48                          ['desc1', 'desc2'])
49
50     def test_Condition_from_table_row(self) -> None:
51         """Test .from_table_row() properly reads in class from DB"""
52         self.check_from_table_row()
53         c = self.versioned_condition()
54         c.save(self.db_conn)
55         assert isinstance(c.id_, int)
56         for row in self.db_conn.row_where(Condition.table_name, 'id', c.id_):
57             retrieved = Condition.from_table_row(self.db_conn, row)
58             # pylint: disable=no-member
59             self.assertEqual(sorted(retrieved.title.history.values()),
60                              ['title1', 'title2'])
61             # pylint: disable=no-member
62             self.assertEqual(sorted(retrieved.description.history.values()),
63                              ['desc1', 'desc2'])
64
65     def test_Condition_by_id(self) -> None:
66         """Test .by_id(), including creation."""
67         self.check_by_id()
68
69     def test_Condition_all(self) -> None:
70         """Test .all()."""
71         self.check_all()
72
73     def test_Condition_singularity(self) -> None:
74         """Test pointers made for single object keep pointing to it."""
75         self.check_singularity('is_active', True)
76
77     def test_Condition_versioned_attributes_singularity(self) -> None:
78         """Test behavior of VersionedAttributes on saving (with .title)."""
79         self.check_versioned_singularity()
80
81     def test_Condition_remove(self) -> None:
82         """Test .remove() effects on DB and cache."""
83         self.check_remove()
84         c = Condition(None)
85         proc = Process(None)
86         todo = Todo(None, proc, False, '2024-01-01')
87         for depender in (proc, todo):
88             assert hasattr(depender, 'save')
89             assert hasattr(depender, 'set_conditions')
90             c.save(self.db_conn)
91             depender.save(self.db_conn)
92             depender.set_conditions(self.db_conn, [c.id_], 'conditions')
93             depender.save(self.db_conn)
94             with self.assertRaises(HandledException):
95                 c.remove(self.db_conn)
96             depender.set_conditions(self.db_conn, [], 'conditions')
97             depender.save(self.db_conn)
98             c.remove(self.db_conn)
99
100
101 class TestsWithServer(TestCaseWithServer):
102     """Module tests against our HTTP server/handler (and database)."""
103
104     def test_do_POST_condition(self) -> None:
105         """Test POST /condition and its effect on the database."""
106         form_data = {'title': 'foo', 'description': 'foo'}
107         self.check_post(form_data, '/condition', 302, '/condition?id=1')
108         self.assertEqual(1, len(Condition.all(self.db_conn)))
109         form_data['delete'] = ''
110         self.check_post(form_data, '/condition?id=', 404)
111         self.check_post(form_data, '/condition?id=2', 404)
112         self.check_post(form_data, '/condition?id=1', 302, '/conditions')
113         self.assertEqual(0, len(Condition.all(self.db_conn)))
114
115     def test_do_GET(self) -> None:
116         """Test /condition and /conditions response codes."""
117         form_data = {'title': 'foo', 'description': 'foo'}
118         self.check_post(form_data, '/condition', 302, '/condition?id=1')
119         self.check_get_defaults('/condition')
120         self.check_get('/conditions', 200)