def test_walkable_repr(self): a = Node("Person", name="Alice") b = Node("Person", name="Bob") c = Node("Person", name="Carol") d = Node("Person", name="Dave") ab = Relationship(a, "LOVES", b) cb = Relationship(c, "HATES", b) cd = Relationship(c, "KNOWS", d) t = Walkable([a, ab, b, cb, c, cd, d]) r = repr(t) expected = "(alice)-[:LOVES]->(bob)<-[:HATES]-(carol)-[:KNOWS]->(dave)" assert r == expected
def test_can_concatenate_path_and_path(self): result = (Walkable([alice, alice_knows_bob, bob]) + Walkable([bob, carol_dislikes_bob, carol])) assert result == Walkable( [alice, alice_knows_bob, bob, carol_dislikes_bob, carol])
def test_can_concatenate_path_and_relationship(self): result = Walkable([alice, alice_knows_bob, bob]) + carol_dislikes_bob assert result == Walkable( [alice, alice_knows_bob, bob, carol_dislikes_bob, carol])
def test_can_concatenate_path_and_node(self): result = Walkable([alice, alice_knows_bob, bob]) + bob assert result == Walkable([alice, alice_knows_bob, bob])
def test_can_concatenate_relationship_and_path(self): result = alice_knows_bob + Walkable([bob, carol_dislikes_bob, carol]) assert result == Walkable( [alice, alice_knows_bob, bob, carol_dislikes_bob, carol])
def test_can_concatenate_relationship_and_node(self): result = alice_knows_bob + bob assert result == Walkable([alice, alice_knows_bob, bob])
def test_can_concatenate_node_and_reversed_path(self): result = alice + Walkable([bob, alice_knows_bob, alice]) assert result == Walkable([alice, alice_knows_bob, bob])
def test_can_concatenate_node_and_path(self): path = Walkable([alice, alice_knows_bob, bob]) result = alice + path assert result == path
def test_can_concatenate_node_and_reversed_relationship(self): bob_knows_alice = Relationship(bob, "KNOWS", alice) result = alice + bob_knows_alice assert result == Walkable([alice, bob_knows_alice, bob])
def test_can_concatenate_node_and_node(self): result = alice + alice assert result == Walkable([alice])
def test_slicing(self): sequence = (alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave) subgraph = Walkable(sequence) assert subgraph[0] == alice_knows_bob assert subgraph[1] == carol_dislikes_bob assert subgraph[2] == carol_married_to_dave assert subgraph[3] == dave_works_for_dave assert subgraph[0:0] == Walkable([alice]) assert subgraph[0:1] == Walkable([alice, alice_knows_bob, bob]) assert subgraph[0:2] == Walkable( [alice, alice_knows_bob, bob, carol_dislikes_bob, carol]) assert subgraph[0:3] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave ]) assert subgraph[0:4] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[0:5] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[0:] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[:1] == Walkable([alice, alice_knows_bob, bob]) assert subgraph[1:1] == Walkable([bob]) assert subgraph[1:2] == Walkable([bob, carol_dislikes_bob, carol]) assert subgraph[1:3] == Walkable( [bob, carol_dislikes_bob, carol, carol_married_to_dave, dave]) assert subgraph[1:4] == Walkable([ bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[1:5] == Walkable([ bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[1:] == Walkable([ bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[:2] == Walkable( [alice, alice_knows_bob, bob, carol_dislikes_bob, carol]) assert subgraph[2:2] == Walkable([carol]) assert subgraph[2:3] == Walkable([carol, carol_married_to_dave, dave]) assert subgraph[2:4] == Walkable( [carol, carol_married_to_dave, dave, dave_works_for_dave, dave]) assert subgraph[2:5] == Walkable( [carol, carol_married_to_dave, dave, dave_works_for_dave, dave]) assert subgraph[2:] == Walkable( [carol, carol_married_to_dave, dave, dave_works_for_dave, dave]) assert subgraph[1:-1] == Walkable( [bob, carol_dislikes_bob, carol, carol_married_to_dave, dave]) assert subgraph[-3:-1] == Walkable( [bob, carol_dislikes_bob, carol, carol_married_to_dave, dave])
def test_inequality(self): other_subgraph = Walkable( [alice, alice_likes_carol, carol, carol_dislikes_bob, bob]) assert self.walkable != other_subgraph assert hash(self.walkable) != hash(other_subgraph)
def test_equality(self): other_subgraph = Walkable(self.sequence) assert self.walkable == other_subgraph assert hash(self.walkable) == hash(other_subgraph)
class WalkableTestCase(TestCase): sequence = (alice, alice_knows_bob, bob, carol_dislikes_bob, carol) walkable = Walkable(sequence) def test_nodes(self): nodes = self.walkable.nodes() assert isinstance(nodes, tuple) assert nodes == (alice, bob, carol) def test_relationships(self): relationships = self.walkable.relationships() assert isinstance(relationships, tuple) assert relationships == (alice_knows_bob, carol_dislikes_bob) def test_length(self): assert len(self.walkable) == 2 def test_order(self): assert order(self.walkable) == 3 def test_size(self): assert size(self.walkable) == 2 def test_equality(self): other_subgraph = Walkable(self.sequence) assert self.walkable == other_subgraph assert hash(self.walkable) == hash(other_subgraph) def test_inequality(self): other_subgraph = Walkable( [alice, alice_likes_carol, carol, carol_dislikes_bob, bob]) assert self.walkable != other_subgraph assert hash(self.walkable) != hash(other_subgraph) def test_inequality_with_other_types(self): assert self.walkable != "this is not a graph" def test_iteration(self): assert tuple(iter(self.walkable)) == (alice_knows_bob, carol_dislikes_bob) def test_slicing(self): sequence = (alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave) subgraph = Walkable(sequence) assert subgraph[0] == alice_knows_bob assert subgraph[1] == carol_dislikes_bob assert subgraph[2] == carol_married_to_dave assert subgraph[3] == dave_works_for_dave assert subgraph[0:0] == Walkable([alice]) assert subgraph[0:1] == Walkable([alice, alice_knows_bob, bob]) assert subgraph[0:2] == Walkable( [alice, alice_knows_bob, bob, carol_dislikes_bob, carol]) assert subgraph[0:3] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave ]) assert subgraph[0:4] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[0:5] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[0:] == Walkable([ alice, alice_knows_bob, bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[:1] == Walkable([alice, alice_knows_bob, bob]) assert subgraph[1:1] == Walkable([bob]) assert subgraph[1:2] == Walkable([bob, carol_dislikes_bob, carol]) assert subgraph[1:3] == Walkable( [bob, carol_dislikes_bob, carol, carol_married_to_dave, dave]) assert subgraph[1:4] == Walkable([ bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[1:5] == Walkable([ bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[1:] == Walkable([ bob, carol_dislikes_bob, carol, carol_married_to_dave, dave, dave_works_for_dave, dave ]) assert subgraph[:2] == Walkable( [alice, alice_knows_bob, bob, carol_dislikes_bob, carol]) assert subgraph[2:2] == Walkable([carol]) assert subgraph[2:3] == Walkable([carol, carol_married_to_dave, dave]) assert subgraph[2:4] == Walkable( [carol, carol_married_to_dave, dave, dave_works_for_dave, dave]) assert subgraph[2:5] == Walkable( [carol, carol_married_to_dave, dave, dave_works_for_dave, dave]) assert subgraph[2:] == Walkable( [carol, carol_married_to_dave, dave, dave_works_for_dave, dave]) assert subgraph[1:-1] == Walkable( [bob, carol_dislikes_bob, carol, carol_married_to_dave, dave]) assert subgraph[-3:-1] == Walkable( [bob, carol_dislikes_bob, carol, carol_married_to_dave, dave])