Пример #1
0
 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
Пример #2
0
 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])
Пример #3
0
 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])
Пример #4
0
 def test_can_concatenate_path_and_node(self):
     result = Walkable([alice, alice_knows_bob, bob]) + bob
     assert result == Walkable([alice, alice_knows_bob, bob])
Пример #5
0
 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])
Пример #6
0
 def test_can_concatenate_relationship_and_node(self):
     result = alice_knows_bob + bob
     assert result == Walkable([alice, alice_knows_bob, bob])
Пример #7
0
 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])
Пример #8
0
 def test_can_concatenate_node_and_path(self):
     path = Walkable([alice, alice_knows_bob, bob])
     result = alice + path
     assert result == path
Пример #9
0
 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])
Пример #10
0
 def test_can_concatenate_node_and_node(self):
     result = alice + alice
     assert result == Walkable([alice])
Пример #11
0
 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])
Пример #12
0
 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)
Пример #13
0
 def test_equality(self):
     other_subgraph = Walkable(self.sequence)
     assert self.walkable == other_subgraph
     assert hash(self.walkable) == hash(other_subgraph)
Пример #14
0
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])