diff --git a/rel2graph/core/factories/matcher.py b/rel2graph/core/factories/matcher.py index 5b946fd..0fc7cd3 100644 --- a/rel2graph/core/factories/matcher.py +++ b/rel2graph/core/factories/matcher.py @@ -97,5 +97,6 @@ def match(self, resource: Resource) -> List[Node]: # Convert to nodes match_list = [Node.from_dict(r['labels'], r['properties'], identity=r["identity"]) for r in match_list] + return match_list diff --git a/rel2graph/neo4j/graph_elements.py b/rel2graph/neo4j/graph_elements.py index 31303f9..97ecc77 100644 --- a/rel2graph/neo4j/graph_elements.py +++ b/rel2graph/neo4j/graph_elements.py @@ -228,6 +228,7 @@ def __db_merge__(self, tx, primary_label=None, primary_key=None): p_key = node.__primarykey__ else: p_key = primary_key + # Add node to the node dictionary key = (p_label, p_key, frozenset(node.labels)) node_dict.setdefault(key, []).append(node) @@ -365,7 +366,7 @@ def __getstate__(self): def __hash__(self): try: - if self._identity: + if self._identity is not None: return hash(self._identity) except: pass @@ -424,7 +425,7 @@ def from_dict(labels: List[str], properties: dict, primary_key: str = None, prim node.set_primary_key(primary_key) if primary_label: node.set_primary_label(primary_label) - if identity: + if identity is not None: node.identity = identity return node @@ -455,6 +456,8 @@ def __repr__(self): args = list(self.labels) kwargs = OrderedDict() d = dict(self) + if self.identity is None: + d["identity"] = self.identity for key in sorted(d): if is_safe_key(key): args.append("%s=%r" % (key, d[key])) @@ -517,7 +520,7 @@ def from_dict(start_node: Node, end_node: Node, type: str, properties: dict, pri relationship = Relationship(start_node, type, end_node, **properties) if primary_key: relationship.set_primary_key(primary_key) - if identity: + if identity is not None: relationship.identity = identity return relationship diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index 44c931a..870f080 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -44,13 +44,13 @@ def auth(): return neo4j.basic_auth("neo4j", "password") def delete_all(session): - session.run("MATCH (n) DETACH DELETE n") + session.run("MATCH (n) DETACH DELETE n;") def num_nodes(session): - return session.run("MATCH (n) RETURN COUNT(n) AS num_nodes").single()["num_nodes"] + return session.run("MATCH (n) RETURN COUNT(DISTINCT n) AS num_nodes").single()["num_nodes"] def num_relationships(session): - return session.run("MATCH ()-[r]->() RETURN COUNT(r) AS num_relations").single()["num_relations"] + return session.run("MATCH ()-[r]->() RETURN COUNT(DISTINCT r) AS num_relations").single()["num_relations"] def get_nodes(session, labels=[]): if not isinstance(labels, list) and not isinstance(labels, tuple): diff --git a/tests/integration/test_merge_relationships.py b/tests/integration/test_merge.py similarity index 76% rename from tests/integration/test_merge_relationships.py rename to tests/integration/test_merge.py index 3382f18..1ec4f10 100644 --- a/tests/integration/test_merge_relationships.py +++ b/tests/integration/test_merge.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- """ -Integration tests for testing the MERGE_RELATIONSHIPS wrapper. +Integration tests for testing the merging and MERGE_RELATIONSHIPS wrapper. authors: Julian Minder """ @@ -58,7 +58,20 @@ def test_standart_same_resource(config, session, uri, auth): @pytest.mark.parametrize("config",[(1,True), (1, False) ,(5, False)]) -def test_merge(config, session, uri, auth): +def test_merge_nodesasdf(config, session, uri, auth): + schema = """ + ENTITY("Entity"): + NODE("Entity") node: + + id = INT(Entity.id) + """ + entities = pd.DataFrame({"id": [1,2, 1, 2]}) + iterator = IteratorIterator([PandasDataFrameIterator(entities, "Entity")]) + converter = Converter(schema, iterator, uri, auth, serialize=config[1], num_workers=config[0]) + converter() + assert num_nodes(session) == 2 + +@pytest.mark.parametrize("config",[(1,True), (1, False) ,(5, False)]) +def test_merge_relationships(config, session, uri, auth): schema = """ ENTITY("Entity"): NODE("Entity") node: @@ -70,12 +83,12 @@ def test_merge(config, session, uri, auth): entities = pd.DataFrame({"id": [1,2]}) relations = pd.DataFrame({"source_id": [1,1], "target_id": [2,2]}) iterator = IteratorIterator([PandasDataFrameIterator(entities, "Entity"), PandasDataFrameIterator(relations, "Relation")]) - converter = Converter(schema, iterator, uri, auth, serialize=config[1], num_workers=config[0]) + converter = Converter(schema, iterator, uri, auth, serialize=config[1], num_workers=config[0], batch_size=1) converter() assert num_relationships(session) == 1 -@pytest.mark.parametrize("config",[(1,True), (1, False) ,(5, False)]) -def test_merge_same_resource(config, session, uri, auth): +@pytest.mark.parametrize("config",[(1,True)]) +def test_merge_relationships_same_resource(config, session, uri, auth): schema = """ ENTITY("Entity"): NODE("Entity") node: @@ -88,6 +101,15 @@ def test_merge_same_resource(config, session, uri, auth): entities = pd.DataFrame({"id": [1,2]}) relations = pd.DataFrame({"source_id": [1], "target_id": [2]}) iterator = IteratorIterator([PandasDataFrameIterator(entities, "Entity"), PandasDataFrameIterator(relations, "Relation")]) - converter = Converter(schema, iterator, uri, auth, serialize=config[1], num_workers=config[0]) + converter = Converter(schema, iterator, uri, auth, serialize=config[1], num_workers=config[0], batch_size=1) converter() + n = num_relationships(session) + if n != 1: + with session.begin_transaction() as tx: + for record in tx.run("MATCH (a) RETURN a.id, id(a)"): + print(record) + for record in tx.run("MATCH (a)-[r]->(b) RETURN r, a.id, b.id, id(r), id(a), id(b)"): + print(record) + print("n", n) + exit() assert num_relationships(session) == 1 \ No newline at end of file