Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from py2neo import Graph, Node, Relationship
g = Graph()
tx = g.begin()
a = Node("Person", name="Alice")
tx.create(a)
b = Node("Person", name="Bob")
ab = Relationship(a, "KNOWS", b)
tx.create(ab)
tx.commit()
g.exists(ab)
"""
Sample Query
def test_subgraph_repr(self):
a = Node("Person", name="Alice")
b = Node("Person", name="Bob")
ab = Relationship(a, "TO", b)
ba = Relationship(b, "FROM", a)
s = ab | ba
assert isinstance(s, Subgraph)
r = repr(s)
assert r.startswith("({")
assert r.endswith("})")
nodes, _, relationships = r[2:-2].partition("}, {")
items = [item.strip() for item in nodes.split(",")]
assert len(items) == 2
for i, item in enumerate(items):
assert re.match(r"\(:Person \{name: '(Alice|Bob)'\}\)", item)
items = [item.strip() for item in relationships.split(",")]
assert len(items) == 2
for _ in items:
assert re.match(r'\(.*\)-\[:(TO|FROM) \{\}\]->\(.*\)', repr(ab))
def test_can_delete_property_on_node_in_same_batch():
graph_db = neo4j.GraphDatabaseService()
batch = neo4j.WriteBatch(graph_db)
alice = batch.create({"name": "Alice", "age": 34})
batch.delete_property(alice, "age")
results = batch.submit()
alice = results[batch.find(alice)]
assert alice["name"] == "Alice"
assert alice["age"] is None
def test_can_add_labels_to_node_in_same_batch():
graph_db = neo4j.GraphDatabaseService()
batch = neo4j.WriteBatch(graph_db)
alice = batch.create({"name": "Alice"})
batch.add_labels(alice, "human", "female")
batch.remove_label(alice, "female")
results = batch.submit()
alice = results[batch.find(alice)]
assert alice.get_labels() == {"human"}
def test_can_create_path_with_existing_nodes():
graph_db = neo4j.GraphDatabaseService()
alice, bob = graph_db.create({"name": "Alice"}, {"name": "Bob"})
batch = neo4j.WriteBatch(graph_db)
batch.create_path(alice, "KNOWS", bob)
results = batch.submit()
path = results[0]
assert len(path) == 1
assert path.nodes[0] == alice
assert path.relationships[0].type == "KNOWS"
assert path.nodes[1] == bob
def test_can_set_properties_on_node_in_same_batch():
graph_db = neo4j.GraphDatabaseService()
batch = neo4j.WriteBatch(graph_db)
alice = batch.create({})
batch.set_properties(alice, {"name": "Alice", "age": 34})
results = batch.submit()
alice = results[batch.find(alice)]
assert alice["name"] == "Alice"
assert alice["age"] == 34
def test_can_delete_property_on_node_in_same_batch():
graph_db = neo4j.GraphDatabaseService()
batch = neo4j.WriteBatch(graph_db)
alice = batch.create({"name": "Alice", "age": 34})
batch.delete_property(alice, "age")
results = batch.submit()
alice = results[batch.find(alice)]
assert alice["name"] == "Alice"
assert alice["age"] is None
def test_can_create_path_with_existing_nodes():
graph_db = neo4j.GraphDatabaseService()
alice, bob = graph_db.create({"name": "Alice"}, {"name": "Bob"})
batch = neo4j.WriteBatch(graph_db)
batch.create_path(alice, "KNOWS", bob)
results = batch.submit()
path = results[0]
assert len(path) == 1
assert path.nodes[0] == alice
assert path.relationships[0].type == "KNOWS"
assert path.nodes[1] == bob
def test_can_add_labels_to_node_in_same_batch():
graph_db = neo4j.GraphDatabaseService()
batch = neo4j.WriteBatch(graph_db)
a = batch.create({"name": "Alice"})
batch.add_labels(a, "human", "female")
results = batch.submit()
alice = results[batch.find(a)]
assert alice.get_labels() == {"human", "female"}
def test_relationship_creation_on_existing_node(graph):
a = Node()
graph.create(a)
b = Node()
r = Relationship(a, "TO", b)
graph.create(r)
assert r.graph is a.graph is graph
assert r.identity is not None