Skip to content

Commit

Permalink
Make geo node optional during json import and add test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
arneboockmeyer committed Jun 24, 2024
1 parent a2a2dd6 commit 3be2ffd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
19 changes: 18 additions & 1 deletion test/topology_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from yaramo.model import Edge, Node, Topology


def test_get_edge_by_nodes():
topology = Topology()

Expand All @@ -23,3 +22,21 @@ def _test_edge(_node_a, _node_b, _expected_edge):
_test_edge(node_a, node_b, edge)
_test_edge(node_b, node_a, edge)
_test_edge(node_a, node_c, None)

def test_json_export_and_import():
topology = Topology()

node_a = Node()
node_b = Node()
topology.add_node(node_a)
topology.add_node(node_b)
edge = Edge(node_a, node_b)
topology.add_edge(edge)

json_str = topology.to_json()
topology_copy = Topology.from_json(json_str)

assert len(topology.nodes) == len(topology_copy.nodes)
assert len(topology.edges) == len(topology_copy.edges)
assert len(topology.signals) == len(topology_copy.signals)
assert len(topology.routes) == len(topology_copy.routes)
19 changes: 9 additions & 10 deletions yaramo/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,19 @@ def from_json(cls, json_str: str):
obj = json.loads(json_str)
topology = cls()
for node in obj["nodes"]:
geo_node = obj["objects"][node["geo_node"]]
topology.add_node(
Node(
**{
**node,
"geo_node": Wgs84GeoNode(
node_obj = Node(**node)
topology.add_node(node_obj)
if "geo_node" in node and node["geo_node"] is not None:
geo_node = obj["objects"][node["geo_node"]]
geo_node_obj = Wgs84GeoNode(
obj["objects"][geo_node["geo_point"]]["x"],
obj["objects"][geo_node["geo_point"]]["y"],
name=geo_node["name"],
uuid=geo_node["uuid"],
),
}
)
)
)
node_obj.geo_node = geo_node_obj


for signal in obj["signals"]:
topology.add_signal(Signal(**signal))
for edge in obj["edges"]:
Expand Down

0 comments on commit 3be2ffd

Please sign in to comment.