|
3 | 3 |
|
4 | 4 | import networkx as nx |
5 | 5 | import pytest |
6 | | -from arango import DocumentDeleteError |
| 6 | +from arango.exceptions import DocumentInsertError |
7 | 7 | from phenolrs.networkx.typings import ( |
8 | 8 | DiGraphAdjDict, |
9 | 9 | GraphAdjDict, |
|
17 | 17 |
|
18 | 18 | from .conftest import create_grid_graph, create_line_graph, db, run_gpu_tests |
19 | 19 |
|
20 | | -G_NX = nx.karate_club_graph() |
| 20 | +G_NX: nx.Graph = nx.karate_club_graph() |
21 | 21 | G_NX_digraph = nx.DiGraph(G_NX) |
22 | 22 | G_NX_multigraph = nx.MultiGraph(G_NX) |
23 | 23 | G_NX_multidigraph = nx.MultiDiGraph(G_NX) |
@@ -108,6 +108,73 @@ def test_load_graph_from_nxadb(): |
108 | 108 | db.delete_graph(graph_name, drop_collections=True) |
109 | 109 |
|
110 | 110 |
|
| 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 | + |
111 | 178 | def test_load_graph_from_nxadb_w_specific_edge_attribute(): |
112 | 179 | graph_name = "KarateGraph" |
113 | 180 |
|
|
0 commit comments