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
80 valid_payload = {'title': '', 'description': ''}
81 self.check_minimal_inputs('/condition', valid_payload)
82 # check valid POST payload on bad paths
83 self.check_post(valid_payload, '/condition?id=foo', 400)
85 def test_POST_condition(self) -> None:
86 """Test (valid) POST /condition and its effect on GET /condition[s]."""
87 url_single, url_all = '/condition?id=1', '/conditions'
88 exp_single, exp_all = ExpectedGetCondition(1), ExpectedGetConditions()
89 all_exps = [exp_single, exp_all]
90 # test valid POST's effect on single /condition and full /conditions
91 self.post_exp_cond(all_exps, {'title': 'foo', 'description': 'oof'},
93 self.check_json_get(url_single, exp_single)
94 self.check_json_get(url_all, exp_all)
95 # test (no) effect of invalid POST to existing Condition on /condition
96 self.check_post({}, url_single, 400)
97 self.check_json_get(url_single, exp_single)
98 # test effect of POST changing title and activeness
99 self.post_exp_cond(all_exps, {'title': 'bar', 'description': 'oof',
101 self.check_json_get(url_single, exp_single)
102 self.check_json_get(url_all, exp_all)
103 # test deletion POST's effect, both to return id=1 into empty single,
104 # full /conditions into empty list
105 self.post_exp_cond(all_exps, {'delete': ''}, redir_to_id=False)
106 exp_single.set('is_new', True)
107 self.check_json_get(url_single, exp_single)
108 self.check_json_get(url_all, exp_all)
110 def test_GET_condition(self) -> None:
111 """More GET /condition testing, especially for Process relations."""
112 # check expected default status codes
113 self.check_get_defaults('/condition')
114 # check 'is_new' set if id= absent or pointing to not-yet-existing ID
115 exp = ExpectedGetCondition(None)
116 exp.set('is_new', True)
117 self.check_json_get('/condition', exp)
118 exp = ExpectedGetCondition(1)
119 exp.set('is_new', True)
120 self.check_json_get('/condition?id=1', exp)
121 # make Condition and two Processes that among them establish all
122 # possible ConditionsRelations to it, check /condition displays all
123 exp = ExpectedGetCondition(1)
124 self.post_exp_cond([exp], {'title': 'foo', 'description': 'oof'},
126 for i, p in enumerate([('conditions', 'disables'),
127 ('enables', 'blockers')]):
128 self.post_exp_process([exp], {k: [1] for k in p}, i+1)
129 self.check_json_get('/condition?id=1', exp)
131 def test_GET_conditions(self) -> None:
132 """Test GET /conditions."""
133 # test empty result on empty DB, default-settings on empty params
134 exp = ExpectedGetConditions()
135 self.check_json_get('/conditions', exp)
136 # test 'sort_by' default to 'title' (even if set to something else, as
137 # long as without handler) and 'pattern' get preserved
138 exp.set('pattern', 'bar')
139 self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x', exp)
140 exp.set('pattern', '')
141 # test non-empty result, automatic (positive) sorting by title
142 post_cond1 = {'is_active': 0, 'title': 'foo', 'description': 'oof'}
143 post_cond2 = {'is_active': 0, 'title': 'bar', 'description': 'rab'}
144 post_cond3 = {'is_active': 1, 'title': 'baz', 'description': 'zab'}
145 for i, post in enumerate([post_cond1, post_cond2, post_cond3]):
146 self.post_exp_cond([exp], post, i+1, post_to_id=False)
147 self.check_filter(exp, 'conditions', 'sort_by', 'title', [2, 3, 1])
148 # test other sortings
149 self.check_filter(exp, 'conditions', 'sort_by', '-title', [1, 3, 2])
150 self.check_filter(exp, 'conditions', 'sort_by', 'is_active', [1, 2, 3])
151 self.check_filter(exp, 'conditions', 'sort_by', '-is_active',
153 exp.set('sort_by', 'title')
154 # test pattern matching on title
155 exp.lib_del('Condition', 1)
156 self.check_filter(exp, 'conditions', 'pattern', 'ba', [2, 3])
157 # test pattern matching on description
158 exp.lib_wipe('Condition')
159 exp.set_cond_from_post(1, post_cond1)
160 self.check_filter(exp, 'conditions', 'pattern', 'of', [1])