Add pattern api and test

This commit is contained in:
WQY\qiong
2022-10-22 12:55:16 +08:00
parent c1b99bc7eb
commit bb1d772eaa
7 changed files with 123 additions and 13 deletions

44
api/s11_patterns.py Normal file
View File

@@ -0,0 +1,44 @@
from .operation import *
from .s0_base import *
def get_pattern_schema(name: str) -> dict[str, dict[str, Any]]:
return { 'id' : {'type': 'str' , 'optional': False , 'readonly': True },
'factors' : {'type': 'float_list' , 'optional': False , 'readonly': False } }
def get_pattern(name: str, id: str) -> dict[str, Any]:
pas = read_all(name, f"select * from patterns where id = '{id}'")
ps = []
for r in pas:
ps.append(float(r['factor']))
return { 'id': id, 'factors': ps }
def set_pattern(name: str, cs: ChangeSet) -> ChangeSet:
id = cs.operations[0]['id']
old = get_pattern(name, id)
new = { 'id': id, 'factors': [] }
f_id = f"'{id}'"
# TODO: transaction ?
redo_sql = f"delete from patterns where id = {f_id};"
redo_sql += f"\ndelete from _pattern where id = {f_id};"
redo_sql += f"\ninsert into _pattern (id) values ({f_id});"
for factor in cs.operations[0]['factors']:
f_factor = float(factor)
redo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
new['factors'].append(f_factor)
undo_sql = f"delete from patterns where id = {f_id};"
undo_sql += f"\ndelete from _pattern where id = {f_id};"
undo_sql += f"\ninsert into _pattern (id) values ({f_id});"
for f_factor in old['factors']:
undo_sql += f"\ninsert into patterns (id, factor) values ({f_id}, {f_factor});"
redo_cs = g_update_prefix | { 'type': 'pattern' } | new
undo_cs = g_update_prefix | { 'type': 'pattern' } | old
return execute_command(name, redo_sql, undo_sql, redo_cs, undo_cs)