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 _on_empty_make_temp = ('Condition', 'cond_as_dict')
56 def __init__(self, id_: int, *args: Any, **kwargs: Any) -> None:
57 self._fields = {'condition': id_}
58 super().__init__(*args, **kwargs)
60 def recalc(self) -> None:
61 """Update internal dictionary by subclass-specific rules."""
63 for p_field, c_field in [('conditions', 'enabled_processes'),
64 ('disables', 'disabling_processes'),
65 ('blockers', 'disabled_processes'),
66 ('enables', 'enabling_processes')]:
67 self._fields[c_field] = self.as_ids([
68 p for p in self.lib_all('Process')
69 if self._fields['condition'] in p[p_field]])
70 self._fields['is_new'] = False
73 class TestsWithServer(TestCaseWithServer):
74 """Module tests against our HTTP server/handler (and database)."""
76 def test_fail_POST_condition(self) -> None:
77 """Test malformed/illegal POST /condition requests."""
78 # check incomplete POST payloads
80 self.check_post({}, url, 400)
81 self.check_post({'title': ''}, url, 400)
82 self.check_post({'title': '', 'is_active': 0}, url, 400)
83 self.check_post({'description': '', 'is_active': 0}, url, 400)
84 # check valid POST payload on bad paths
85 valid_payload = {'title': '', 'description': '', 'is_active': 0}
86 self.check_post(valid_payload, '/condition?id=foo', 400)
88 def test_POST_condition(self) -> None:
89 """Test (valid) POST /condition and its effect on GET /condition[s]."""
90 exp_single = ExpectedGetCondition(1)
91 exp_all = ExpectedGetConditions()
92 all_exps = [exp_single, exp_all]
93 # test valid POST's effect on single /condition and full /conditions
94 post = {'title': 'foo', 'description': 'oof', 'is_active': 0}
95 self.post_exp_cond(all_exps, 1, post, '', '?id=1')
96 self.check_json_get('/condition?id=1', exp_single)
97 self.check_json_get('/conditions', exp_all)
98 # test (no) effect of invalid POST to existing Condition on /condition
99 self.check_post({}, '/condition?id=1', 400)
100 self.check_json_get('/condition?id=1', exp_single)
101 # test effect of POST changing title and activeness
102 post = {'title': 'bar', 'description': 'oof', 'is_active': 1}
103 self.post_exp_cond(all_exps, 1, post, '?id=1', '?id=1')
104 self.check_json_get('/condition?id=1', exp_single)
105 self.check_json_get('/conditions', 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, 1, {'delete': ''}, '?id=1', 's')
109 self.check_json_get('/condition?id=1', exp_single)
110 self.check_json_get('/conditions', exp_all)
112 def test_GET_condition(self) -> None:
113 """More GET /condition testing, especially for Process relations."""
114 # check expected default status codes
115 self.check_get_defaults('/condition')
116 # make Condition and two Processes that among them establish all
117 # possible ConditionsRelations to it, check /condition displays all
118 exp = ExpectedGetCondition(1)
119 cond_post = {'title': 'foo', 'description': 'oof', 'is_active': 0}
120 self.post_exp_cond([exp], 1, cond_post, '', '?id=1')
121 proc1_post = {'title': 'A', 'description': '', 'effort': 1.1,
122 'conditions': [1], 'disables': [1]}
123 proc2_post = {'title': 'B', 'description': '', 'effort': 0.9,
124 'enables': [1], 'blockers': [1]}
125 self.post_exp_process([exp], proc1_post, 1)
126 self.post_exp_process([exp], proc2_post, 2)
127 self.check_json_get('/condition?id=1', exp)
129 def test_GET_conditions(self) -> None:
130 """Test GET /conditions."""
131 # test empty result on empty DB, default-settings on empty params
132 exp = ExpectedGetConditions()
133 self.check_json_get('/conditions', exp)
134 # test ignorance of meaningless non-empty params (incl. unknown key),
135 # that 'sort_by' default to 'title' (even if set to something else, as
136 # long as without handler) and 'pattern' get preserved
137 exp.set('pattern', 'bar')
138 exp.set('sort_by', 'title') # for clarity (already default)
139 self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x', exp)
140 # test non-empty result, automatic (positive) sorting by title
141 exp.set('pattern', '')
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], i+1, post, '', f'?id={i+1}')
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 # test pattern matching on title
154 exp.set('sort_by', '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])