From 44475cf0056e8620be42763240952369bc3dd068 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sat, 29 Oct 2022 19:04:42 +0800 Subject: [PATCH] Add control api and test --- api/__init__.py | 2 ++ api/s13_controls.py | 26 ++++++++++++++++++++ script/sql/create/13.controls.sql | 2 ++ test_tjnetwork.py | 41 +++++++++++++++++++++++++++++++ tjnetwork.py | 15 +++++++++++ 5 files changed, 86 insertions(+) create mode 100644 api/s13_controls.py diff --git a/api/__init__.py b/api/__init__.py index 4f2c38b..7755fe8 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -50,6 +50,8 @@ from .s11_patterns import get_pattern_schema, get_pattern, set_pattern from .s12_curves import get_curve_schema, get_curve, set_curve +from .s13_controls import get_control_schema, get_control, set_control + from .s16_emitters import get_emitter_schema, get_emitter, set_emitter from .s21_times import TIME_STATISTIC_NONE, TIME_STATISTIC_AVERAGED, TIME_STATISTIC_MINIMUM, TIME_STATISTIC_MAXIMUM, TIME_STATISTIC_RANGE diff --git a/api/s13_controls.py b/api/s13_controls.py new file mode 100644 index 0000000..1deda79 --- /dev/null +++ b/api/s13_controls.py @@ -0,0 +1,26 @@ +from .operation import * + + +def get_control_schema(name: str) -> dict[str, dict[str, Any]]: + return { 'control' : {'type': 'str' , 'optional': False , 'readonly': False} } + + +def get_control(name: str) -> dict[str, Any]: + e = read(name, f"select * from controls") + return { 'control': e['control'] } + + +def set_control_cache(name: str, cs: ChangeSet) -> SqlChangeSet: + old = get_control(name) + + redo_sql = f"update controls set control = '{cs.operations[0]['control']}' where control = '{old['control']}';" + undo_sql = f"update controls set control = '{old['control']}' where control = '{cs.operations[0]['control']}';" + + redo_cs = g_update_prefix | { 'type': 'control', 'control': cs.operations[0]['control'] } + undo_cs = g_update_prefix | { 'type': 'control', 'control': old['control'] } + + return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) + + +def set_control(name: str, cs: ChangeSet) -> ChangeSet: + return execute_command(name, set_control_cache(name, cs)) diff --git a/script/sql/create/13.controls.sql b/script/sql/create/13.controls.sql index 335db45..38c925a 100644 --- a/script/sql/create/13.controls.sql +++ b/script/sql/create/13.controls.sql @@ -4,3 +4,5 @@ create table controls ( control text primary key ); + +insert into controls (control) values (''); diff --git a/test_tjnetwork.py b/test_tjnetwork.py index e33b971..b20c675 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1206,6 +1206,7 @@ class TestApi: self.leave(p) + def test_tag_op(self): p = 'test_tag_op' self.enter(p) @@ -1219,36 +1220,42 @@ class TestApi: assert is_pipe(p, 'p0') cs = set_tag(p, ChangeSet({'t_type': TAG_TYPE_NODE, 'id': 'j1', 'tag': 'j1t' })).operations[0] + assert cs['operation'] == API_UPDATE assert cs['type'] == 'tag' assert cs['t_type'] == TAG_TYPE_NODE assert cs['id'] == 'j1' assert cs['tag'] == 'j1t' cs = execute_undo(p).operations[0] + assert cs['operation'] == API_UPDATE assert cs['type'] == 'tag' assert cs['t_type'] == TAG_TYPE_NODE assert cs['id'] == 'j1' assert cs['tag'] == None cs = execute_redo(p).operations[0] + assert cs['operation'] == API_UPDATE assert cs['type'] == 'tag' assert cs['t_type'] == TAG_TYPE_NODE assert cs['id'] == 'j1' assert cs['tag'] == 'j1t' cs = set_tag(p, ChangeSet({'t_type': TAG_TYPE_LINK, 'id': 'p0', 'tag': 'p0t' })).operations[0] + assert cs['operation'] == API_UPDATE assert cs['type'] == 'tag' assert cs['t_type'] == TAG_TYPE_LINK assert cs['id'] == 'p0' assert cs['tag'] == 'p0t' cs = execute_undo(p).operations[0] + assert cs['operation'] == API_UPDATE assert cs['type'] == 'tag' assert cs['t_type'] == TAG_TYPE_LINK assert cs['id'] == 'p0' assert cs['tag'] == None cs = execute_redo(p).operations[0] + assert cs['operation'] == API_UPDATE assert cs['type'] == 'tag' assert cs['t_type'] == TAG_TYPE_LINK assert cs['id'] == 'p0' @@ -1257,6 +1264,40 @@ class TestApi: self.leave(p) + def test_control(self): + p = 'test_control' + self.enter(p) + + assert get_control(p)['control'] == '' + + set_control(p, ChangeSet({'control': 'x'})) + assert get_control(p)['control'] == 'x' + + self.leave(p) + + + def test_control_op(self): + p = 'test_control_op' + self.enter(p) + + cs = set_control(p, ChangeSet({'control': 'x'})).operations[0] + assert cs['operation'] == API_UPDATE + assert cs['type'] == 'control' + assert cs['control'] == 'x' + + cs = execute_undo(p).operations[0] + assert cs['operation'] == API_UPDATE + assert cs['type'] == 'control' + assert cs['control'] == '' + + cs = execute_redo(p).operations[0] + assert cs['operation'] == API_UPDATE + assert cs['type'] == 'control' + assert cs['control'] == 'x' + + self.leave(p) + + def test_demand(self): p = 'test_demand' self.enter(p) diff --git a/tjnetwork.py b/tjnetwork.py index 7579f49..41e3e0b 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -414,6 +414,21 @@ def set_curve(name: str, cs: ChangeSet) -> ChangeSet: return api.set_curve(name, cs) +############################################################ +# control 12.[CONTROLS] +############################################################ + +def get_control_schema(name: str) -> dict[str, dict[str, Any]]: + return api.get_control_schema(name) + +def get_control(name: str) -> dict[str, Any]: + return api.get_control(name) + +# example: set_control(p, ChangeSet({'control': 'x'})) +def set_control(name: str, cs: ChangeSet) -> ChangeSet: + return api.set_control(name, cs) + + ############################################################ # emitter 16.[EMITTERS] ############################################################