Skip to content

Commit 2491982

Browse files
committed
Fix caption logic
1 parent 237c267 commit 2491982

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

python-wrapper/src/neo4j_viz/neo4j.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def _map_node(node: neo4j.graph.Node, size_property: Optional[str], caption_prop
7373
else:
7474
caption = next(iter(labels))
7575
else:
76-
caption = node.get(caption)
76+
caption = str(node.get(caption_property))
7777

7878
return Node(id=node.element_id, caption=caption, labels=labels, size=size, **{k: v for k, v in node.items()})
7979

@@ -86,7 +86,7 @@ def _map_relationship(rel: neo4j.graph.Relationship, caption_property: Optional[
8686
if caption_property == "type":
8787
caption = rel.type
8888
else:
89-
caption = rel.get(caption)
89+
caption = str(rel.get(caption_property))
9090
else:
9191
caption = None
9292

python-wrapper/tests/test_neo4j.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
@pytest.fixture(scope="class", autouse=True)
1111
def graph_setup(neo4j_session: Session) -> Generator[None, None, None]:
12-
neo4j_session.run("CREATE (a:_CI_A {name:'Alice'})-[:KNOWS]->(b:_CI_A:_CI_B {name:'Bob'}), (b)-[:RELATED]->(a)")
12+
neo4j_session.run(
13+
"CREATE (a:_CI_A {name:'Alice', height:20})-[:KNOWS {year: 2025}]->(b:_CI_A:_CI_B {name:'Bob', height:10}), (b)-[:RELATED {year: 2015}]->(a)"
14+
)
1315
yield
1416
neo4j_session.run("MATCH (n:_CI_A|_CI_B) DETACH DELETE n")
1517

@@ -23,8 +25,8 @@ def test_from_neo4j_graph(neo4j_session: Session) -> None:
2325
node_ids: list[str] = [node.element_id for node in graph.nodes]
2426

2527
expected_nodes = [
26-
Node(id=node_ids[0], caption="_CI_A", labels=["_CI_A"], name="Alice"),
27-
Node(id=node_ids[1], caption="_CI_A:_CI_B", labels=["_CI_A", "_CI_B"], name="Bob"),
28+
Node(id=node_ids[0], caption="_CI_A", labels=["_CI_A"], name="Alice", height=20),
29+
Node(id=node_ids[1], caption="_CI_A:_CI_B", labels=["_CI_A", "_CI_B"], name="Bob", height=10),
2830
]
2931

3032
assert len(VG.nodes) == 2
@@ -36,3 +38,27 @@ def test_from_neo4j_graph(neo4j_session: Session) -> None:
3638
(node_ids[0], node_ids[1], "KNOWS"),
3739
(node_ids[1], node_ids[0], "RELATED"),
3840
]
41+
42+
43+
@pytest.mark.requires_neo4j_and_gds
44+
def test_from_neo4j_graph_full(neo4j_session: Session) -> None:
45+
graph = neo4j_session.run("MATCH (a:_CI_A|_CI_B)-[r]->(b) RETURN a, b, r ORDER BY a").graph()
46+
47+
VG = from_neo4j(graph, node_caption="name", relationship_caption="year", size_property="height")
48+
49+
node_ids: list[str] = [node.element_id for node in graph.nodes]
50+
51+
expected_nodes = [
52+
Node(id=node_ids[0], caption="Alice", labels=["_CI_A"], name="Alice", height=20, size=60.0),
53+
Node(id=node_ids[1], caption="Bob", labels=["_CI_A", "_CI_B"], name="Bob", height=10, size=3.0),
54+
]
55+
56+
assert len(VG.nodes) == 2
57+
assert sorted(VG.nodes, key=lambda x: x.name) == expected_nodes # type: ignore[attr-defined]
58+
59+
assert len(VG.relationships) == 2
60+
vg_rels = sorted([(e.source, e.target, e.caption) for e in VG.relationships], key=lambda x: x[0])
61+
assert vg_rels == [
62+
(node_ids[0], node_ids[1], "2025"),
63+
(node_ids[1], node_ids[0], "2015"),
64+
]

0 commit comments

Comments
 (0)