Guard mixing

This commit is contained in:
WQY\qiong
2023-03-22 21:10:36 +08:00
parent 26b7f089fd
commit 7a2a52b920
2 changed files with 19 additions and 1 deletions

View File

@@ -13,7 +13,9 @@ def get_mixing_schema(name: str) -> dict[str, dict[str, Any]]:
def get_mixing(name: str, tank: str) -> dict[str, Any]:
m = read(name, f"select * from mixing where tank = '{tank}'")
m = try_read(name, f"select * from mixing where tank = '{tank}'")
if m == None:
return {}
d = {}
d['tank'] = str(m['tank'])
d['model'] = str(m['model'])
@@ -61,6 +63,10 @@ def set_mixing_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def set_mixing(name: str, cs: ChangeSet) -> ChangeSet:
if 'tank' not in cs.operations[0]:
return ChangeSet()
if get_mixing(name, cs.operations[0]['tank']) == {}:
return ChangeSet()
return execute_command(name, set_mixing_cmd(name, cs))
@@ -77,6 +83,10 @@ def add_mixing_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def add_mixing(name: str, cs: ChangeSet) -> ChangeSet:
if 'tank' not in cs.operations[0]:
return ChangeSet()
if get_mixing(name, cs.operations[0]['tank']) != {}:
return ChangeSet()
return execute_command(name, add_mixing_cmd(name, cs))
@@ -93,6 +103,10 @@ def delete_mixing_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def delete_mixing(name: str, cs: ChangeSet) -> ChangeSet:
if 'tank' not in cs.operations[0]:
return ChangeSet()
if get_mixing(name, cs.operations[0]['tank']) == {}:
return ChangeSet()
return execute_command(name, delete_mixing_cmd(name, cs))

View File

@@ -3036,6 +3036,8 @@ class TestApi:
add_tank(p, ChangeSet({'id': 't0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0, 'init_level': 1.0, 'min_level': 0.0, 'max_level': 2.0, 'diameter': 10.0, 'min_vol': 100.0, 'vol_curve': None, 'overflow': OVERFLOW_NO}))
assert get_mixing(p, 't0') == {}
add_mixing(p, ChangeSet({'tank': 't0', 'model': MIXING_MODEL_MIXED, 'value': 10.0}))
m = get_mixing(p,'t0')
assert m['tank'] == 't0'
@@ -3056,6 +3058,8 @@ class TestApi:
delete_mixing(p, ChangeSet({'tank': 't0'}))
assert get_mixing(p, 't0') == {}
self.leave(p)