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
9 class TestsSansDB(TestCaseSansDB):
10 """Tests requiring no DB setup."""
11 checked_class = Condition
13 versioned_defaults_to_test = {'title': 'UNNAMED', 'description': ''}
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}
22 def test_from_table_row(self) -> None:
23 """Test .from_table_row() properly reads in class from DB"""
24 super().test_from_table_row()
25 self.check_versioned_from_table_row('title', str)
26 self.check_versioned_from_table_row('description', str)
28 def test_Condition_by_id(self) -> None:
29 """Test .by_id(), including creation."""
32 def test_Condition_all(self) -> None:
36 def test_Condition_versioned_attributes_singularity(self) -> None:
37 """Test behavior of VersionedAttributes on saving (with .title)."""
38 self.check_versioned_singularity()
40 def test_Condition_remove(self) -> None:
41 """Test .remove() effects on DB and cache."""
44 proc.save(self.db_conn)
45 todo = Todo(None, proc, False, '2024-01-01')
46 for depender in (proc, todo):
47 assert hasattr(depender, 'save')
48 assert hasattr(depender, 'set_conditions')
51 depender.save(self.db_conn)
52 depender.set_conditions(self.db_conn, [c.id_], 'conditions')
53 depender.save(self.db_conn)
54 with self.assertRaises(HandledException):
55 c.remove(self.db_conn)
56 depender.set_conditions(self.db_conn, [], 'conditions')
57 depender.save(self.db_conn)
58 c.remove(self.db_conn)
61 class TestsWithServer(TestCaseWithServer):
62 """Module tests against our HTTP server/handler (and database)."""
65 def cond_as_dict(id_: int = 1,
66 is_active: bool = False,
67 titles: None | list[str] = None,
68 descriptions: None | list[str] = None
69 ) -> dict[str, object]:
70 """Return JSON of Condition to expect."""
72 'is_active': is_active,
78 titles = titles if titles else []
79 descriptions = descriptions if descriptions else []
80 assert isinstance(d['_versioned'], dict)
81 for i, title in enumerate(titles):
82 d['_versioned']['title'][i] = title
83 for i, description in enumerate(descriptions):
84 d['_versioned']['description'][i] = description
88 def proc_as_dict(id_: int = 1,
90 enables: None | list[dict[str, object]] = None,
91 disables: None | list[dict[str, object]] = None,
92 conditions: None | list[dict[str, object]] = None,
93 blockers: None | list[dict[str, object]] = None
94 ) -> dict[str, object]:
95 """Return JSON of Process to expect."""
96 # pylint: disable=too-many-arguments
99 'suppressed_steps': [],
100 'explicit_steps': [],
103 'description': {0: ''},
106 'conditions': conditions if conditions else [],
107 'disables': disables if disables else [],
108 'enables': enables if enables else [],
109 'blockers': blockers if blockers else []}
112 def test_do_POST_condition(self) -> None:
113 """Test POST /condition and its effect on GET /condition[s]."""
114 # check empty POST fails
115 self.check_post({}, '/condition', 400)
116 # test valid POST's effect on …
117 post = {'title': 'foo', 'description': 'oof', 'is_active': False}
118 self.check_post(post, '/condition', 302, '/condition?id=1')
119 # … single /condition
120 cond = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
121 expected_single: dict[str, object]
122 expected_single = {'is_new': False,
123 'enabled_processes': [],
124 'disabled_processes': [],
125 'enabling_processes': [],
126 'disabling_processes': [],
128 self.check_json_get('/condition?id=1', expected_single)
130 expected_all: dict[str, object]
131 expected_all = {'conditions': [cond],
132 'sort_by': 'title', 'pattern': ''}
133 self.check_json_get('/conditions', expected_all)
134 # test effect of invalid POST to existing Condition on /condition
135 self.check_post({}, '/condition?id=1', 400)
136 self.check_json_get('/condition?id=1', expected_single)
137 # test effect of POST changing title and activeness
138 post = {'title': 'bar', 'description': 'oof', 'is_active': True}
139 self.check_post(post, '/condition?id=1', 302)
140 assert isinstance(expected_single['condition'], dict)
141 expected_single['condition']['_versioned']['title'][1] = 'bar'
142 expected_single['condition']['is_active'] = True
143 self.check_json_get('/condition?id=1', expected_single)
144 # test deletion POST's effect on …
145 self.check_post({'delete': ''}, '/condition?id=1', 302, '/conditions')
146 cond = self.cond_as_dict()
147 expected_single['condition'] = cond
148 self.check_json_get('/condition?id=1', expected_single)
150 expected_all['conditions'] = []
151 self.check_json_get('/conditions', expected_all)
153 def test_do_GET_condition(self) -> None:
154 """More GET /condition testing, especially for Process relations."""
155 # check expected default status codes
156 self.check_get_defaults('/condition')
157 # check display of process relations
158 form_data = {'title': 'foo', 'description': 'oof', 'is_active': False}
159 self.check_post(form_data, '/condition', 302, '/condition?id=1')
160 proc_1_post = {'title': 'A', 'description': '', 'effort': 1.0,
161 'condition': [1], 'disables': [1]}
162 self.post_process(1, proc_1_post)
163 proc_2_post = {'title': 'B', 'description': '', 'effort': 1.0,
164 'enables': [1], 'blocker': [1]}
165 self.post_process(2, proc_2_post)
166 cond = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
167 proc_1 = self.proc_as_dict(conditions=[cond], disables=[cond])
168 proc_2 = self.proc_as_dict(2, 'B', blockers=[cond], enables=[cond])
169 expected_single = {'is_new': False,
170 'enabled_processes': [proc_1],
171 'disabled_processes': [proc_2],
172 'enabling_processes': [proc_2],
173 'disabling_processes': [proc_1],
175 self.check_json_get('/condition?id=1', expected_single)
177 def test_do_GET_conditions(self) -> None:
178 """Test GET /conditions."""
179 # test empty result on empty DB, default-settings on empty params
180 expected_json: dict[str, object] = {'conditions': [],
183 self.check_json_get('/conditions', expected_json)
184 # test on meaningless non-empty params (incl. entirely un-used key)
185 expected_json = {'conditions': [],
186 'sort_by': 'title', # nonsense "foo" defaulting
187 'pattern': 'bar'} # preserved despite zero effect
188 self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x',
190 # test non-empty result, automatic (positive) sorting by title
191 post_1 = {'title': 'foo', 'description': 'oof', 'is_active': False}
192 self.check_post(post_1, '/condition', 302, '/condition?id=1')
193 post_2 = {'title': 'bar', 'description': 'rab', 'is_active': False}
194 self.check_post(post_2, '/condition', 302, '/condition?id=2')
195 post_3 = {'title': 'baz', 'description': 'zab', 'is_active': True}
196 self.check_post(post_3, '/condition', 302, '/condition?id=3')
197 cond_1 = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
198 cond_2 = self.cond_as_dict(2, titles=['bar'], descriptions=['rab'])
199 cond_3 = self.cond_as_dict(3, True, ['baz'], ['zab'])
200 cons = [cond_2, cond_3, cond_1]
201 expected_json = {'conditions': cons, 'sort_by': 'title', 'pattern': ''}
202 self.check_json_get('/conditions', expected_json)
203 # test other sortings
204 # (NB: by .is_active has two items of =False, their order currently
205 # is not explicitly made predictable, so mail fail until we do)
206 expected_json['conditions'] = [cond_1, cond_3, cond_2]
207 expected_json['sort_by'] = '-title'
208 self.check_json_get('/conditions?sort_by=-title', expected_json)
209 expected_json['conditions'] = [cond_1, cond_2, cond_3]
210 expected_json['sort_by'] = 'is_active'
211 self.check_json_get('/conditions?sort_by=is_active', expected_json)
212 expected_json['conditions'] = [cond_3, cond_1, cond_2]
213 expected_json['sort_by'] = '-is_active'
214 self.check_json_get('/conditions?sort_by=-is_active', expected_json)
215 # test pattern matching on title
216 expected_json = {'conditions': [cond_2, cond_3],
217 'sort_by': 'title', 'pattern': 'ba'}
218 self.check_json_get('/conditions?pattern=ba', expected_json)
219 # test pattern matching on description
220 expected_json['conditions'] = [cond_1]
221 expected_json['pattern'] = 'oo'
222 self.check_json_get('/conditions?pattern=oo', expected_json)