Add label api and test
This commit is contained in:
@@ -71,4 +71,6 @@ from .s23_options import get_option_schema, get_option, set_option
|
||||
|
||||
from .s24_coordinates import get_node_coord
|
||||
|
||||
from .s26_labels import get_label_schema, get_label, set_label, add_label, delete_label
|
||||
|
||||
from .s27_backdrop import get_backdrop_schema, get_backdrop, set_backdrop
|
||||
|
||||
99
api/s26_labels.py
Normal file
99
api/s26_labels.py
Normal file
@@ -0,0 +1,99 @@
|
||||
from .operation import *
|
||||
|
||||
|
||||
def get_label_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return { 'x' : {'type': 'float' , 'optional': False , 'readonly': False},
|
||||
'y' : {'type': 'float' , 'optional': False , 'readonly': False},
|
||||
'label' : {'type': 'str' , 'optional': False , 'readonly': False},
|
||||
'node' : {'type': 'str' , 'optional': True , 'readonly': False} }
|
||||
|
||||
|
||||
def get_label(name: str, x: float, y: float) -> dict[str, Any]:
|
||||
d = {}
|
||||
d['x'] = x
|
||||
d['y'] = y
|
||||
l = try_read(name, f'select * from labels where x = {x} and y = {y}')
|
||||
if l == None:
|
||||
d['label'] = None
|
||||
d['node'] = None
|
||||
else:
|
||||
d['label'] = str(l['label'])
|
||||
d['node'] = str(l['node']) if l['node'] != None else None
|
||||
return d
|
||||
|
||||
|
||||
class Label(object):
|
||||
def __init__(self, input: dict[str, Any]) -> None:
|
||||
self.type = 'label'
|
||||
self.x = float(input['x'])
|
||||
self.y = float(input['y'])
|
||||
self.label = str(input['label'])
|
||||
self.node = str(input['node']) if 'node' in input and input['node'] != None else None
|
||||
|
||||
self.f_type = f"'{self.type}'"
|
||||
self.f_x = self.x
|
||||
self.f_y = self.y
|
||||
self.f_label = f"'{self.label}'"
|
||||
self.f_node = f"'{self.node}'" if self.node != None else 'null'
|
||||
|
||||
def as_dict(self) -> dict[str, Any]:
|
||||
return { 'type': self.type, 'x': self.x, 'y': self.y, 'label': self.label, 'node': self.node }
|
||||
|
||||
def as_xy_dict(self) -> dict[str, Any]:
|
||||
return { 'type': self.type, 'x': self.x, 'y': self.y }
|
||||
|
||||
|
||||
def set_label_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Label(get_label(name, cs.operations[0]['x'], cs.operations[0]['y']))
|
||||
raw_new = get_label(name, cs.operations[0]['x'], cs.operations[0]['y'])
|
||||
|
||||
new_dict = cs.operations[0]
|
||||
schema = get_label_schema(name)
|
||||
for key, value in schema.items():
|
||||
if key in new_dict and not value['readonly']:
|
||||
raw_new[key] = new_dict[key]
|
||||
new = Label(raw_new)
|
||||
|
||||
redo_sql = f"update labels set label = {new.f_label}, node = {new.f_node} where x = {new.f_x} and y = {new.f_y};"
|
||||
undo_sql = f"update labels set label = {old.f_label}, node = {old.f_node} where x = {old.f_x} and y = {old.f_y};"
|
||||
|
||||
redo_cs = g_update_prefix | new.as_dict()
|
||||
undo_cs = g_update_prefix | old.as_dict()
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def set_label(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, set_label_cache(name, cs))
|
||||
|
||||
|
||||
def add_label_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
new = Label(cs.operations[0])
|
||||
|
||||
redo_sql = f"insert into labels (x, y, label, node) values ({new.f_x}, {new.f_y}, {new.f_label}, {new.f_node});"
|
||||
undo_sql = f"delete from labels where x = {new.f_x} and y = {new.f_y};"
|
||||
|
||||
redo_cs = g_add_prefix | new.as_dict()
|
||||
undo_cs = g_delete_prefix | new.as_xy_dict()
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def add_label(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, add_label_cache(name, cs))
|
||||
|
||||
|
||||
def delete_label_cache(name: str, cs: ChangeSet) -> SqlChangeSet:
|
||||
old = Label(get_label(name, cs.operations[0]['x'], cs.operations[0]['y']))
|
||||
|
||||
redo_sql = f"delete from labels where x = {old.f_x} and y = {old.f_y};"
|
||||
undo_sql = f"insert into labels (x, y, label, node) values ({old.f_x}, {old.f_y}, {old.f_label}, {old.f_node});"
|
||||
|
||||
redo_cs = g_delete_prefix | old.as_xy_dict()
|
||||
undo_cs = g_add_prefix | old.as_dict()
|
||||
|
||||
return SqlChangeSet(redo_sql, undo_sql, redo_cs, undo_cs)
|
||||
|
||||
|
||||
def delete_label(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return execute_command(name, delete_label_cache(name, cs))
|
||||
@@ -5,5 +5,6 @@ create table labels
|
||||
x numeric not null
|
||||
, y numeric not null
|
||||
, label text not null
|
||||
, anchornode varchar(32) references _node(id)
|
||||
, node varchar(32) references _node(id)
|
||||
, primary key (x, y)
|
||||
);
|
||||
|
||||
@@ -2488,6 +2488,121 @@ class TestApi:
|
||||
self.leave(p)
|
||||
|
||||
|
||||
# 26 label
|
||||
|
||||
|
||||
def test_label(self):
|
||||
p = 'test_label'
|
||||
self.enter(p)
|
||||
|
||||
l = get_label(p, 0.0, 0.0)
|
||||
assert l['x'] == 0.0
|
||||
assert l['y'] == 0.0
|
||||
assert l['label'] == None
|
||||
assert l['node'] == None
|
||||
|
||||
add_junction(p, ChangeSet({'id': 'j0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
add_label(p, ChangeSet({'x': 0.0, 'y': 0.0, 'label': 'x', 'node': 'j0'}))
|
||||
l = get_label(p, 0.0, 0.0)
|
||||
assert l['x'] == 0.0
|
||||
assert l['y'] == 0.0
|
||||
assert l['label'] == 'x'
|
||||
assert l['node'] == 'j0'
|
||||
|
||||
add_junction(p, ChangeSet({'id': 'j1', 'x': 1.0, 'y': 20.0, 'elevation': 20.0}))
|
||||
set_label(p, ChangeSet({'x': 0.0, 'y': 0.0, 'label': 'xxx', 'node': 'j1'}))
|
||||
l = get_label(p, 0.0, 0.0)
|
||||
assert l['x'] == 0.0
|
||||
assert l['y'] == 0.0
|
||||
assert l['label'] == 'xxx'
|
||||
assert l['node'] == 'j1'
|
||||
|
||||
delete_label(p, ChangeSet({'x': 0.0, 'y': 0.0}))
|
||||
l = get_label(p, 0.0, 0.0)
|
||||
assert l['x'] == 0.0
|
||||
assert l['y'] == 0.0
|
||||
assert l['label'] == None
|
||||
assert l['node'] == None
|
||||
|
||||
self.leave(p)
|
||||
|
||||
|
||||
def test_label_op(self):
|
||||
p = 'test_label_op'
|
||||
self.enter(p)
|
||||
|
||||
add_junction(p, ChangeSet({'id': 'j0', 'x': 0.0, 'y': 10.0, 'elevation': 20.0}))
|
||||
add_junction(p, ChangeSet({'id': 'j1', 'x': 1.0, 'y': 20.0, 'elevation': 20.0}))
|
||||
|
||||
cs = add_label(p, ChangeSet({'x': 0.0, 'y': 0.0, 'label': 'x', 'node': 'j0'})).operations[0]
|
||||
assert cs['operation'] == API_ADD
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
assert cs['label'] == 'x'
|
||||
assert cs['node'] == 'j0'
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_DELETE
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_ADD
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
assert cs['label'] == 'x'
|
||||
assert cs['node'] == 'j0'
|
||||
|
||||
cs = set_label(p, ChangeSet({'x': 0.0, 'y': 0.0, 'label': 'xxx', 'node': 'j1'})).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
assert cs['label'] == 'xxx'
|
||||
assert cs['node'] == 'j1'
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
assert cs['label'] == 'x'
|
||||
assert cs['node'] == 'j0'
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_UPDATE
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
assert cs['label'] == 'xxx'
|
||||
assert cs['node'] == 'j1'
|
||||
|
||||
cs = delete_label(p, ChangeSet({'x': 0.0, 'y': 0.0})).operations[0]
|
||||
assert cs['operation'] == API_DELETE
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
|
||||
cs = execute_undo(p).operations[0]
|
||||
assert cs['operation'] == API_ADD
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
assert cs['label'] == 'xxx'
|
||||
assert cs['node'] == 'j1'
|
||||
|
||||
cs = execute_redo(p).operations[0]
|
||||
assert cs['operation'] == API_DELETE
|
||||
assert cs['type'] == 'label'
|
||||
assert cs['x'] == 0.0
|
||||
assert cs['y'] == 0.0
|
||||
|
||||
self.leave(p)
|
||||
|
||||
|
||||
# 27 backdrop
|
||||
|
||||
|
||||
@@ -2525,7 +2640,5 @@ class TestApi:
|
||||
self.leave(p)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pytest.main()
|
||||
|
||||
20
tjnetwork.py
20
tjnetwork.py
@@ -532,6 +532,26 @@ def get_node_coord(name: str, node_id: str) -> dict[str, float] | None:
|
||||
return api.get_node_coord(name, node_id)
|
||||
|
||||
|
||||
############################################################
|
||||
# backdrop 27.[BACKDROP]
|
||||
############################################################
|
||||
|
||||
def get_label_schema(name: str) -> dict[str, dict[str, Any]]:
|
||||
return api.get_label_schema(name)
|
||||
|
||||
def get_label(name: str, x: float, y: float) -> dict[str, Any]:
|
||||
return api.get_label(name, x, y)
|
||||
|
||||
def set_label(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return api.set_label(name, cs)
|
||||
|
||||
def add_label(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return api.add_label(name, cs)
|
||||
|
||||
def delete_label(name: str, cs: ChangeSet) -> ChangeSet:
|
||||
return api.delete_label(name, cs)
|
||||
|
||||
|
||||
############################################################
|
||||
# backdrop 27.[BACKDROP]
|
||||
############################################################
|
||||
|
||||
Reference in New Issue
Block a user