Skip to content

Commit 315c525

Browse files
committed
new: test_load_graph_from_nxadb_as_smart_graph
1 parent ed4df9b commit 315c525

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

tests/test.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import networkx as nx
55
import pytest
6-
from arango import DocumentDeleteError
6+
from arango.exceptions import DocumentInsertError
77
from phenolrs.networkx.typings import (
88
DiGraphAdjDict,
99
GraphAdjDict,
@@ -17,7 +17,7 @@
1717

1818
from .conftest import create_grid_graph, create_line_graph, db, run_gpu_tests
1919

20-
G_NX = nx.karate_club_graph()
20+
G_NX: nx.Graph = nx.karate_club_graph()
2121
G_NX_digraph = nx.DiGraph(G_NX)
2222
G_NX_multigraph = nx.MultiGraph(G_NX)
2323
G_NX_multidigraph = nx.MultiDiGraph(G_NX)
@@ -108,6 +108,73 @@ def test_load_graph_from_nxadb():
108108
db.delete_graph(graph_name, drop_collections=True)
109109

110110

111+
def test_load_graph_from_nxadb_as_smart_graph():
112+
graph_name = "SmartKarateGraph"
113+
114+
db.delete_graph(graph_name, drop_collections=True, ignore_missing=True)
115+
db.create_graph(
116+
graph_name,
117+
smart=True,
118+
smart_field="club",
119+
edge_definitions=[
120+
{
121+
"edge_collection": "smart_person_to_smart_person",
122+
"from_vertex_collections": ["smart_person"],
123+
"to_vertex_collections": ["smart_person"],
124+
}
125+
],
126+
)
127+
128+
G_NX_copy = G_NX.copy()
129+
for _, node in G_NX_copy.nodes(data=True):
130+
node["club"] = node["club"].replace(" ", "")
131+
132+
G = nxadb.Graph(
133+
name=graph_name,
134+
incoming_graph_data=G_NX_copy,
135+
write_async=False,
136+
)
137+
138+
assert db.has_graph(graph_name)
139+
assert db.has_collection("smart_person")
140+
assert db.has_collection("smart_person_to_smart_person")
141+
assert db.collection("smart_person").count() == len(G_NX_copy.nodes)
142+
assert db.collection("smart_person_to_smart_person").count() == len(G_NX_copy.edges)
143+
144+
assert db.has_document("smart_person/Mr.Hi:0")
145+
146+
with pytest.raises(DocumentInsertError):
147+
G.add_node(35, club="Officer")
148+
149+
with pytest.raises(DocumentInsertError):
150+
G.add_node("35", club="Officer")
151+
152+
with pytest.raises(DocumentInsertError):
153+
G.add_node("smart_person/35", club="Officer")
154+
155+
with pytest.raises(DocumentInsertError):
156+
G.add_node("smart_person/Officer:35", club="officer")
157+
158+
with pytest.raises(DocumentInsertError):
159+
G.add_node("smart_person/Officer", club="Officer")
160+
161+
assert G.nodes["Mr.Hi:0"]["club"] == "Mr.Hi"
162+
G.add_node("Officer:35", club="Officer")
163+
assert G.nodes["smart_person/Officer:35"]["club"] == "Officer"
164+
165+
assert G["Mr.Hi:0"]["Mr.Hi:1"]["weight"] == 4
166+
G.add_edge("Mr.Hi:0", "Officer:35", weight=5)
167+
assert G["Mr.Hi:0"]["Officer:35"]["weight"] == 5
168+
169+
G.add_nodes_from(
170+
[("Officer:36", {"club": "Officer"}), ("Mr.Hi:37", {"club": "Mr.Hibl"})]
171+
)
172+
assert G.has_node("Officer:36")
173+
assert G.has_node("Mr.Hi:37")
174+
175+
db.delete_graph(graph_name, drop_collections=True)
176+
177+
111178
def test_load_graph_from_nxadb_w_specific_edge_attribute():
112179
graph_name = "KarateGraph"
113180

0 commit comments

Comments
 (0)