From 789136a88113247ee0f827f10827d7c035631a06 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Fri, 4 Nov 2022 14:39:30 +0800 Subject: [PATCH] Add backdrop api and test --- api/__init__.py | 2 ++ api/s27_backdrop.py | 26 +++++++++++++++++++++ script/sql/create/27.backdrop.sql | 2 ++ test_tjnetwork.py | 39 +++++++++++++++++++++++++++++++ tjnetwork.py | 19 +++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 api/s27_backdrop.py diff --git a/api/__init__.py b/api/__init__.py index 6afb550..1594dca 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -70,3 +70,5 @@ from .s23_options import OPTION_QUALITY_NONE, OPTION_QUALITY_CHEMICAL, OPTION_QU from .s23_options import get_option_schema, get_option, set_option from .s24_coordinates import get_node_coord + +from .s27_backdrop import get_backdrop_schema, get_backdrop, set_backdrop diff --git a/api/s27_backdrop.py b/api/s27_backdrop.py new file mode 100644 index 0000000..84d4da6 --- /dev/null +++ b/api/s27_backdrop.py @@ -0,0 +1,26 @@ +from .operation import * + + +def get_backdrop_schema(name: str) -> dict[str, dict[str, Any]]: + return { 'content' : {'type': 'str' , 'optional': False , 'readonly': False} } + + +def get_backdrop(name: str) -> dict[str, Any]: + e = read(name, f"select * from backdrop") + return { 'content': e['content'] } + + +def set_backdrop_cache(name: str, cs: ChangeSet) -> SqlChangeSet: + old = get_backdrop(name) + + redo_sql = f"update backdrop set content = '{cs.operations[0]['content']}' where content = '{old['content']}';" + undo_sql = f"update backdrop set content = '{old['content']}' where content = '{cs.operations[0]['content']}';" + + redo_cs = g_update_prefix | { 'type': 'backdrop', 'content': cs.operations[0]['content'] } + undo_cs = g_update_prefix | { 'type': 'backdrop', 'content': old['content'] } + + return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) + + +def set_backdrop(name: str, cs: ChangeSet) -> ChangeSet: + return execute_command(name, set_backdrop_cache(name, cs)) diff --git a/script/sql/create/27.backdrop.sql b/script/sql/create/27.backdrop.sql index d664e97..8cd174b 100644 --- a/script/sql/create/27.backdrop.sql +++ b/script/sql/create/27.backdrop.sql @@ -4,3 +4,5 @@ create table backdrop ( content text primary key ); + +insert into backdrop (content) values (''); diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 2a53168..cf4a1a3 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -2488,5 +2488,44 @@ class TestApi: self.leave(p) + # 27 backdrop + + + def test_backdrop(self): + p = 'test_backdrop' + self.enter(p) + + assert get_backdrop(p)['content'] == '' + + set_backdrop(p, ChangeSet({'content': 'x'})) + assert get_backdrop(p)['content'] == 'x' + + self.leave(p) + + + def test_backdrop_op(self): + p = 'test_backdrop_op' + self.enter(p) + + cs = set_backdrop(p, ChangeSet({'content': 'x'})).operations[0] + assert cs['operation'] == API_UPDATE + assert cs['type'] == 'backdrop' + assert cs['content'] == 'x' + + cs = execute_undo(p).operations[0] + assert cs['operation'] == API_UPDATE + assert cs['type'] == 'backdrop' + assert cs['content'] == '' + + cs = execute_redo(p).operations[0] + assert cs['operation'] == API_UPDATE + assert cs['type'] == 'backdrop' + assert cs['content'] == 'x' + + self.leave(p) + + + + if __name__ == '__main__': pytest.main() diff --git a/tjnetwork.py b/tjnetwork.py index d2a20b6..86b37d0 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -530,3 +530,22 @@ def set_option(name: str, cs: ChangeSet) -> ChangeSet: def get_node_coord(name: str, node_id: str) -> dict[str, float] | None: return api.get_node_coord(name, node_id) + + +############################################################ +# backdrop 27.[BACKDROP] +############################################################ + +def get_backdrop_schema(name: str) -> dict[str, dict[str, Any]]: + return api.get_backdrop_schema(name) + +def get_backdrop(name: str) -> dict[str, Any]: + return api.get_backdrop(name) + +def set_backdrop(name: str, cs: ChangeSet) -> ChangeSet: + return api.set_backdrop(name, cs) + + +############################################################ +# end 28.[END] +############################################################