Skip to content

Commit

Permalink
fixed bug with identities from neo4j being 0 and therefore not distin…
Browse files Browse the repository at this point in the history
…guishable from None under simple if identity: clauses. Switched everything to if identity is None
  • Loading branch information
jkminder committed Dec 21, 2023
1 parent 5f6ee43 commit d7012ca
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
1 change: 1 addition & 0 deletions rel2graph/core/factories/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

9 changes: 6 additions & 3 deletions rel2graph/neo4j/graph_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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

0 comments on commit d7012ca

Please sign in to comment.