Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_can_create_path_2(graph):
path = Path({"name": "Alice"}, "KNOWS", {"name": "Bob"})
nodes = path.nodes
assert len(path) == 1
assert nodes[0]["name"] == "Alice"
assert type(path[0]) is KNOWS
assert nodes[-1]["name"] == "Bob"
path = Path(path, "KNOWS", {"name": "Carol"})
nodes = path.nodes
assert len(path) == 2
assert nodes[0]["name"] == "Alice"
assert type(path[0]) is KNOWS
assert nodes[1]["name"] == "Bob"
path = Path({"name": "Zach"}, "KNOWS", path)
nodes = path.nodes
assert len(path) == 3
assert nodes[0]["name"] == "Zach"
assert type(path[0]) is KNOWS
assert nodes[1]["name"] == "Alice"
assert type(path[1]) is KNOWS
assert nodes[2]["name"] == "Bob"
def test_can_slice_path(graph):
a = Node(name="Alice")
b = Node(name="Bob")
c = Node(name="Carol")
d = Node(name="Dave")
e = Node(name="Eve")
f = Node(name="Frank")
path = Path(a, "KNOWS", b, "KNOWS", c, "KNOWS", d, "KNOWS", e, "KNOWS", f)
assert len(path) == 5
assert path[0] == Relationship(a, "KNOWS", b)
assert path[1] == Relationship(b, "KNOWS", c)
assert path[2] == Relationship(c, "KNOWS", d)
assert path[-1] == Relationship(e, "KNOWS", f)
assert path[0:2] == Path(a, "KNOWS", b, "KNOWS", c)
assert path[3:5] == Path(d, "KNOWS", e, "KNOWS", f)
assert path[:] == Path(a, "KNOWS", b, "KNOWS", c, "KNOWS", d, "KNOWS", e, "KNOWS", f)
def test_subgraph_from_other_subgraph(self):
s = Subgraph(Path({"name": "Alice"}, "KNOWS", {"name": "Bob"}))
t = Subgraph(s)
assert len(t) == 1
assert t.order == 2
assert t.size == 1
assert s.__bool__()
assert s.__nonzero__()
assert not s.bound
def test_subgraph_add(self):
s = Subgraph()
assert len(s) == 0
assert s.order == 0
assert s.size == 0
assert not s.__bool__()
assert not s.__nonzero__()
assert not s.bound
s.add(Path({"name": "Alice"}, "KNOWS", {"name": "Bob"}))
assert len(s) == 1
assert s.order == 2
assert s.size == 1
assert s.__bool__()
assert s.__nonzero__()
assert not s.bound
def test_can_slice_path(graph):
a = Node(name="Alice")
b = Node(name="Bob")
c = Node(name="Carol")
d = Node(name="Dave")
e = Node(name="Eve")
f = Node(name="Frank")
path = Path(a, "KNOWS", b, "KNOWS", c, "KNOWS", d, "KNOWS", e, "KNOWS", f)
assert len(path) == 5
assert path[0] == Relationship(a, "KNOWS", b)
assert path[1] == Relationship(b, "KNOWS", c)
assert path[2] == Relationship(c, "KNOWS", d)
assert path[-1] == Relationship(e, "KNOWS", f)
assert path[0:2] == Path(a, "KNOWS", b, "KNOWS", c)
assert path[3:5] == Path(d, "KNOWS", e, "KNOWS", f)
assert path[:] == Path(a, "KNOWS", b, "KNOWS", c, "KNOWS", d, "KNOWS", e, "KNOWS", f)
def test_subgraph_inequality(self):
s1 = Subgraph(Path({"name": "Alice"}, "KNOWS", {"name": "Bob"}))
s2 = Subgraph(Path({"name": "Alice"}, "KNOWS", {"name": "Robert"}))
assert s1 != s2
assert s1 != ""
def test_can_load_object_with_relationships(self):
alice_node = self.index_manager.get_or_create_indexed_node(
"People", "email", "alice@example.com", {
"email": "alice@example.com",
"name": "Alice Allison",
"age": 34,
}
)
path = Path(alice_node, "LIKES", {"name": "Bob Robertson"})
self.graph.create(path)
bob_node = path.nodes()[1]
alice = self.store.load_unique("People", "email", "alice@example.com", Person)
assert isinstance(alice, Person)
assert hasattr(alice, "__node__")
assert alice.__node__ == alice_node
assert hasattr(alice, "__rel__")
assert alice.__rel__ == {
"LIKES": [({}, bob_node)],
}
assert alice.email == "alice@example.com"
assert alice.name == "Alice Allison"
assert alice.age == 34
friends = self.store.load_related(alice, "LIKES", Person)
assert isinstance(friends, list)
assert len(friends) == 1
def test_can_join_paths(graph):
a = Node(name="Alice")
b = Node(name="Bob")
c = Node(name="Carol")
d = Node(name="Dave")
path1 = Path(a, "KNOWS", b)
path2 = Path(c, "KNOWS", d)
path = Path(path1, "KNOWS", path2)
assert list(path) == [
Relationship(a, 'KNOWS', b),
Relationship(b, 'KNOWS', c),
Relationship(c, 'KNOWS', d),
]
def test_can_pull_path(graph):
alice = Node(name="Alice")
bob = Node(name="Bob")
carol = Node(name="Carol")
dave = Node(name="Dave")
path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol, "KNOWS", dave)
graph.create(path)
assert path[0]["amount"] is None
assert path[1]["amount"] is None
assert path[2]["since"] is None
statement = ("MATCH ()-[ab]->() WHERE id(ab)={ab} "
"MATCH ()-[bc]->() WHERE id(bc)={bc} "
"MATCH ()-[cd]->() WHERE id(cd)={cd} "
"SET ab.amount = 'lots', bc.amount = 'some', cd.since = 1999")
id_0 = path[0].identity
id_1 = path[1].identity
id_2 = path[2].identity
parameters = {"ab": id_0, "bc": id_1, "cd": id_2}
graph.run(statement, parameters)
graph.pull(path)
assert path[0]["amount"] == "lots"
assert path[1]["amount"] == "some"
def _yielded_value(self, value):
"""
Translate 'raw' query return to an appropriate object in our world
:param value: object: Node, relationship, path, list, tuple, scalar value
:return: object: appropriate value in our world
"""
if isinstance(value, py2neo.Node):
return self._construct_obj_from_node(value)
elif isinstance(value, py2neo.Relationship):
from graphnodes import NeoRelationship
return NeoRelationship(value)
elif isinstance(value, py2neo.Path):
return "Sorry, Path values not yet supported"
elif isinstance(value, (list, tuple)):
result = []
for elem in value:
result.append(self._yielded_value(elem))
return result
else:
# Integers, strings, None, etc.
return value