Update reservoir api and test

This commit is contained in:
wqy
2022-09-24 23:51:00 +08:00
parent baeef6c859
commit 277f5a3501
5 changed files with 134 additions and 125 deletions

View File

@@ -16,7 +16,7 @@ schema: dict[str, dict[str, Any]] = { \
'links' : define_property(str_list_type, False, True)}
def get_junction_schema(name: str) -> dict[str, str]:
def get_junction_schema(name: str) -> dict[str, dict[str, Any]]:
return schema
@@ -47,6 +47,7 @@ def get_junction(name: str, id: str) -> dict[str, Any] | None:
return None
ps: dict[str, str] = {}
ps['id'] = id
ps['elevation'] = float(row['elevation'])
ps['demand'] = float(row['demand']) if row['demand'] != None else None
ps['pattern'] = row['pattern']
@@ -55,26 +56,6 @@ def get_junction(name: str, id: str) -> dict[str, Any] | None:
return ps
def delete_junction(name: str, id: str) -> ChangeSet:
row = get_junction(name, id)
if row == None:
return ChangeSet()
old = Serialize(get_junction(name, id), schema).to_storage()
sql = f"delete from coordinates where node = '{id}';"
sql += f"\ndelete from junctions where id = '{id}';"
sql += f"\ndelete from _node where id = '{id}';"
undo = f"insert into _node (id, type) values (''{id}'', ''{JUNCTION}'');"
undo += f"\ninsert into junctions (id, elevation, demand, pattern) values (''{id}'', {old['elevation']}, {old['demand']}, {old['pattern']});"
undo += f"\ninsert into coordinates (node, coord) values (''{id}'', {old['coord']});"
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'delete_junction', API_DELETE, JUNCTION, id)
return get_current_change_set(name)
def set_junction(name: str, id: str, properties: dict[str, Any]) -> ChangeSet:
if not is_junction(name, id):
return ChangeSet()
@@ -99,3 +80,23 @@ def set_junction(name: str, id: str, properties: dict[str, Any]) -> ChangeSet:
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'set_junction', API_UPDATE, JUNCTION, id, ps)
return get_current_change_set(name)
def delete_junction(name: str, id: str) -> ChangeSet:
row = get_junction(name, id)
if row == None:
return ChangeSet()
old = Serialize(get_junction(name, id), schema).to_storage()
sql = f"delete from coordinates where node = '{id}';"
sql += f"\ndelete from junctions where id = '{id}';"
sql += f"\ndelete from _node where id = '{id}';"
undo = f"insert into _node (id, type) values (''{id}'', ''{JUNCTION}'');"
undo += f"\ninsert into junctions (id, elevation, demand, pattern) values (''{id}'', {old['elevation']}, {old['demand']}, {old['pattern']});"
undo += f"\ninsert into coordinates (node, coord) values (''{id}'', {old['coord']});"
write(name, sql)
add_operation(name, sql.replace("'", "''"), undo, 'delete_junction', API_DELETE, JUNCTION, id)
return get_current_change_set(name)