home · contact · privacy
Some work on tests, kinda unfinished.
[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
7
8 class TestsSansDB(TestCaseSansDB):
9     """Tests requiring no DB setup."""
10     checked_class = Condition
11
12
13 class TestsWithDB(TestCaseWithDB):
14     """Tests requiring DB, but not server setup."""
15     checked_class = Condition
16     default_init_kwargs = {'is_active': 0}
17
18
19 class ExpectedGetConditions(Expected):
20     """Builder of expectations for GET /conditions."""
21     _default_dict = {'sort_by': 'title', 'pattern': ''}
22
23     def recalc(self) -> None:
24         """Update internal dictionary by subclass-specific rules."""
25         super().recalc()
26         self._fields['conditions'] = self.as_ids(self.lib_all('Condition'))
27
28
29 class ExpectedGetCondition(Expected):
30     """Builder of expectations for GET /condition."""
31     _default_dict = {'is_new': False}
32     _on_empty_make_temp = ('Condition', 'cond_as_dict')
33
34     def __init__(self, id_: int | None, *args: Any, **kwargs: Any) -> None:
35         self._fields = {'condition': id_}
36         super().__init__(*args, **kwargs)
37
38     def recalc(self) -> None:
39         """Update internal dictionary by subclass-specific rules."""
40         super().recalc()
41         for p_field, c_field in [('conditions', 'enabled_processes'),
42                                  ('disables', 'disabling_processes'),
43                                  ('blockers', 'disabled_processes'),
44                                  ('enables', 'enabling_processes')]:
45             self._fields[c_field] = self.as_ids([
46                 p for p in self.lib_all('Process')
47                 if self._fields['condition'] in p[p_field]])
48
49
50 class TestsWithServer(TestCaseWithServer):
51     """Module tests against our HTTP server/handler (and database)."""
52     checked_class = Condition
53
54     def test_fail_POST_condition(self) -> None:
55         """Test malformed/illegal POST /condition requests."""
56         # check incomplete POST payloads
57         valid_payload = {'title': '', 'description': ''}
58         self.check_minimal_inputs('/condition', valid_payload)
59         # check valid POST payload on bad paths
60         self.check_post(valid_payload, '/condition?id=foo', 400)
61         # check cannot delete depended-upon Condition
62         self.post_exp_cond([], {})
63         for key in ('conditions', 'blockers', 'enables', 'disables'):
64             self.post_exp_process([], {key: [1]}, 1)
65             self.check_post({'delete': ''}, '/condition?id=1', 500)
66         self.post_exp_process([], {}, 1)
67         self.post_exp_day([], {'new_todo': '1'})
68         for key in ('conditions', 'blockers', 'enables', 'disables'):
69             self.post_exp_todo([], {key: [1]}, 1)
70             self.check_post({'delete': ''}, '/condition?id=1', 500)
71
72     def test_POST_condition(self) -> None:
73         """Test (valid) POST /condition and its effect on GET /condition[s]."""
74         url_single, url_all = '/condition?id=1', '/conditions'
75         exp_single, exp_all = ExpectedGetCondition(1), ExpectedGetConditions()
76         all_exps = [exp_single, exp_all]
77         # test valid POST's effect on single /condition and full /conditions
78         self.post_exp_cond(all_exps, {}, post_to_id=False)
79         self.check_json_get(url_single, exp_single)
80         self.check_json_get(url_all, exp_all)
81         # test (no) effect of invalid POST to existing Condition on /condition
82         self.check_post({}, url_single, 400)
83         self.check_json_get(url_single, exp_single)
84         # test effect of POST changing title, description, and activeness
85         self.post_exp_cond(all_exps, {'title': 'bar', 'description': 'oof',
86                                       'is_active': 1})
87         self.check_json_get(url_single, exp_single)
88         # test POST sans 'is_active' setting it negative
89         self.post_exp_cond(all_exps, {})
90         self.check_json_get(url_single, exp_single)
91         # test deletion POST's effect, both to return id=1 into empty single,
92         # full /conditions into empty list
93         self.check_json_get(url_single, exp_single)
94         self.post_exp_cond(all_exps, {'delete': ''}, redir_to_id=False)
95         exp_single.set('is_new', True)
96         self.check_json_get(url_single, exp_single)
97         self.check_json_get(url_all, exp_all)
98
99     def test_GET_condition(self) -> None:
100         """More GET /condition testing, especially for Process relations."""
101         # check expected default status codes
102         self.check_get_defaults('/condition')
103         # check 'is_new' set if id= absent or pointing to not-yet-existing ID
104         exp = ExpectedGetCondition(None)
105         exp.set('is_new', True)
106         self.check_json_get('/condition', exp)
107         exp = ExpectedGetCondition(1)
108         exp.set('is_new', True)
109         self.check_json_get('/condition?id=1', exp)
110         # make Condition and two Processes that among them establish all
111         # possible ConditionsRelations to it, check /condition displays all
112         exp = ExpectedGetCondition(1)
113         self.post_exp_cond([exp], {}, post_to_id=False)
114         for i, p in enumerate([('conditions', 'disables'),
115                                ('enables', 'blockers')]):
116             self.post_exp_process([exp], {k: [1] for k in p}, i+1)
117         self.check_json_get('/condition?id=1', exp)
118
119     def test_GET_conditions(self) -> None:
120         """Test GET /conditions."""
121         # test empty result on empty DB, default-settings on empty params
122         exp = ExpectedGetConditions()
123         self.check_json_get('/conditions', exp)
124         # test 'sort_by' default to 'title' (even if set to something else, as
125         # long as without handler) and 'pattern' get preserved
126         exp.set('pattern', 'bar')
127         self.check_json_get('/conditions?sort_by=foo&pattern=bar&foo=x', exp)
128         exp.set('pattern', '')
129         # test non-empty result, automatic (positive) sorting by title
130         post_cond1 = {'is_active': 0, 'title': 'foo', 'description': 'oof'}
131         post_cond2 = {'is_active': 0, 'title': 'bar', 'description': 'rab'}
132         post_cond3 = {'is_active': 1, 'title': 'baz', 'description': 'zab'}
133         for i, post in enumerate([post_cond1, post_cond2, post_cond3]):
134             self.post_exp_cond([exp], post, i+1, post_to_id=False)
135         self.check_filter(exp, 'conditions', 'sort_by', 'title', [2, 3, 1])
136         # test other sortings
137         self.check_filter(exp, 'conditions', 'sort_by', '-title', [1, 3, 2])
138         self.check_filter(exp, 'conditions', 'sort_by', 'is_active', [1, 2, 3])
139         self.check_filter(exp, 'conditions', 'sort_by', '-is_active',
140                           [3, 2, 1])
141         exp.set('sort_by', 'title')
142         # test pattern matching on title
143         exp.lib_del('Condition', 1)
144         self.check_filter(exp, 'conditions', 'pattern', 'ba', [2, 3])
145         # test pattern matching on description
146         exp.lib_wipe('Condition')
147         exp.set_cond_from_post(1, post_cond1)
148         self.check_filter(exp, 'conditions', 'pattern', 'of', [1])