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_slice_path(self):
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_construction_of_path_with_loop(self):
sequence = [carol, carol_married_to_dave, dave, dave_works_for_dave, dave]
path = Path(*sequence)
assert order(path) == 2
assert size(path) == 2
assert len(path) == 2
assert set(path.nodes) == {carol, dave}
assert set(path.relationships) == {carol_married_to_dave, dave_works_for_dave}
assert path.start_node == carol
assert path.end_node == dave
assert len(path) == 2
assert list(walk(path)) == sequence
def test_can_delete_path(self):
alice, bob, carol, dave = Node(), Node(), Node(), Node()
path = Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol, "KNOWS", dave)
self.graph.create(path)
assert self.graph.exists(path)
self.graph.delete(path)
assert not self.graph.exists(path)
def test_can_iterate_path(self):
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 list(path) == [
Relationship(a, 'KNOWS', b),
Relationship(b, 'KNOWS', c),
Relationship(c, 'KNOWS', d),
Relationship(d, 'KNOWS', e),
Relationship(e, 'KNOWS', f),
]
assert list(enumerate(path)) == [
(0, Relationship(a, 'KNOWS', b)),
(1, Relationship(b, 'KNOWS', c)),
(2, Relationship(c, 'KNOWS', d)),
(3, Relationship(d, 'KNOWS', e)),
(4, Relationship(e, 'KNOWS', f))
]
def test_path_in_several_ways(self):
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)
assert path.__bool__()
assert path.__nonzero__()
assert path[0] == Relationship(alice, "LOVES", bob)
assert path[1] == Relationship(carol, "HATES", bob)
assert path[2] == Relationship(carol, "KNOWS", dave)
assert path[-1] == Relationship(carol, "KNOWS", dave)
assert path[0:1] == Path(alice, "LOVES", bob)
assert path[0:2] == Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol)
try:
_ = path[7]
except IndexError:
assert True
else:
assert False
def test_path_in_several_ways(self):
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)
assert path.__bool__()
assert path.__nonzero__()
assert path[0] == Relationship(alice, "LOVES", bob)
assert path[1] == Relationship(carol, "HATES", bob)
assert path[2] == Relationship(carol, "KNOWS", dave)
assert path[-1] == Relationship(carol, "KNOWS", dave)
assert path[0:1] == Path(alice, "LOVES", bob)
assert path[0:2] == Path(alice, "LOVES", bob, Relationship(carol, "HATES", bob), carol)
try:
_ = path[7]
except IndexError:
assert True
else:
assert False
def test_can_make_new_path_from_path(self):
# given
path = Path(self.alice, "LOVES", self.bob, Relationship(self.carol, "HATES", self.bob),
self.carol, "KNOWS", self.dave)
# when
new_path = Path(path)
# then
new_rels = list(new_path)
assert new_rels == [
Relationship(self.alice, "LOVES", self.bob),
Relationship(self.carol, "HATES", self.bob),
Relationship(self.carol, "KNOWS", self.dave),
]
def test_can_create_path(self):
path = Path({"name": "Alice"}, "KNOWS", {"name": "Bob"})
nodes = path.nodes
assert len(path) == 1
assert nodes[0]["name"] == "Alice"
assert path[0].type == "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 path[0].type == "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 path[0].type == "KNOWS"
assert nodes[1]["name"] == "Alice"
assert path[1].type == "KNOWS"
assert nodes[2]["name"] == "Bob"
def write(self, obj):
""" Write any entity, value or collection.
"""
if obj is None:
pass
elif isinstance(obj, Node):
self.write_node(id(obj), obj.labels(), dict(obj))
elif isinstance(obj, Path):
self.write_path(obj)
elif isinstance(obj, dict):
self.write_map(obj)
elif is_collection(obj):
self.write_list(obj)
else:
self.write_value(obj)
def write(self, obj, **kwargs):
""" Write any entity, value or collection.
:arg obj:
"""
if obj is None:
pass
elif isinstance(obj, Node):
self.write_node(obj, **kwargs)
elif isinstance(obj, Relationship):
self.write_relationship(obj, **kwargs)
elif isinstance(obj, Path):
self.write_walkable(obj)
elif isinstance(obj, dict):
self.write_map(obj, **kwargs)
elif is_collection(obj):
self.write_list(obj)
else:
self.write_value(obj)