diff --git a/api/s5_pipes.py b/api/s5_pipes.py index e9a40a5..4c6c089 100644 --- a/api/s5_pipes.py +++ b/api/s5_pipes.py @@ -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)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index af59d46..ad482a7 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -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)