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
12 versioned_defaults_to_test = {'title': 'UNNAMED', 'description': ''}
15 class TestsWithDB(TestCaseWithDB):
16 """Tests requiring DB, but not server setup."""
17 checked_class = Condition
18 default_init_kwargs = {'is_active': False}
19 test_versioneds = {'title': str, 'description': str}
21 def test_remove(self) -> None:
22 """Test .remove() effects on DB and cache."""
25 proc.save(self.db_conn)
26 todo = Todo(None, proc, False, '2024-01-01')
27 for depender in (proc, todo):
28 assert hasattr(depender, 'save')
29 assert hasattr(depender, 'set_conditions')
32 depender.save(self.db_conn)
33 depender.set_conditions(self.db_conn, [c.id_], 'conditions')
34 depender.save(self.db_conn)
35 with self.assertRaises(HandledException):
36 c.remove(self.db_conn)
37 depender.set_conditions(self.db_conn, [], 'conditions')
38 depender.save(self.db_conn)
39 c.remove(self.db_conn)
42 class TestsWithServer(TestCaseWithServer):
43 """Module tests against our HTTP server/handler (and database)."""
45 def test_do_POST_condition(self) -> None:
46 """Test POST /condition and its effect on GET /condition[s]."""
47 # check empty POST fails
48 self.check_post({}, '/condition', 400)
49 # test valid POST's effect on …
50 post = {'title': 'foo', 'description': 'oof', 'is_active': False}
51 self.check_post(post, '/condition', 302, '/condition?id=1')
53 cond = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
54 expected_single: dict[str, object]
55 expected_single = {'is_new': False,
56 'enabled_processes': [],
57 'disabled_processes': [],
58 'enabling_processes': [],
59 'disabling_processes': [],
60 'condition': cond['id'],
61 '_library': {'Condition': self.as_refs([cond])}}
62 self.check_json_get('/condition?id=1', expected_single)
64 expected_all: dict[str, object]
65 expected_all = {'conditions': self.as_id_list([cond]),
66 'sort_by': 'title', 'pattern': '',
67 '_library': {'Condition': self.as_refs([cond])}}
68 self.check_json_get('/conditions', expected_all)
69 # test effect of invalid POST to existing Condition on /condition
70 self.check_post({}, '/condition?id=1', 400)
71 self.check_json_get('/condition?id=1', expected_single)
72 # test effect of POST changing title and activeness
73 post = {'title': 'bar', 'description': 'oof', 'is_active': True}
74 self.check_post(post, '/condition?id=1', 302)
75 assert isinstance(cond['_versioned'], dict)
76 cond['_versioned']['title'][1] = 'bar'
77 cond['is_active'] = True
78 self.check_json_get('/condition?id=1', expected_single)
79 # test deletion POST's effect on …
80 self.check_post({'delete': ''}, '/condition?id=1', 302, '/conditions')
81 cond = self.cond_as_dict()
82 assert isinstance(expected_single['_library'], dict)
83 assert isinstance(expected_single['_library']['Condition'], dict)
84 expected_single['_library']['Condition'] = self.as_refs([cond])
85 self.check_json_get('/condition?id=1', expected_single)
87 expected_all['conditions'] = []
88 expected_all['_library'] = {}
89 self.check_json_get('/conditions', expected_all)
91 def test_do_GET_condition(self) -> None:
92 """More GET /condition testing, especially for Process relations."""
93 # check expected default status codes
94 self.check_get_defaults('/condition')
95 # check display of process relations
96 form_data = {'title': 'foo', 'description': 'oof', 'is_active': False}
97 self.check_post(form_data, '/condition', 302, '/condition?id=1')
98 proc_1_post = {'title': 'A', 'description': '', 'effort': 1.0,
99 'conditions': [1], 'disables': [1]}
100 self.post_process(1, proc_1_post)
101 proc_2_post = {'title': 'B', 'description': '', 'effort': 1.0,
102 'enables': [1], 'blockers': [1]}
103 self.post_process(2, proc_2_post)
104 cond = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
105 assert isinstance(cond['id'], int)
106 proc_1 = self.proc_as_dict(conditions=[cond['id']],
107 disables=[cond['id']])
108 proc_2 = self.proc_as_dict(2, 'B',
109 blockers=[cond['id']],
110 enables=[cond['id']])
111 expected = {'is_new': False,
112 'enabled_processes': self.as_id_list([proc_1]),
113 'disabled_processes': self.as_id_list([proc_2]),
114 'enabling_processes': self.as_id_list([proc_2]),
115 'disabling_processes': self.as_id_list([proc_1]),
116 'condition': cond['id'],
117 '_library': {'Condition': self.as_refs([cond]),
118 'Process': self.as_refs([proc_1, proc_2])}}
119 self.check_json_get('/condition?id=1', expected)
121 def test_do_GET_conditions(self) -> None:
122 """Test GET /conditions."""
123 # test empty result on empty DB, default-settings on empty params
124 expected_json: dict[str, object] = {'conditions': [],
128 self.check_json_get('/conditions', expected_json)
129 # test on meaningless non-empty params (incl. entirely un-used key)
130 expected_json = {'conditions': [],
131 'sort_by': 'title', # nonsense "foo" defaulting
132 'pattern': 'bar', # preserved despite zero effect
134 self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x',
136 # test non-empty result, automatic (positive) sorting by title
137 post_1 = {'title': 'foo', 'description': 'oof', 'is_active': False}
138 self.check_post(post_1, '/condition', 302, '/condition?id=1')
139 post_2 = {'title': 'bar', 'description': 'rab', 'is_active': False}
140 self.check_post(post_2, '/condition', 302, '/condition?id=2')
141 post_3 = {'title': 'baz', 'description': 'zab', 'is_active': True}
142 self.check_post(post_3, '/condition', 302, '/condition?id=3')
143 cond_1 = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
144 cond_2 = self.cond_as_dict(2, titles=['bar'], descriptions=['rab'])
145 cond_3 = self.cond_as_dict(3, True, ['baz'], ['zab'])
146 cons = [cond_2, cond_3, cond_1]
147 expected_json = {'conditions': self.as_id_list(cons),
150 '_library': {'Condition': self.as_refs(cons)}}
151 self.check_json_get('/conditions', expected_json)
152 # test other sortings
153 # (NB: by .is_active has two items of =False, their order currently
154 # is not explicitly made predictable, so mail fail until we do)
155 expected_json['conditions'] = self.as_id_list([cond_1, cond_3, cond_2])
156 expected_json['sort_by'] = '-title'
157 self.check_json_get('/conditions?sort_by=-title', expected_json)
158 expected_json['conditions'] = self.as_id_list([cond_1, cond_2, cond_3])
159 expected_json['sort_by'] = 'is_active'
160 self.check_json_get('/conditions?sort_by=is_active', expected_json)
161 expected_json['conditions'] = self.as_id_list([cond_3, cond_1, cond_2])
162 expected_json['sort_by'] = '-is_active'
163 self.check_json_get('/conditions?sort_by=-is_active', expected_json)
164 # test pattern matching on title
165 cons = [cond_2, cond_3]
166 expected_json = {'conditions': self.as_id_list(cons),
167 'sort_by': 'title', 'pattern': 'ba',
168 '_library': {'Condition': self.as_refs(cons)}}
169 self.check_json_get('/conditions?pattern=ba', expected_json)
170 # test pattern matching on description
171 expected_json['conditions'] = self.as_id_list([cond_1])
172 assert isinstance(expected_json['_library'], dict)
173 expected_json['_library']['Condition'] = self.as_refs([cond_1])
174 expected_json['pattern'] = 'oo'
175 self.check_json_get('/conditions?pattern=oo', expected_json)