1 """Test Conditions module."""
3 from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer,
5 from plomtask.conditions import Condition
6 from plomtask.processes import Process
7 from plomtask.todos import Todo
8 from plomtask.exceptions import HandledException
11 class TestsSansDB(TestCaseSansDB):
12 """Tests requiring no DB setup."""
13 checked_class = Condition
16 class TestsWithDB(TestCaseWithDB):
17 """Tests requiring DB, but not server setup."""
18 checked_class = Condition
19 default_init_kwargs = {'is_active': 0}
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 todo.save(self.db_conn)
28 # check condition can only be deleted if not depended upon
29 for depender in (proc, todo):
32 assert isinstance(c.id_, int)
33 depender.set_condition_relations(self.db_conn, [c.id_], [], [], [])
34 depender.save(self.db_conn)
35 with self.assertRaises(HandledException):
36 c.remove(self.db_conn)
37 depender.set_condition_relations(self.db_conn, [], [], [], [])
38 depender.save(self.db_conn)
39 c.remove(self.db_conn)
42 class ExpectedGetConditions(Expected):
43 """Builder of expectations for GET /conditions."""
44 _default_dict = {'sort_by': 'title', 'pattern': ''}
46 def recalc(self) -> None:
47 """Update internal dictionary by subclass-specific rules."""
49 self._fields['conditions'] = self.as_ids(self.lib_all('Condition'))
52 class ExpectedGetCondition(Expected):
53 """Builder of expectations for GET /condition."""
54 _default_dict = {'is_new': False}
55 _on_empty_make_temp = ('Condition', 'cond_as_dict')
57 def __init__(self, id_: int | None, *args: Any, **kwargs: Any) -> None:
58 self._fields = {'condition': id_}
59 super().__init__(*args, **kwargs)
61 def recalc(self) -> None:
62 """Update internal dictionary by subclass-specific rules."""
64 for p_field, c_field in [('conditions', 'enabled_processes'),
65 ('disables', 'disabling_processes'),
66 ('blockers', 'disabled_processes'),
67 ('enables', 'enabling_processes')]:
68 self._fields[c_field] = self.as_ids([
69 p for p in self.lib_all('Process')
70 if self._fields['condition'] in p[p_field]])
73 class TestsWithServer(TestCaseWithServer):
74 """Module tests against our HTTP server/handler (and database)."""
75 checked_class = Condition
77 def test_fail_POST_condition(self) -> None:
78 """Test malformed/illegal POST /condition requests."""
79 # check incomplete POST payloads
81 self.check_post({}, url, 400)
82 self.check_post({'title': ''}, url, 400)
83 self.check_post({'description': ''}, url, 400)
84 # check valid POST payload on bad paths
85 valid_payload = {'title': '', 'description': '', 'is_active': 0}
86 self.check_post(valid_payload, f'{url}?id=foo', 400)
88 def test_POST_condition(self) -> None:
89 """Test (valid) POST /condition and its effect on GET /condition[s]."""
90 url_single, url_all = '/condition?id=1', '/conditions'
91 exp_single, exp_all = ExpectedGetCondition(1), ExpectedGetConditions()
92 all_exps = [exp_single, exp_all]
93 # test valid POST's effect on single /condition and full /conditions
94 self.post_exp_cond(all_exps, {'title': 'foo', 'description': 'oof'},
96 self.check_json_get(url_single, exp_single)
97 self.check_json_get(url_all, exp_all)
98 # test (no) effect of invalid POST to existing Condition on /condition
99 self.check_post({}, url_single, 400)
100 self.check_json_get(url_single, exp_single)
101 # test effect of POST changing title and activeness
102 self.post_exp_cond(all_exps, {'title': 'bar', 'description': 'oof',
104 self.check_json_get(url_single, exp_single)
105 self.check_json_get(url_all, exp_all)
106 # test deletion POST's effect, both to return id=1 into empty single,
107 # full /conditions into empty list
108 self.post_exp_cond(all_exps, {'delete': ''}, redir_to_id=False)
109 exp_single.set('is_new', True)
110 self.check_json_get(url_single, exp_single)
111 self.check_json_get(url_all, exp_all)
113 def test_GET_condition(self) -> None:
114 """More GET /condition testing, especially for Process relations."""
115 # check expected default status codes
116 self.check_get_defaults('/condition')
117 # check 'is_new' set if id= absent or pointing to not-yet-existing ID
118 exp = ExpectedGetCondition(None)
119 exp.set('is_new', True)
120 self.check_json_get('/condition', exp)
121 exp = ExpectedGetCondition(1)
122 exp.set('is_new', True)
123 self.check_json_get('/condition?id=1', exp)
124 # make Condition and two Processes that among them establish all
125 # possible ConditionsRelations to it, check /condition displays all
126 exp = ExpectedGetCondition(1)
127 self.post_exp_cond([exp], {'title': 'foo', 'description': 'oof'},
129 for i, p in enumerate([('conditions', 'disables'),
130 ('enables', 'blockers')]):
131 self.post_exp_process([exp], {k: [1] for k in p}, i+1)
132 self.check_json_get('/condition?id=1', exp)
134 def test_GET_conditions(self) -> None:
135 """Test GET /conditions."""
136 # test empty result on empty DB, default-settings on empty params
137 exp = ExpectedGetConditions()
138 self.check_json_get('/conditions', exp)
139 # test 'sort_by' default to 'title' (even if set to something else, as
140 # long as without handler) and 'pattern' get preserved
141 exp.set('pattern', 'bar')
142 self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x', exp)
143 exp.set('pattern', '')
144 # test non-empty result, automatic (positive) sorting by title
145 post_cond1 = {'is_active': 0, 'title': 'foo', 'description': 'oof'}
146 post_cond2 = {'is_active': 0, 'title': 'bar', 'description': 'rab'}
147 post_cond3 = {'is_active': 1, 'title': 'baz', 'description': 'zab'}
148 for i, post in enumerate([post_cond1, post_cond2, post_cond3]):
149 self.post_exp_cond([exp], post, i+1, post_to_id=False)
150 self.check_filter(exp, 'conditions', 'sort_by', 'title', [2, 3, 1])
151 # test other sortings
152 self.check_filter(exp, 'conditions', 'sort_by', '-title', [1, 3, 2])
153 self.check_filter(exp, 'conditions', 'sort_by', 'is_active', [1, 2, 3])
154 self.check_filter(exp, 'conditions', 'sort_by', '-is_active',
156 exp.set('sort_by', 'title')
157 # test pattern matching on title
158 exp.lib_del('Condition', 1)
159 self.check_filter(exp, 'conditions', 'pattern', 'ba', [2, 3])
160 # test pattern matching on description
161 exp.lib_wipe('Condition')
162 exp.set_cond_from_post(1, post_cond1)
163 self.check_filter(exp, 'conditions', 'pattern', 'of', [1])