From: Christian Heller Date: Mon, 17 Jun 2024 18:33:51 +0000 (+0200) Subject: Extend Conditions POST test to use new JSON interface. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/move_up?a=commitdiff_plain;h=84ff28d055b785e5f205f942c68ece72136e5af1;p=plomtask Extend Conditions POST test to use new JSON interface. --- diff --git a/plomtask/versioned_attributes.py b/plomtask/versioned_attributes.py index b04c564..cc42bbc 100644 --- a/plomtask/versioned_attributes.py +++ b/plomtask/versioned_attributes.py @@ -28,9 +28,7 @@ class VersionedAttribute: @property def as_dict(self) -> dict[str, object]: """Return self as (json.dumps-coompatible) dict.""" - d = {'parent_id': self.parent.id_, - 'table_name': self.table_name, - 'history': self.history} + d = {'parent_id': self.parent.id_, 'history': self.history} return d @property diff --git a/tests/conditions.py b/tests/conditions.py index a316d01..2b522e6 100644 --- a/tests/conditions.py +++ b/tests/conditions.py @@ -68,14 +68,45 @@ class TestsWithServer(TestCaseWithServer): def test_do_POST_condition(self) -> None: """Test POST /condition and its effect on the database.""" - form_data = {'title': 'foo', 'description': 'foo', 'is_active': False} - self.check_post(form_data, '/condition', 302, '/condition?id=1') - self.assertEqual(1, len(Condition.all(self.db_conn))) - form_data['delete'] = '' - self.check_post(form_data, '/condition?id=', 404) - self.check_post(form_data, '/condition?id=2', 404) - self.check_post(form_data, '/condition?id=1', 302, '/conditions') - self.assertEqual(0, len(Condition.all(self.db_conn))) + + def check(path: str, expected: dict[str, object]) -> None: + self.conn.request('GET', path) + response = self.conn.getresponse() + self.assertEqual(response.status, 200) + retrieved = json_loads(response.read().decode()) + self.blank_history_keys_in(retrieved) + self.assertEqual(expected, retrieved) + + # check empty POST fails + self.check_post({}, '/condition', 400) + # test valid POST's effect on … + post = {'title': 'foo', 'description': 'oof', 'is_active': False} + self.check_post(post, '/condition', 302, '/condition?id=1') + # … single /condition + cond = {'id': 1, 'is_active': False, + 'title': {'parent_id': 1, 'history': {'[0]': 'foo'}}, + 'description': {'parent_id': 1, 'history': {'[0]': 'oof'}}} + expected_single = {'is_new': False, + 'enabled_processes': [], + 'disabled_processes': [], + 'enabling_processes': [], + 'disabling_processes': [], + 'condition': cond} + check('/condition?id=1', expected_single) + # … full /conditions + expected_all = {'conditions': [cond], 'sort_by': 'title', 'pattern': ''} + check('/conditions', expected_all) + # test effect of invalid POST to existing Condition on /condition + self.check_post({}, '/condition?id=1', 400) + check('/condition?id=1', expected_single) + # test deletion POST's effect on … + self.check_post({'delete': ''}, '/condition?id=1', 302, '/conditions') + cond['title']['history'] = {} + cond['description']['history'] = {} + check('/condition?id=1', expected_single) + # … full /conditions + expected_all['conditions'] = [] + check('/conditions', expected_all) def test_do_GET_condition(self) -> None: """Test GET /condition.""" @@ -91,12 +122,7 @@ class TestsWithServer(TestCaseWithServer): response = self.conn.getresponse() self.assertEqual(response.status, 200) retrieved_json = json_loads(response.read().decode()) - # ignore history timestamps (too difficult too anticipate) - for cond in retrieved_json['conditions']: - for k in [k for k in cond.keys() if isinstance(cond[k], dict)]: - history = cond[k]['history'] - history = {'[IGNORE]': list(history.values())[0]} - cond[k]['history'] = history + self.blank_history_keys_in(retrieved_json) self.assertEqual(expected_json, retrieved_json) # test empty result on empty DB, default-settings on empty params @@ -117,19 +143,19 @@ class TestsWithServer(TestCaseWithServer): post_3 = {'title': 'baz', 'description': 'zab', 'is_active': True} self.check_post(post_3, '/condition', 302, '/condition?id=3') cond_1 = {'id': 1, 'is_active': False, - 'title': {'history': {'[IGNORE]': 'foo'}, + 'title': {'history': {'[0]': 'foo'}, 'parent_id': 1}, - 'description': {'history': {'[IGNORE]': 'oof'}, + 'description': {'history': {'[0]': 'oof'}, 'parent_id': 1}} cond_2 = {'id': 2, 'is_active': False, - 'title': {'history': {'[IGNORE]': 'bar'}, + 'title': {'history': {'[0]': 'bar'}, 'parent_id': 2}, - 'description': {'history': {'[IGNORE]': 'rab'}, + 'description': {'history': {'[0]': 'rab'}, 'parent_id': 2}} cond_3 = {'id': 3, 'is_active': True, - 'title': {'history': {'[IGNORE]': 'baz'}, + 'title': {'history': {'[0]': 'baz'}, 'parent_id': 3}, - 'description': {'history': {'[IGNORE]': 'zab'}, + 'description': {'history': {'[0]': 'zab'}, 'parent_id': 3}} cons = [cond_2, cond_3, cond_1] expected_json = {'conditions': cons, 'sort_by': 'title', 'pattern': ''} diff --git a/tests/utils.py b/tests/utils.py index 15a53ae..13e4f94 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -268,3 +268,21 @@ class TestCaseWithServer(TestCaseWithDB): self.check_post(form_data, f'/process?id={id_}', 302, f'/process?id={id_}') return form_data + + @staticmethod + def blank_history_keys_in(d: dict[str, object]) -> None: + """Re-write "history" object keys to bracketed integer strings.""" + def walk_tree(d: Any) -> Any: + if isinstance(d, dict): + if 'history' in d.keys(): + vals = d['history'].values() + history = {} + for i, val in enumerate(vals): + history[f'[{i}]'] = val + d['history'] = history + for k in list(d.keys()): + walk_tree(d[k]) + elif isinstance(d, list): + d[:] = [walk_tree(i) for i in d] + return d + walk_tree(d)