From: Christian Heller Date: Sun, 14 Jul 2024 17:23:25 +0000 (+0200) Subject: Slightly improve/re-factor Day tests. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/blog?a=commitdiff_plain;h=26bb302fb27cb06deed1d5fe72c474ad9e1e11e9;p=plomtask Slightly improve/re-factor Day tests. --- diff --git a/tests/days.py b/tests/days.py index f57c532..3297032 100644 --- a/tests/days.py +++ b/tests/days.py @@ -108,37 +108,9 @@ class TestsWithServer(TestCaseWithServer): date = datetime.now() + timedelta(days=n) return date.strftime(TESTING_DATE_FORMAT) - @classmethod - def GET_day_dict(cls, date: str) -> dict[str, object]: - """Return JSON of GET /day to expect.""" - # day: dict[str, object] = {'id': date, 'comment': '', 'todos': []} - day = cls._day_as_dict(date) - d: dict[str, object] = {'day': date, - 'top_nodes': [], - 'make_type': '', - 'enablers_for': {}, - 'disablers_for': {}, - 'conditions_present': [], - 'processes': [], - '_library': {'Day': cls.as_refs([day])}} - return d - - @classmethod - def GET_calendar_dict(cls, start: int, end: int) -> dict[str, object]: - """Return JSON of GET /calendar to expect. - - NB: the date string list to key 'days' implies/expects a continuous (= - gaps filled) alphabetical order of dates by virtue of range(start, - end+1) and date_in_n_days. - """ - today_date = cls._testing_date_in_n_days(0) - start_date = cls._testing_date_in_n_days(start) - end_date = cls._testing_date_in_n_days(end) - dates = [cls._testing_date_in_n_days(i) for i in range(start, end+1)] - days = [cls._day_as_dict(d) for d in dates] - library = {'Day': cls.as_refs(days)} if len(days) > 0 else {} - return {'today': today_date, 'start': start_date, 'end': end_date, - 'days': dates, '_library': library} + @staticmethod + def _day_as_dict(date: str) -> dict[str, object]: + return {'id': date, 'comment': '', 'todos': []} @staticmethod def _todo_as_dict(id_: int = 1, @@ -172,29 +144,23 @@ class TestsWithServer(TestCaseWithServer): return {'children': [], 'seen': False, 'todo': todo_id} @staticmethod - def _day_as_dict(date: str) -> dict[str, object]: - return {'id': date, 'comment': '', 'todos': []} - - @staticmethod - def _post_batch(list_of_args: list[list[object]], - names_of_simples: list[str], - names_of_versioneds: list[str], - f_as_dict: Callable[..., dict[str, object]], - f_to_post: Callable[..., None | dict[str, object]] - ) -> list[dict[str, object]]: - """Post expected=f_as_dict(*args) as input to f_to_post, for many.""" - expecteds = [] - for args in list_of_args: - expecteds += [f_as_dict(*args)] - for expected in expecteds: - assert isinstance(expected['_versioned'], dict) - post = {} - for name in names_of_simples: - post[name] = expected[name] - for name in names_of_versioneds: - post[name] = expected['_versioned'][name][0] - f_to_post(expected['id'], post) - return expecteds + def _post_args_return_expectation( + args: list[object], + names_of_simples: list[str], + names_of_versioneds: list[str], + f_as_dict: Callable[..., dict[str, object]], + f_to_post: Callable[..., None | dict[str, object]] + ) -> dict[str, object]: + """Create expected=f_as_dict(*args), post as names_* with f_to_post.""" + expected = f_as_dict(*args) + assert isinstance(expected['_versioned'], dict) + to_post = {} + for name in names_of_simples: + to_post[name] = expected[name] + for name in names_of_versioneds: + to_post[name] = expected['_versioned'][name][0] + f_to_post(expected['id'], to_post) + return expected def _post_day(self, params: str = '', form_data: None | dict[str, object] = None, @@ -209,6 +175,38 @@ class TestsWithServer(TestCaseWithServer): redir_to = f'{target}&make_type={form_data["make_type"]}' self.check_post(form_data, target, status, redir_to) + @classmethod + def GET_day_dict(cls, date: str) -> dict[str, object]: + """Return JSON of GET /day to expect.""" + # day: dict[str, object] = {'id': date, 'comment': '', 'todos': []} + day = cls._day_as_dict(date) + d: dict[str, object] = {'day': date, + 'top_nodes': [], + 'make_type': '', + 'enablers_for': {}, + 'disablers_for': {}, + 'conditions_present': [], + 'processes': [], + '_library': {'Day': cls.as_refs([day])}} + return d + + @classmethod + def GET_calendar_dict(cls, start: int, end: int) -> dict[str, object]: + """Return JSON of GET /calendar to expect. + + NB: the date string list to key 'days' implies/expects a continuous (= + gaps filled) alphabetical order of dates by virtue of range(start, + end+1) and date_in_n_days. + """ + today_date = cls._testing_date_in_n_days(0) + start_date = cls._testing_date_in_n_days(start) + end_date = cls._testing_date_in_n_days(end) + dates = [cls._testing_date_in_n_days(i) for i in range(start, end+1)] + days = [cls._day_as_dict(d) for d in dates] + library = {'Day': cls.as_refs(days)} if len(days) > 0 else {} + return {'today': today_date, 'start': start_date, 'end': end_date, + 'days': dates, '_library': library} + def test_basic_GET_day(self) -> None: """Test basic (no Processes/Conditions/Todos) GET /day basics.""" # check illegal date parameters @@ -218,19 +216,16 @@ class TestsWithServer(TestCaseWithServer): date = self._testing_date_in_n_days(0) expected = self.GET_day_dict(date) self.check_json_get('/day', expected) - # NB: GET ?date="today"/"yesterday"/"tomorrow" in test_basic_POST_day - # check 'make_type' GET parameter affects immediate reply, but … + # check defined day, with and without make_type parameter date = '2024-01-01' expected = self.GET_day_dict(date) expected['make_type'] = 'bar' self.check_json_get(f'/day?date={date}&make_type=bar', expected) - # … not any following, … - expected['make_type'] = '' - self.check_json_get(f'/day?date={date}', expected) - # … not even when part of a POST request - post: dict[str, object] = {'day_comment': '', 'make_type': 'foo'} - self._post_day(f'date={date}', post) - self.check_json_get(f'/day?date={date}', expected) + # check parsing of 'yesterday', 'today', 'tomorrow' + for name, dist in [('yesterday', -1), ('today', 0), ('tomorrow', +1)]: + date = self._testing_date_in_n_days(dist) + expected = self.GET_day_dict(date) + self.check_json_get(f'/day?date={name}', expected) def test_fail_POST_day(self) -> None: """Test malformed/illegal POST /day requests.""" @@ -288,12 +283,12 @@ class TestsWithServer(TestCaseWithServer): self.check_post(post, '/day?date=foo', 400) def test_basic_POST_day(self) -> None: - """Test basic (no Todos) POST /day. + """Test basic (no Processes/Conditions/Todos) POST /day. - Check POST (& GET!) requests properly parse 'today', 'tomorrow', - 'yesterday', and actual date strings; + Check POST requests properly parse 'today', 'tomorrow', 'yesterday', + and actual date strings; preserve 'make_type' setting in redirect even if nonsensical; - and store 'day_comment' + and store 'day_comment'. """ for name, dist, test_str in [('2024-01-01', None, 'a'), ('today', 0, 'b'), @@ -312,16 +307,19 @@ class TestsWithServer(TestCaseWithServer): def test_GET_day_with_processes_and_todos(self) -> None: """Test GET /day displaying Processes and Todos (no trees).""" date = '2024-01-01' - # check Processes get displayed in ['processes'] and ['_library'] - procs_data = [[1, 'foo', 'oof', 1.1], [2, 'bar', 'rab', 0.9]] - procs_expected = self._post_batch(procs_data, [], - ['title', 'description', 'effort'], - self.proc_as_dict, self.post_process) + # check Processes get displayed in ['processes'] and ['_library'], + # even without any Todos referencing them + procs_data = [[1, 'foo', 'oof', 1.1], # id, title, desc, effort + [2, 'bar', 'rab', 0.9]] + procs_expected = [] + for p_data in procs_data: + procs_expected += [self._post_args_return_expectation( + p_data, [], ['title', 'description', 'effort'], + self.proc_as_dict, self.post_process)] expected = self.GET_day_dict(date) assert isinstance(expected['_library'], dict) expected['processes'] = self.as_id_list(procs_expected) expected['_library']['Process'] = self.as_refs(procs_expected) - self._post_day(f'date={date}') self.check_json_get(f'/day?date={date}', expected) # post Todos of either process and check their display post_day: dict[str, object] @@ -348,17 +346,25 @@ class TestsWithServer(TestCaseWithServer): """Test GET /day displaying Conditions and their relations.""" date = '2024-01-01' # add Process with Conditions and their Todos, check display - conds_data = [[1, False, ['A'], ['a']], [2, True, ['B'], ['b']]] - conds_expected = self._post_batch( - conds_data, ['is_active'], ['title', 'description'], + conds_data = [[1, False, ['A'], ['a']], # id, is_active, title, desc + [2, True, ['B'], ['b']]] + conds_expected = [] + for c_data in conds_data: + conds_expected += [self._post_args_return_expectation( + c_data, ['is_active'], ['title', 'description'], self.cond_as_dict, - lambda x, y: self.check_post(y, f'/condition?id={x}', 302)) - cond_names = ['conditions', 'disables', 'blockers', 'enables'] - procs_data = [[1, 'foo', 'oof', 1.1, [1], [1], [2], [2]], + lambda x, y: self.check_post(y, f'/condition?id={x}'))] + procs_data = [ # id, title, desc, effort, + # conditions, disables, blockers, enables + [1, 'foo', 'oof', 1.1, [1], [1], [2], [2]], [2, 'bar', 'rab', 0.9, [2], [2], [1], [1]]] - procs_expected = self._post_batch(procs_data, cond_names, - ['title', 'description', 'effort'], - self.proc_as_dict, self.post_process) + procs_expected = [] + for p_data in procs_data: + procs_expected += [self._post_args_return_expectation( + p_data, + ['conditions', 'disables', 'blockers', 'enables'], + ['title', 'description', 'effort'], + self.proc_as_dict, self.post_process)] expected = self.GET_day_dict(date) assert isinstance(expected['_library'], dict) expected['processes'] = self.as_id_list(procs_expected) @@ -369,7 +375,8 @@ class TestsWithServer(TestCaseWithServer): # add Todos in relation to Conditions, check consequences post_day: dict[str, object] post_day = {'day_comment': '', 'make_type': '', 'new_todo': [1, 2]} - todos = [self._todo_as_dict(1, 1, date, [1], [1], [2], [2]), + todos = [ # id, process_id, date, conds, disables, blockers, enables + self._todo_as_dict(1, 1, date, [1], [1], [2], [2]), self._todo_as_dict(2, 2, date, [2], [2], [1], [1])] expected['_library']['Todo'] = self.as_refs(todos) expected['_library']['Day'][date]['todos'] = self.as_id_list(todos) @@ -398,12 +405,12 @@ class TestsWithServer(TestCaseWithServer): self.check_json_get('/calendar?start=tomorrow&end=today', expected) # check saved day shows up in results, proven by its comment post_day: dict[str, object] = {'day_comment': 'foo', 'make_type': ''} - date1 = self._testing_date_in_n_days(-2) - self._post_day(f'date={date1}', post_day) + date = self._testing_date_in_n_days(-2) + self._post_day(f'date={date}', post_day) start_date = self._testing_date_in_n_days(-5) end_date = self._testing_date_in_n_days(+5) url = f'/calendar?start={start_date}&end={end_date}' expected = self.GET_calendar_dict(-5, +5) assert isinstance(expected['_library'], dict) - expected['_library']['Day'][date1]['comment'] = post_day['day_comment'] + expected['_library']['Day'][date]['comment'] = post_day['day_comment'] self.check_json_get(url, expected)