def test_nx_create_edges_with_string_ids(): graph = nx.Graph() graph.add_nodes_from([ (1, { "id": "id1" }), (2, { "id": "id2" }), (3, { "id": "id3" }), ]) graph.add_edges_from([(1, 2), (2, 3)]) expected_cypher_queries = [ "CREATE ( {id: 'id1'});", "CREATE ( {id: 'id2'});", "CREATE ( {id: 'id3'});", "MATCH (n {id: 'id1'}), (m {id: 'id2'}) CREATE (n)-[:TO ]->(m);", "MATCH (n {id: 'id2'}), (m {id: 'id3'}) CREATE (n)-[:TO ]->(m);", ] actual_cypher_queries = list(nx_to_cypher(graph)) assert actual_cypher_queries == expected_cypher_queries
def test_simple_index_nx_to_memgraph(memgraph: Memgraph): graph = nx.Graph() graph.add_nodes_from([ (1, { "labels": "L1", "num": 123 }), (2, { "labels": "L1", "num": 123 }), (3, { "labels": ["L1", "L2", "L3"], "num": 123 }), ]) graph.add_edges_from([(1, 2), (1, 3)]) expected_indexes = { MemgraphIndex("L1", "id"), MemgraphIndex("L2", "id"), MemgraphIndex("L3", "id"), } for query in nx_to_cypher(graph, NetworkXCypherConfig(create_index=True)): memgraph.execute(query) actual_indexes = set(memgraph.get_indexes()) assert actual_indexes == expected_indexes
def test_nx_create_edge_and_node_with_index(): graph = nx.Graph() graph.add_nodes_from([(1, { "labels": "Label1" }), (2, { "labels": ["Label1", "Label2"], "name": "name1" }), (3, { "labels": "Label1" })]) graph.add_edges_from([(1, 2, { "type": "TYPE1" }), (2, 3, { "type": "TYPE2", "data": "abc" })]) expected_cypher_queries = [ "CREATE (:Label1 {id: 1});", "CREATE (:Label1:Label2 {name: 'name1', id: 2});", "CREATE (:Label1 {id: 3});", "CREATE INDEX ON :Label2(id);", "CREATE INDEX ON :Label1(id);", "MATCH (n:Label1 {id: 1}), (m:Label1:Label2 {id: 2}) CREATE (n)-[:TYPE1 ]->(m);", "MATCH (n:Label1:Label2 {id: 2}), (m:Label1 {id: 3}) CREATE (n)-[:TYPE2 {data: 'abc'}]->(m);", ] actual_cypher_queries = list( nx_to_cypher(graph, NetworkXCypherConfig(create_index=True))) assert actual_cypher_queries[0:3] == expected_cypher_queries[0:3] assert set(actual_cypher_queries[3:5]) == set(expected_cypher_queries[3:5]) assert actual_cypher_queries[5:7] == expected_cypher_queries[5:7]
def test_nx_create_edge_and_node_with_properties(): graph = nx.Graph() graph.add_nodes_from([(1, { "labels": "Label1" }), (2, { "labels": ["Label1", "Label2"], "name": "name1" }), (3, { "labels": "Label1" })]) graph.add_edges_from([(1, 2, { "type": "TYPE1" }), (2, 3, { "type": "TYPE2", "data": "abc" })]) expected_cypher_queries = [ "CREATE (:Label1 {id: 1});", "CREATE (:Label1:Label2 {name: 'name1', id: 2});", "CREATE (:Label1 {id: 3});", "MATCH (n:Label1 {id: 1}), (m:Label1:Label2 {id: 2}) CREATE (n)-[:TYPE1 ]->(m);", "MATCH (n:Label1:Label2 {id: 2}), (m:Label1 {id: 3}) CREATE (n)-[:TYPE2 {data: 'abc'}]->(m);", ] actual_cypher_queries = list(nx_to_cypher(graph)) assert actual_cypher_queries == expected_cypher_queries
def test_nx_create_nodes(): graph = nx.Graph() graph.add_nodes_from([1, 2]) expected_cypher_queries = [ "CREATE ( {id: 1});", "CREATE ( {id: 2});", ] actual_cypher_queries = list(nx_to_cypher(graph)) assert actual_cypher_queries == expected_cypher_queries
def test_nx_create_edges(): graph = nx.Graph() graph.add_nodes_from([1, 2, 3]) graph.add_edges_from([(1, 2), (2, 3)]) expected_cypher_queries = [ "CREATE ( {id: 1});", "CREATE ( {id: 2});", "CREATE ( {id: 3});", "MATCH (n {id: 1}), (m {id: 2}) CREATE (n)-[:TO ]->(m);", "MATCH (n {id: 2}), (m {id: 3}) CREATE (n)-[:TO ]->(m);", ] actual_cypher_queries = list(nx_to_cypher(graph)) assert actual_cypher_queries == expected_cypher_queries
def test_nx_to_memgraph(memgraph: Memgraph): graph = nx.Graph() expected_nodes = [ (1, { "labels": "L1", "num": 123 }), (2, { "labels": "L1", "num": 123 }), (3, { "labels": ["L1", "L2", "L3"], "num": 123 }), ] expected_edges = [(1, 2, { "type": "E1", "num": 3.14 }), (1, 3, { "type": "E2", "num": 123 })] graph.add_nodes_from(expected_nodes) graph.add_edges_from(expected_edges) for query in nx_to_cypher(graph): memgraph.execute(query) actual_nodes = list( memgraph.execute_and_fetch("MATCH (n) RETURN n ORDER BY n.id")) assert len(actual_nodes) == 3 for i, node in enumerate(actual_nodes): assert node["n"]._properties["id"] == expected_nodes[i][0] if isinstance(expected_nodes[i][1]["labels"], (list, tuple)): assert node["n"]._labels == set(expected_nodes[i][1]["labels"]) else: assert node["n"]._labels == {expected_nodes[i][1]["labels"]} assert node["n"]._properties["num"] == expected_nodes[i][1]["num"] actual_edges = list( memgraph.execute_and_fetch("MATCH ()-[e]->() RETURN e")) assert len(actual_edges) == 2 for i, edge in enumerate(actual_edges): assert edge["e"]._type == expected_edges[i][2]["type"] assert edge["e"]._properties["num"] == expected_edges[i][2]["num"]
def test_nx_create_nodes_with_string(): graph = nx.Graph() graph.add_nodes_from([ (1, { "id": "id1" }), (2, { "id": "id2" }), ]) expected_cypher_queries = [ "CREATE ( {id: 'id1'});", "CREATE ( {id: 'id2'});", ] actual_cypher_queries = list(nx_to_cypher(graph)) assert actual_cypher_queries == expected_cypher_queries
def test_simple_nx_to_memgraph(memgraph: Memgraph): graph = nx.Graph() graph.add_nodes_from([1, 2, 3]) graph.add_edges_from([(1, 2), (1, 3)]) for query in nx_to_cypher(graph): memgraph.execute(query) actual_nodes = list( memgraph.execute_and_fetch("MATCH (n) RETURN n ORDER BY n.id")) assert len(actual_nodes) == 3 for i, node in enumerate(actual_nodes): assert node["n"]._properties["id"] == i + 1 assert node["n"]._labels == set() actual_edges = list( memgraph.execute_and_fetch("MATCH ()-[e]->() RETURN e")) assert len(actual_edges) == 2 for i, edge in enumerate(actual_edges): assert edge["e"]._type == "TO"
def test_nx_create_edges_with_properties(): graph = nx.Graph() graph.add_nodes_from([1, 2, 3]) graph.add_edges_from([(1, 2, { "type": "TYPE1" }), (2, 3, { "type": "TYPE2", "data": "abc" })]) expected_cypher_queries = [ "CREATE ( {id: 1});", "CREATE ( {id: 2});", "CREATE ( {id: 3});", "MATCH (n {id: 1}), (m {id: 2}) CREATE (n)-[:TYPE1 ]->(m);", "MATCH (n {id: 2}), (m {id: 3}) CREATE (n)-[:TYPE2 {data: 'abc'}]->(m);", ] actual_cypher_queries = list(nx_to_cypher(graph)) assert actual_cypher_queries == expected_cypher_queries
def test_nx_create_nodes_with_properties(): graph = nx.Graph() graph.add_nodes_from([ (1, { "color": "blue", "labels": "L1" }), (2, { "age": 32 }), (3, { "data": [1, 2, 3], "labels": ["L1", "L2", "L3"] }), ]) expected_cypher_queries = [ "CREATE (:L1 {color: 'blue', id: 1});", "CREATE ( {age: 32, id: 2});", "CREATE (:L1:L2:L3 {data: [1, 2, 3], id: 3});", ] actual_cypher_queries = list(nx_to_cypher(graph)) assert actual_cypher_queries == expected_cypher_queries
def test_big_nx_to_memgraph(memgraph: Memgraph, random_nx_graph: nx.Graph): for query in nx_to_cypher(random_nx_graph, NetworkXCypherConfig(create_index=True)): memgraph.execute(query)
def test_big_nx_to_memgraph_with_manual_index(memgraph: Memgraph, random_nx_graph: nx.Graph): memgraph.create_index(MemgraphIndex("Label", "id")) for query in nx_to_cypher(random_nx_graph): memgraph.execute(query)