From 1109ca4b6d7c52564674cb2f2adc73f79df2e905 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Sat, 29 Oct 2022 19:09:34 +0800 Subject: [PATCH] Add rule api and test --- api/__init__.py | 2 ++ api/s14_rules.py | 26 ++++++++++++++++++++++++++ script/sql/create/14.rules.sql | 2 ++ test_tjnetwork.py | 28 ++++++++++++++-------------- tjnetwork.py | 15 +++++++++++++++ 5 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 api/s14_rules.py diff --git a/api/__init__.py b/api/__init__.py index 7755fe8..dde87a2 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -52,6 +52,8 @@ from .s12_curves import get_curve_schema, get_curve, set_curve from .s13_controls import get_control_schema, get_control, set_control +from .s14_rules import get_rule_schema, get_rule, set_rule + 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/s14_rules.py b/api/s14_rules.py new file mode 100644 index 0000000..f8bed6c --- /dev/null +++ b/api/s14_rules.py @@ -0,0 +1,26 @@ +from .operation import * + + +def get_rule_schema(name: str) -> dict[str, dict[str, Any]]: + return { 'rule' : {'type': 'str' , 'optional': False , 'readonly': False} } + + +def get_rule(name: str) -> dict[str, Any]: + e = read(name, f"select * from rules") + return { 'rule': e['rule'] } + + +def set_rule_cache(name: str, cs: ChangeSet) -> SqlChangeSet: + old = get_rule(name) + + redo_sql = f"update rules set rule = '{cs.operations[0]['rule']}' where rule = '{old['rule']}';" + undo_sql = f"update rules set rule = '{old['rule']}' where rule = '{cs.operations[0]['rule']}';" + + redo_cs = g_update_prefix | { 'type': 'rule', 'rule': cs.operations[0]['rule'] } + undo_cs = g_update_prefix | { 'type': 'rule', 'rule': old['rule'] } + + return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs) + + +def set_rule(name: str, cs: ChangeSet) -> ChangeSet: + return execute_command(name, set_rule_cache(name, cs)) diff --git a/script/sql/create/14.rules.sql b/script/sql/create/14.rules.sql index bfd74c3..8c09e06 100644 --- a/script/sql/create/14.rules.sql +++ b/script/sql/create/14.rules.sql @@ -4,3 +4,5 @@ create table rules ( rule text primary key ); + +insert into rules (rule) values (''); diff --git a/test_tjnetwork.py b/test_tjnetwork.py index b20c675..aa8e8c7 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1264,36 +1264,36 @@ class TestApi: self.leave(p) - def test_control(self): - p = 'test_control' + def test_rule(self): + p = 'test_rule' self.enter(p) - assert get_control(p)['control'] == '' + assert get_rule(p)['rule'] == '' - set_control(p, ChangeSet({'control': 'x'})) - assert get_control(p)['control'] == 'x' + set_rule(p, ChangeSet({'rule': 'x'})) + assert get_rule(p)['rule'] == 'x' self.leave(p) - def test_control_op(self): - p = 'test_control_op' + def test_rule_op(self): + p = 'test_rule_op' self.enter(p) - cs = set_control(p, ChangeSet({'control': 'x'})).operations[0] + cs = set_rule(p, ChangeSet({'rule': 'x'})).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'control' - assert cs['control'] == 'x' + assert cs['type'] == 'rule' + assert cs['rule'] == 'x' cs = execute_undo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'control' - assert cs['control'] == '' + assert cs['type'] == 'rule' + assert cs['rule'] == '' cs = execute_redo(p).operations[0] assert cs['operation'] == API_UPDATE - assert cs['type'] == 'control' - assert cs['control'] == 'x' + assert cs['type'] == 'rule' + assert cs['rule'] == 'x' self.leave(p) diff --git a/tjnetwork.py b/tjnetwork.py index 41e3e0b..31c5a3d 100644 --- a/tjnetwork.py +++ b/tjnetwork.py @@ -429,6 +429,21 @@ def set_control(name: str, cs: ChangeSet) -> ChangeSet: return api.set_control(name, cs) +############################################################ +# rule 12.[CONTROLS] +############################################################ + +def get_rule_schema(name: str) -> dict[str, dict[str, Any]]: + return api.get_rule_schema(name) + +def get_rule(name: str) -> dict[str, Any]: + return api.get_rule(name) + +# example: set_rule(p, ChangeSet({'rule': 'x'})) +def set_rule(name: str, cs: ChangeSet) -> ChangeSet: + return api.set_rule(name, cs) + + ############################################################ # emitter 16.[EMITTERS] ############################################################