home · contact · privacy
Re-organize testing.
[plomtask] / tests / conditions.py
1 """Test Conditions module."""
2 from typing import Any
3 from tests.utils import (TestCaseSansDB, TestCaseWithDB, TestCaseWithServer,
4                          Expected)
5 from plomtask.conditions import Condition
6 from plomtask.processes import Process
7 from plomtask.todos import Todo
8 from plomtask.exceptions import HandledException
9
10
11 class TestsSansDB(TestCaseSansDB):
12     """Tests requiring no DB setup."""
13     checked_class = Condition
14
15
16 class TestsWithDB(TestCaseWithDB):
17     """Tests requiring DB, but not server setup."""
18     checked_class = Condition
19     default_init_kwargs = {'is_active': False}
20
21     def test_remove(self) -> None:
22         """Test .remove() effects on DB and cache."""
23         super().test_remove()
24         proc = Process(None)
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):
30             c = Condition(None)
31             c.save(self.db_conn)
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)
40
41
42 class ExpectedGetConditions(Expected):
43     """Builder of expectations for GET /conditions."""
44     _default_dict = {'sort_by': 'title', 'pattern': ''}
45
46     def recalc(self) -> None:
47         """Update internal dictionary by subclass-specific rules."""
48         super().recalc()
49         self._fields['conditions'] = self.as_ids(self.lib_all('Condition'))
50
51
52 class ExpectedGetCondition(Expected):
53     """Builder of expectations for GET /condition."""
54     _on_empty_make_temp = ('Condition', 'cond_as_dict')
55
56     def __init__(self, id_: int, *args: Any, **kwargs: Any) -> None:
57         self._fields = {'condition': id_}
58         super().__init__(*args, **kwargs)
59
60     def recalc(self) -> None:
61         """Update internal dictionary by subclass-specific rules."""
62         super().recalc()
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
71
72
73 class TestsWithServer(TestCaseWithServer):
74     """Module tests against our HTTP server/handler (and database)."""
75
76     def test_fail_POST_condition(self) -> None:
77         """Test malformed/illegal POST /condition requests."""
78         # check incomplete POST payloads
79         url = '/condition'
80         self.check_post({}, url, 400)
81         self.check_post({'title': ''}, url, 400)
82         self.check_post({'title': '', 'description': ''}, url, 400)
83         self.check_post({'title': '', 'is_active': False}, url, 400)
84         self.check_post({'description': '', 'is_active': False}, url, 400)
85         # check valid POST payload on bad paths
86         valid_payload = {'title': '', 'description': '', 'is_active': False}
87         self.check_post(valid_payload, '/condition?id=foo', 400)
88
89     def test_POST_condition(self) -> None:
90         """Test (valid) POST /condition and its effect on GET /condition[s]."""
91         exp_single = ExpectedGetCondition(1)
92         exp_all = ExpectedGetConditions()
93         all_exps = [exp_single, exp_all]
94         # test valid POST's effect on single /condition and full /conditions
95         post = {'title': 'foo', 'description': 'oof', 'is_active': False}
96         self.post_exp_cond(all_exps, 1, post, '', '?id=1')
97         self.check_json_get('/condition?id=1', exp_single)
98         self.check_json_get('/conditions', exp_all)
99         # test (no) effect of invalid POST to existing Condition on /condition
100         self.check_post({}, '/condition?id=1', 400)
101         self.check_json_get('/condition?id=1', exp_single)
102         # test effect of POST changing title and activeness
103         post = {'title': 'bar', 'description': 'oof', 'is_active': True}
104         self.post_exp_cond(all_exps, 1, post, '?id=1', '?id=1')
105         self.check_json_get('/condition?id=1', exp_single)
106         self.check_json_get('/conditions', exp_all)
107         # test deletion POST's effect, both to return id=1 into empty single,
108         # full /conditions into empty list
109         self.post_exp_cond(all_exps, 1, {'delete': ''}, '?id=1', 's')
110         self.check_json_get('/condition?id=1', exp_single)
111         self.check_json_get('/conditions', exp_all)
112
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         # make Condition and two Processes that among them establish all
118         # possible ConditionsRelations to it, check /condition displays all
119         exp = ExpectedGetCondition(1)
120         cond_post = {'title': 'foo', 'description': 'oof', 'is_active': False}
121         self.post_exp_cond([exp], 1, cond_post, '', '?id=1')
122         proc1_post = {'title': 'A', 'description': '', 'effort': 1.1,
123                       'conditions': [1], 'disables': [1]}
124         proc2_post = {'title': 'B', 'description': '', 'effort': 0.9,
125                       'enables': [1], 'blockers': [1]}
126         self.post_exp_process([exp], proc1_post, 1)
127         self.post_exp_process([exp], proc2_post, 2)
128         self.check_json_get('/condition?id=1', exp)
129
130     def test_GET_conditions(self) -> None:
131         """Test GET /conditions."""
132         # test empty result on empty DB, default-settings on empty params
133         exp = ExpectedGetConditions()
134         self.check_json_get('/conditions', exp)
135         # test ignorance of meaningless non-empty params (incl. unknown key),
136         # that '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')  # preserved despite zero effect!
139         exp.set('sort_by', 'title')  # for clarity (already default)
140         self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x', exp)
141         # test non-empty result, automatic (positive) sorting by title
142         post_cond1 = {'is_active': False, 'title': 'foo', 'description': 'oof'}
143         post_cond2 = {'is_active': False, 'title': 'bar', 'description': 'rab'}
144         post_cond3 = {'is_active': True, '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         exp.set('pattern', '')
148         exp.force('conditions', [2, 3, 1])
149         self.check_json_get('/conditions', exp)
150         # test other sortings
151         exp.set('sort_by', '-title')
152         exp.force('conditions', [1, 3, 2])
153         self.check_json_get('/conditions?sort_by=-title', exp)
154         exp.set('sort_by', 'is_active')
155         exp.force('conditions', [1, 2, 3])
156         self.check_json_get('/conditions?sort_by=is_active', exp)
157         exp.set('sort_by', '-is_active')
158         exp.force('conditions', [3, 2, 1])
159         self.check_json_get('/conditions?sort_by=-is_active', exp)
160         # test pattern matching on title
161         exp.set('sort_by', 'title')
162         exp.set('pattern', 'ba')
163         exp.force('conditions', [2, 3])
164         exp.lib_del('Condition', 1)
165         self.check_json_get('/conditions?pattern=ba', exp)
166         # test pattern matching on description
167         exp.set('pattern', 'of')
168         exp.lib_wipe('Condition')
169         exp.set_cond_from_post(1, post_cond1)
170         exp.force('conditions', [1])
171         self.check_json_get('/conditions?pattern=of', exp)