From e68154ea0ba34eeda2f18730da64a10c16b1b4d9 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 20:47:16 +0800 Subject: [PATCH] Guard junction --- api/s2_junctions.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/s2_junctions.py b/api/s2_junctions.py index 68e6a48..28aadd0 100644 --- a/api/s2_junctions.py +++ b/api/s2_junctions.py @@ -12,7 +12,9 @@ def get_junction_schema(name: str) -> dict[str, dict[str, Any]]: def get_junction(name: str, id: str) -> dict[str, Any]: - j = read(name, f"select * from junctions where id = '{id}'") + j = try_read(name, f"select * from junctions where id = '{id}'") + if j == None: + return {} xy = get_node_coord(name, id) d = {} d['id'] = str(j['id']) @@ -67,6 +69,10 @@ def set_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_junction(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_junction(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, set_junction_cmd(name, cs)) @@ -88,6 +94,10 @@ def add_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def add_junction(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_junction(name, cs.operations[0]['id']) != {}: + return ChangeSet() return execute_command(name, add_junction_cmd(name, cs)) @@ -109,6 +119,10 @@ def delete_junction_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def delete_junction(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_junction(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, delete_junction_cmd(name, cs)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 63dc91f..1eaf3e1 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -249,6 +249,8 @@ class TestApi: p = 'test_junction' self.enter(p) + assert get_junction(p, 'j0') == {} + add_junction(p, ChangeSet({'id': 'j0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0})) j0 = get_junction(p, 'j0') assert j0['x'] == 0.0 @@ -284,6 +286,8 @@ class TestApi: nodes = get_nodes(p) assert len(nodes) == 0 + assert get_junction(p, 'j0') == {} + add_junction(p, ChangeSet({'id': 'j1', 'x': 0.0, 'y': 10.0, 'elevation': 20.0})) add_junction(p, ChangeSet({'id': 'j2', 'x': 0.0, 'y': 10.0, 'elevation': 20.0})) add_junction(p, ChangeSet({'id': 'j3', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))