Guard pipe

This commit is contained in:
WQY\qiong
2023-03-22 20:58:45 +08:00
parent 236fcf0612
commit 6a304ba7e0
2 changed files with 19 additions and 1 deletions

View File

@@ -19,7 +19,9 @@ def get_pipe_schema(name: str) -> dict[str, dict[str, Any]]:
def get_pipe(name: str, id: str) -> dict[str, Any]:
p = read(name, f"select * from pipes where id = '{id}'")
p = try_read(name, f"select * from pipes where id = '{id}'")
if p == None:
return {}
d = {}
d['id'] = str(p['id'])
d['node1'] = str(p['node1'])
@@ -82,6 +84,10 @@ def set_pipe_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def set_pipe(name: str, cs: ChangeSet) -> ChangeSet:
if 'id' not in cs.operations[0]:
return ChangeSet()
if get_pipe(name, cs.operations[0]['id']) == {}:
return ChangeSet()
return execute_command(name, set_pipe_cmd(name, cs))
@@ -101,6 +107,10 @@ def add_pipe_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def add_pipe(name: str, cs: ChangeSet) -> ChangeSet:
if 'id' not in cs.operations[0]:
return ChangeSet()
if get_pipe(name, cs.operations[0]['id']) != {}:
return ChangeSet()
return execute_command(name, add_pipe_cmd(name, cs))
@@ -120,6 +130,10 @@ def delete_pipe_cmd(name: str, cs: ChangeSet) -> DbChangeSet:
def delete_pipe(name: str, cs: ChangeSet) -> ChangeSet:
if 'id' not in cs.operations[0]:
return ChangeSet()
if get_pipe(name, cs.operations[0]['id']) == {}:
return ChangeSet()
return execute_command(name, delete_pipe_cmd(name, cs))

View File

@@ -907,6 +907,8 @@ class TestApi:
assert is_junction(p, 'j3')
assert is_junction(p, 'j4')
assert get_pipe(p, 'p0') == {}
add_pipe(p, ChangeSet({'id': 'p0', 'node1': 'j1', 'node2': 'j2', 'length': 100.0, 'diameter': 10.0, 'roughness': 0.1, 'minor_loss': 0.5, 'status': PIPE_STATUS_OPEN }))
p0 = get_pipe(p, 'p0')
assert p0['node1'] == 'j1'
@@ -959,6 +961,8 @@ class TestApi:
links = get_links(p)
assert len(links) == 0
assert get_pipe(p, 'p0') == {}
self.leave(p)