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 _insert_queries(queries: List[str], host: str, port: int, username: str, password: str, encrypted: bool) -> None: """Used by multiprocess insertion of nx into memgraph, works on a chunk of queries.""" memgraph = Memgraph(host, port, username, password, encrypted) while len(queries) > 0: try: query = queries.pop() memgraph.execute(query) except mgclient.DatabaseError as e: queries.append(query) logging.getLogger(__file__).warning(f"Ignoring database error: {e}") continue
def populated_memgraph(dataset_file: str) -> Memgraph: memgraph = Memgraph() memgraph.ensure_indexes([]) memgraph.ensure_constraints([]) memgraph.drop_database() with get_data_dir().joinpath(dataset_file).open("r") as dataset: for query in dataset: memgraph.execute(query) yield memgraph memgraph.drop_database()
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_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_path_deserialisation(): db = Memgraph() db.execute("create (:Person {id: 1, name: 'person'});") db.execute("create (:Alice {id: 8, name: 'alice'});") db.execute("match (a:Alice) match(b:Person) create (a)-[:FRIENDS]->(b);") result = list(db.execute_and_fetch("MATCH p = ()-[*1]-() RETURN p")) path = result[0]["p"] assert isinstance(path, Path) assert len(path._nodes) == 2 assert len(path._relationships) == 1 db.drop_database()
def test_automatic_deserialisation_from_database(): db = Memgraph() db.execute("create (:Person {id: 1, name: 'person'});") db.execute("create (:Alice {id: 8, name: 'alice'});") db.execute("match (a:Alice) match(b:Person) create (a)-[:FRIENDS]->(b);") result = list(db.execute_and_fetch("match (a)-[r]->(b) return a, r, b")) for node in result: a = node["a"] assert isinstance(a, Alice) assert a.id == 8 assert a.name == "alice" assert a._node_labels == {"Alice"} assert isinstance(a._node_id, int) assert a._properties == {"id": 8, "name": "alice"} assert isinstance(a._id, int) r = node["r"] assert isinstance(r, Friends) assert r._type == "FRIENDS" assert isinstance(r._relationship_id, int) assert isinstance(r._start_node_id, int) assert isinstance(r._end_node_id, int) assert r._properties == {} assert isinstance(r._id, int) b = node["b"] assert isinstance(b, Person) assert b.id == 1 assert b.name == "person" assert b._node_labels == {"Person"} assert isinstance(b._node_id, int) assert b._properties == {"id": 1, "name": "person"} assert isinstance(b._id, int) db.drop_database()
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)
def cleanup_trigger(): yield memgraph = Memgraph() memgraph.execute("DROP TRIGGER test_trigger;")