From e68154ea0ba34eeda2f18730da64a10c16b1b4d9 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 20:47:16 +0800 Subject: [PATCH 01/12] 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})) From 3afb24c816e1162ce415e21745dea9503a0e2be8 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 20:52:47 +0800 Subject: [PATCH 02/12] Guard reservoir --- api/s3_reservoirs.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/s3_reservoirs.py b/api/s3_reservoirs.py index caf8a53..d3a3d7f 100644 --- a/api/s3_reservoirs.py +++ b/api/s3_reservoirs.py @@ -13,7 +13,9 @@ def get_reservoir_schema(name: str) -> dict[str, dict[str, Any]]: def get_reservoir(name: str, id: str) -> dict[str, Any]: - r = read(name, f"select * from reservoirs where id = '{id}'") + r = try_read(name, f"select * from reservoirs where id = '{id}'") + if r == None: + return {} xy = get_node_coord(name, id) d = {} d['id'] = str(r['id']) @@ -71,6 +73,10 @@ def set_reservoir_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_reservoir(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_reservoir(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, set_reservoir_cmd(name, cs)) @@ -92,6 +98,10 @@ def add_reservoir_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def add_reservoir(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_reservoir(name, cs.operations[0]['id']) != {}: + return ChangeSet() return execute_command(name, add_reservoir_cmd(name, cs)) @@ -113,6 +123,10 @@ def delete_reservoir_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def delete_reservoir(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_reservoir(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, delete_reservoir_cmd(name, cs)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 1eaf3e1..41ab604 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -459,6 +459,8 @@ class TestApi: p = 'test_reservoir' self.enter(p) + assert get_reservoir(p, 'r0') == {} + add_reservoir(p, ChangeSet({'id': 'r0', 'x': 0.0, 'y': 10.0, 'head': 20.0})) r0 = get_reservoir(p, 'r0') assert r0['x'] == 0.0 @@ -495,6 +497,8 @@ class TestApi: nodes = get_nodes(p) assert len(nodes) == 0 + assert get_reservoir(p, 'r0') == {} + add_reservoir(p, ChangeSet({'id': 'r1', 'x': 0.0, 'y': 10.0, 'head': 20.0})) add_reservoir(p, ChangeSet({'id': 'r2', 'x': 0.0, 'y': 10.0, 'head': 20.0})) add_reservoir(p, ChangeSet({'id': 'r3', 'x': 0.0, 'y': 10.0, 'head': 20.0})) From 236fcf061246f4f1fb3d8ab24071fdd39a5cbb3b Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 20:55:15 +0800 Subject: [PATCH 03/12] Guard tank --- api/s4_tanks.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/s4_tanks.py b/api/s4_tanks.py index 688e28a..45341fe 100644 --- a/api/s4_tanks.py +++ b/api/s4_tanks.py @@ -23,7 +23,9 @@ def get_tank_schema(name: str) -> dict[str, dict[str, Any]]: def get_tank(name: str, id: str) -> dict[str, Any]: - t = read(name, f"select * from tanks where id = '{id}'") + t = try_read(name, f"select * from tanks where id = '{id}'") + if t == None: + return {} xy = get_node_coord(name, id) d = {} d['id'] = str(t['id']) @@ -99,6 +101,10 @@ def set_tank_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_tank(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_tank(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, set_tank_cmd(name, cs)) @@ -120,6 +126,10 @@ def add_tank_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def add_tank(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_tank(name, cs.operations[0]['id']) != {}: + return ChangeSet() return execute_command(name, add_tank_cmd(name, cs)) @@ -141,6 +151,10 @@ def delete_tank_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def delete_tank(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_tank(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, delete_tank_cmd(name, cs)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 41ab604..af59d46 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -646,6 +646,8 @@ class TestApi: p = 'test_tank' self.enter(p) + assert get_tank(p, 't0') == {} + 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})) t0 = get_tank(p, 't0') assert t0['x'] == 0.0 @@ -706,6 +708,8 @@ class TestApi: nodes = get_nodes(p) assert len(nodes) == 0 + assert get_tank(p, 't0') == {} + add_tank(p, ChangeSet({'id': 't1', '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})) add_tank(p, ChangeSet({'id': 't2', '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})) add_tank(p, ChangeSet({'id': 't3', '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})) From 6a304ba7e0162c3b4439f1dce9077650d3b1ad35 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 20:58:45 +0800 Subject: [PATCH 04/12] Guard pipe --- api/s5_pipes.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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) From da796f0c7f21a28472818bdd3b2a1b305e85b60f Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:00:58 +0800 Subject: [PATCH 05/12] Guard pump --- api/s6_pumps.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/s6_pumps.py b/api/s6_pumps.py index 1fb06da..1f73cfb 100644 --- a/api/s6_pumps.py +++ b/api/s6_pumps.py @@ -13,7 +13,9 @@ def get_pump_schema(name: str) -> dict[str, dict[str, Any]]: def get_pump(name: str, id: str) -> dict[str, Any]: - p = read(name, f"select * from pumps where id = '{id}'") + p = try_read(name, f"select * from pumps where id = '{id}'") + if p == None: + return {} d = {} d['id'] = str(p['id']) d['node1'] = str(p['node1']) @@ -73,6 +75,10 @@ def set_pump_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_pump(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_pump(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, set_pump_cmd(name, cs)) @@ -92,6 +98,10 @@ def add_pump_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def add_pump(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_pump(name, cs.operations[0]['id']) != {}: + return ChangeSet() return execute_command(name, add_pump_cmd(name, cs)) @@ -111,6 +121,10 @@ def delete_pump_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def delete_pump(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_pump(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, delete_pump_cmd(name, cs)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index ad482a7..7f0b9d4 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1156,6 +1156,8 @@ class TestApi: assert is_junction(p, 'j3') assert is_junction(p, 'j4') + assert get_pump(p, 'p0') == {} + add_pump(p, ChangeSet({'id': 'p0', 'node1': 'j1', 'node2': 'j2', 'power': 0.0})) p0 = get_pump(p, 'p0') assert p0['node1'] == 'j1' @@ -1191,6 +1193,8 @@ class TestApi: links = get_links(p) assert len(links) == 0 + assert get_pump(p, 'p0') == {} + self.leave(p) From 39e231663148b96e4c1533b144458fe9f58c9fe2 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:02:56 +0800 Subject: [PATCH 06/12] Guard valve --- api/s7_valves.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/s7_valves.py b/api/s7_valves.py index 5ab210b..b63955e 100644 --- a/api/s7_valves.py +++ b/api/s7_valves.py @@ -21,7 +21,9 @@ def get_valve_schema(name: str) -> dict[str, dict[str, Any]]: def get_valve(name: str, id: str) -> dict[str, Any]: - p = read(name, f"select * from valves where id = '{id}'") + p = try_read(name, f"select * from valves where id = '{id}'") + if p == None: + return {} d = {} d['id'] = str(p['id']) d['node1'] = str(p['node1']) @@ -81,6 +83,10 @@ def set_valve_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_valve(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_valve(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, set_valve_cmd(name, cs)) @@ -100,6 +106,10 @@ def add_valve_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def add_valve(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_valve(name, cs.operations[0]['id']) != {}: + return ChangeSet() return execute_command(name, add_valve_cmd(name, cs)) @@ -119,6 +129,10 @@ def delete_valve_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def delete_valve(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_valve(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, delete_valve_cmd(name, cs)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 7f0b9d4..c95e66c 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1367,6 +1367,8 @@ class TestApi: assert is_junction(p, 'j3') assert is_junction(p, 'j4') + assert get_valve(p, 'v0') == {} + add_valve(p, ChangeSet({'id': 'v0', 'node1': 'j1', 'node2': 'j2', 'diameter': 10.0, 'v_type': VALVES_TYPE_FCV, 'setting': '0.1', 'minor_loss': 0.5 })) v0 = get_valve(p, 'v0') assert v0['node1'] == 'j1' @@ -1414,6 +1416,8 @@ class TestApi: links = get_links(p) assert len(links) == 0 + assert get_valve(p, 'v0') == {} + self.leave(p) From 26b7f089fde0e1f78b0dd8c32df72218f04633f0 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:06:14 +0800 Subject: [PATCH 07/12] Guard tag --- api/s8_tags.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/s8_tags.py b/api/s8_tags.py index 8ae4a5d..18fc1cd 100644 --- a/api/s8_tags.py +++ b/api/s8_tags.py @@ -76,6 +76,8 @@ def set_tag_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_tag(name: str, cs: ChangeSet) -> ChangeSet: + if 't_type' not in cs.operations[0] or 'id' not in cs.operations[0] or 'tag' not in cs.operations[0]: + return ChangeSet() return execute_command(name, set_tag_cmd(name, cs)) From 7a2a52b9201bfd5289cc211ada2c6dce297c13a4 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:10:36 +0800 Subject: [PATCH 08/12] Guard mixing --- api/s20_mixing.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/s20_mixing.py b/api/s20_mixing.py index 1a43465..1b8917c 100644 --- a/api/s20_mixing.py +++ b/api/s20_mixing.py @@ -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)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index c95e66c..ba39f98 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -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) From 4c4a03fcb058ee6a996ce6ca867f4a4653d3d821 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:16:50 +0800 Subject: [PATCH 09/12] Guard pattern and curve --- api/s11_patterns.py | 15 +++++++++++++++ api/s12_curves.py | 16 +++++++++++++++- test_tjnetwork.py | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/api/s11_patterns.py b/api/s11_patterns.py index 44bee01..236720c 100644 --- a/api/s11_patterns.py +++ b/api/s11_patterns.py @@ -11,6 +11,9 @@ def get_pattern_schema(name: str) -> dict[str, dict[str, Any]]: def get_pattern(name: str, id: str) -> dict[str, Any]: + p_one = try_read(name, f"select * from _pattern where id = '{id}'") + if p_one == None: + return {} pas = read_all(name, f"select * from patterns where id = '{id}' order by _order") ps = [] for r in pas: @@ -46,6 +49,10 @@ def set_pattern_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_pattern(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_pattern(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, set_pattern_cmd(name, cs)) @@ -70,6 +77,10 @@ def add_pattern_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def add_pattern(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_pattern(name, cs.operations[0]['id']) != {}: + return ChangeSet() return execute_command(name, add_pattern_cmd(name, cs)) @@ -94,6 +105,10 @@ def delete_pattern_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def delete_pattern(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_pattern(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, delete_pattern_cmd(name, cs)) diff --git a/api/s12_curves.py b/api/s12_curves.py index 34780b0..a615564 100644 --- a/api/s12_curves.py +++ b/api/s12_curves.py @@ -16,7 +16,9 @@ def get_curve_schema(name: str) -> dict[str, dict[str, Any]]: def get_curve(name: str, id: str) -> dict[str, Any]: - c_one = read(name, f"select * from _curve where id = '{id}'") + c_one = try_read(name, f"select * from _curve where id = '{id}'") + if c_one == None: + return {} cus = read_all(name, f"select * from curves where id = '{id}' order by _order") cs = [] for r in cus: @@ -66,6 +68,10 @@ def set_curve_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def set_curve(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_curve(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, set_curve_cmd(name, cs)) @@ -94,6 +100,10 @@ def add_curve_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def add_curve(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_curve(name, cs.operations[0]['id']) != {}: + return ChangeSet() return execute_command(name, add_curve_cmd(name, cs)) @@ -120,6 +130,10 @@ def delete_curve_cmd(name: str, cs: ChangeSet) -> DbChangeSet: def delete_curve(name: str, cs: ChangeSet) -> ChangeSet: + if 'id' not in cs.operations[0]: + return ChangeSet() + if get_curve(name, cs.operations[0]['id']) == {}: + return ChangeSet() return execute_command(name, delete_curve_cmd(name, cs)) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index ba39f98..74f6063 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -1850,6 +1850,8 @@ class TestApi: assert is_pattern(p, 'p0') == False + assert get_pattern(p, 'p0') == {} + add_pattern(p, ChangeSet({'id' : 'p0', 'factors': [1.0, 2.0, 3.0]})) assert is_pattern(p, 'p0') @@ -1874,6 +1876,8 @@ class TestApi: delete_pattern(p, ChangeSet({'id' : 'p0'})) assert is_pattern(p, 'p0') == False + assert get_pattern(p, 'p0') == {} + self.leave(p) From 19a5b860b6d88dd6f1b708af38df3fc8cd209357 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:17:46 +0800 Subject: [PATCH 10/12] Add curve test --- test_tjnetwork.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 74f6063..270eedf 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -2011,6 +2011,8 @@ class TestApi: assert is_curve(p, 'c0') == False + assert get_curve(p, 'c0') == {} + add_curve(p, ChangeSet({'id' : 'c0', 'c_type' : CURVE_TYPE_PUMP, 'coords': [{'x': 1.0, 'y': 2.0}, {'x': 2.0, 'y': 1.0}]})) assert is_curve(p, 'c0') @@ -2047,6 +2049,8 @@ class TestApi: assert is_curve(p, 'c0') == False + assert get_curve(p, 'c0') == {} + self.leave(p) From c6d74e3cad8a7e264f42706f836dc55c8af37d4c Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:19:37 +0800 Subject: [PATCH 11/12] Guard coord --- api/s24_coordinates.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/s24_coordinates.py b/api/s24_coordinates.py index 0df02db..ba5dd73 100644 --- a/api/s24_coordinates.py +++ b/api/s24_coordinates.py @@ -7,7 +7,9 @@ def _to_client_point(coord: str) -> dict[str, float]: def get_node_coord(name: str, id: str) -> dict[str, float]: - row = read(name, f"select * from coordinates where node = '{id}'") + row = try_read(name, f"select * from coordinates where node = '{id}'") + if row == None: + return {} return _to_client_point(row['coord']) From bcdaddc5cf5824889dfce37141578780bb1792d2 Mon Sep 17 00:00:00 2001 From: "WQY\\qiong" Date: Wed, 22 Mar 2023 21:55:58 +0800 Subject: [PATCH 12/12] Add complex test case --- test_tjnetwork.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/test_tjnetwork.py b/test_tjnetwork.py index 270eedf..99d36ef 100644 --- a/test_tjnetwork.py +++ b/test_tjnetwork.py @@ -185,6 +185,63 @@ class TestApi: self.leave(p) + # complex test + + + def test_delete_node_link_then_restore(self): + p = 'test_remove_node_link_then_restore' + read_inp(p, f'./inp/net3.inp', '2') + + open_project(p) + + nodes = [] + links = [] + + nodes.append('131') + links += get_node_links(p, nodes[-1]) + delete_junction(p, ChangeSet({'id': '131'})) + + links.append('137') + delete_pipe(p, ChangeSet({'id': '137'})) + + nodes.append('129') + links += get_node_links(p, nodes[-1]) + delete_junction(p, ChangeSet({'id': '129'})) + + nodes.append('127') + links += get_node_links(p, nodes[-1]) + delete_junction(p, ChangeSet({'id': '127'})) + + links.append('135') + delete_pipe(p, ChangeSet({'id': '135'})) + + links.append('135') + delete_pipe(p, ChangeSet({'id': '133'})) + + nodes.append('20') + links += get_node_links(p, nodes[-1]) + delete_junction(p, ChangeSet({'id': '20'})) + + nodes.append('3') + links += get_node_links(p, nodes[-1]) + delete_tank(p, ChangeSet({'id': '3'})) + + for node in nodes: + assert is_node(p, node) == False + for link in links: + assert is_link(p, link) == False + + op = get_restore_operation(p) + pick_operation(p, op) + + for node in nodes: + assert is_node(p, node) + for link in links: + assert is_link(p, link) + + self.leave(p) + + # 1 title