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 'condition': [1], 'disables': [1]}
100 self.post_process(1, proc_1_post)
101 proc_2_post = {'title': 'B', 'description': '', 'effort': 1.0,
102 'enables': [1], 'blocker': [1]}
103 self.post_process(2, proc_2_post)
104 cond = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
105 proc_1 = self.proc_as_dict(conditions=[cond], disables=[cond])
106 proc_2 = self.proc_as_dict(2, 'B', blockers=[cond], enables=[cond])
107 expected = {'is_new': False,
108 'enabled_processes': self.as_id_list([proc_1]),
109 'disabled_processes': self.as_id_list([proc_2]),
110 'enabling_processes': self.as_id_list([proc_2]),
111 'disabling_processes': self.as_id_list([proc_1]),
112 'condition': cond['id'],
113 '_library': {'Condition': self.as_refs([cond]),
114 'Process': self.as_refs([proc_1, proc_2])}}
115 self.check_json_get('/condition?id=1', expected)
117 def test_do_GET_conditions(self) -> None:
118 """Test GET /conditions."""
119 # test empty result on empty DB, default-settings on empty params
120 expected_json: dict[str, object] = {'conditions': [],
124 self.check_json_get('/conditions', expected_json)
125 # test on meaningless non-empty params (incl. entirely un-used key)
126 expected_json = {'conditions': [],
127 'sort_by': 'title', # nonsense "foo" defaulting
128 'pattern': 'bar', # preserved despite zero effect
130 self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x',
132 # test non-empty result, automatic (positive) sorting by title
133 post_1 = {'title': 'foo', 'description': 'oof', 'is_active': False}
134 self.check_post(post_1, '/condition', 302, '/condition?id=1')
135 post_2 = {'title': 'bar', 'description': 'rab', 'is_active': False}
136 self.check_post(post_2, '/condition', 302, '/condition?id=2')
137 post_3 = {'title': 'baz', 'description': 'zab', 'is_active': True}
138 self.check_post(post_3, '/condition', 302, '/condition?id=3')
139 cond_1 = self.cond_as_dict(titles=['foo'], descriptions=['oof'])
140 cond_2 = self.cond_as_dict(2, titles=['bar'], descriptions=['rab'])
141 cond_3 = self.cond_as_dict(3, True, ['baz'], ['zab'])
142 cons = [cond_2, cond_3, cond_1]
143 expected_json = {'conditions': self.as_id_list(cons),
146 '_library': {'Condition': self.as_refs(cons)}}
147 self.check_json_get('/conditions', expected_json)
148 # test other sortings
149 # (NB: by .is_active has two items of =False, their order currently
150 # is not explicitly made predictable, so mail fail until we do)
151 expected_json['conditions'] = self.as_id_list([cond_1, cond_3, cond_2])
152 expected_json['sort_by'] = '-title'
153 self.check_json_get('/conditions?sort_by=-title', expected_json)
154 expected_json['conditions'] = self.as_id_list([cond_1, cond_2, cond_3])
155 expected_json['sort_by'] = 'is_active'
156 self.check_json_get('/conditions?sort_by=is_active', expected_json)
157 expected_json['conditions'] = self.as_id_list([cond_3, cond_1, cond_2])
158 expected_json['sort_by'] = '-is_active'
159 self.check_json_get('/conditions?sort_by=-is_active', expected_json)
160 # test pattern matching on title
161 cons = [cond_2, cond_3]
162 expected_json = {'conditions': self.as_id_list(cons),
163 'sort_by': 'title', 'pattern': 'ba',
164 '_library': {'Condition': self.as_refs(cons)}}
165 self.check_json_get('/conditions?pattern=ba', expected_json)
166 # test pattern matching on description
167 expected_json['conditions'] = self.as_id_list([cond_1])
168 assert isinstance(expected_json['_library'], dict)
169 expected_json['_library']['Condition'] = self.as_refs([cond_1])
170 expected_json['pattern'] = 'oo'
171 self.check_json_get('/conditions?pattern=oo', expected_json)