def _create_n_user_objects(n: int) -> None: db = Memgraph() SQLitePropertyDatabase("./tests/on_disk_storage.db", db) huge_string = "I LOVE MEMGRAPH" * 1000 for _ in range(n): id_ = random.randint(1, 2 * n) try: user1 = User(id=id_, huge_string=huge_string).save(db) assert user1.huge_string == huge_string except mgclient.DatabaseError: # Memgraph collision happened continue except GQLAlchemyError as e: print("Error when saving a node.") print(traceback.format_exc()) print(list(db.execute_and_fetch("match (a) return a;"))) print(list(db.execute_and_fetch("show constraint info;"))) raise e try: user2 = User(id=id_).load(db) assert user2.huge_string == huge_string except mgclient.DatabaseError: # Memgraph collision happened continue except GQLAlchemyError as e: print("Error in loading a node.") print(traceback.format_exc()) print(list(db.execute_and_fetch("match (a) return a;"))) print(list(db.execute_and_fetch("show constraint info;"))) raise e
def test_edges_mapping(populated_memgraph: Memgraph): query = "MATCH ()-[e]->() RETURN e" expected_edges = [ Relationship( _id=0, _type="Relation", _start_node_id=0, _end_node_id=1, id=0, name="name1", ), Relationship( _id=1, _type="Relation", _start_node_id=1, _end_node_id=2, id=1, num=100, ), Relationship( _id=2, _type="Relation", _start_node_id=2, _end_node_id=0, id=2, data=[1, 2, 3], ), ] actual_edges = [ r["e"] for r in populated_memgraph.execute_and_fetch(query) ] compare_edges(actual_edges, expected_edges)
def test_nodes_mapping(populated_memgraph: Memgraph): query = "MATCH (n) RETURN n" expected_nodes = [ Node( _id=0, _node_labels={"Node"}, id=0, name="name1", ), Node( _id=1, _node_labels={"Node"}, id=1, data=[1, 2, 3], ), Node( _id=2, _node_labels={"Node"}, id=2, data={ "a": 1, "b": "abc" }, ), ] actual_nodes = [ r["n"] for r in populated_memgraph.execute_and_fetch(query) ] compare_nodes(actual_nodes, expected_nodes)
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_path_mapping(populated_memgraph: Memgraph): query = "MATCH p = ({id: 0})-[*]->({id: 3}) RETURN p" expected_nodes = [ Node(_id=0, _node_labels={"Node"}, id=0), Node(_id=1, _node_labels={"Node"}, id=1), Node(_id=2, _node_labels={"Node"}, id=2), Node(_id=2, _node_labels={"Node"}, id=3), ] expected_relationships = [ Relationship( _id=0, _type="Relation", _start_node_id=0, _end_node_id=1, id=0, ), Relationship( _id=1, _type="Relation", _start_node_id=1, _end_node_id=2, id=1, ), Relationship( _id=2, _type="Relation", _start_node_id=2, _end_node_id=3, id=2, ), ] actual_paths = [ r["p"] for r in populated_memgraph.execute_and_fetch(query) ] assert len(actual_paths) == 1 actual_path = actual_paths[0] compare_nodes(actual_path._nodes, expected_nodes) compare_edges(actual_path._relationships, expected_relationships)
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 query(command: str) -> Iterator[Dict[str, Any]]: """Queries Memgraph database and returns iterator of results""" db = Memgraph() yield from db.execute_and_fetch(command)