Skip to content

Instantly share code, notes, and snippets.

@sorrell
Created February 2, 2022 21:43
Show Gist options
  • Save sorrell/49a8ae02272a93dce893a5492e246426 to your computer and use it in GitHub Desktop.
Save sorrell/49a8ae02272a93dce893a5492e246426 to your computer and use it in GitHub Desktop.
Test MERGE direction in Neo4j
from neo4j import GraphDatabase
class TestDirection:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def run_test(self):
left_count = 0
right_count = 0
with self.driver.session() as session:
for i in range(10000):
create = session.write_transaction(self._createNode)
right = session.write_transaction(self._matchRight)
if right is True:
right_count = right_count + 1
left = session.write_transaction(self._matchLeft)
if left is True:
left_count = left_count + 1
cleanup = session.write_transaction(self._cleanup)
print("Left: " + str(left_count))
print("Right: " + str(right_count))
@staticmethod
def _createNode(tx):
result = tx.run("MERGE ({side: 'left'})-[:edge]-({side:'right'})")
if result.single() is None:
return False
return True
@staticmethod
def _matchRight(tx):
result = tx.run("MATCH (l{side: 'left'})-[:edge]->(r{side:'right'}) RETURN l, r")
if result.single() is None:
return False
return True
@staticmethod
def _matchLeft(tx):
result = tx.run("MATCH (l{side: 'left'})<-[:edge]-(r{side:'right'}) RETURN l, r")
if result.single() is None:
return False
return True
@staticmethod
def _cleanup(tx):
result = tx.run("MATCH (n) DETACH DELETE n")
if result.single() is None:
return False
return True
if __name__ == "__main__":
tester = TestDirection("bolt://localhost:7687", "neo4j", "test")
tester.run_test()
tester.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment