Beispiel #1
0
 def test_path_hashing(self):
     alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
     bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
     carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
     alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
     carol_dislikes_bob = Relationship(carol.id, bob.id, "DISLIKES")
     path_1 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
     path_2 = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
     assert hash(path_1) == hash(path_2)
Beispiel #2
0
 def test_can_create_path(self):
     alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
     bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
     carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
     alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
     carol_dislikes_bob = Relationship(carol.id, bob.id, "DISLIKES")
     path = Path(alice, alice_knows_bob, bob, carol_dislikes_bob, carol)
     assert path.start == alice
     assert path.end == carol
     assert path.nodes == (alice, bob, carol)
     assert path.relationships == (alice_knows_bob, carol_dislikes_bob)
     assert list(path) == [alice_knows_bob, carol_dislikes_bob]
     assert repr(path)
Beispiel #3
0
 def test_can_hydrate_path(self):
     alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
     bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
     carol = Node.hydrate(3, {"Person"}, {"name": "Carol", "age": 55})
     alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
     carol_dislikes_bob = Relationship(carol.id, bob.id, "DISLIKES")
     rels = [UnboundRelationship(alice_knows_bob.type, alice_knows_bob.properties),
             UnboundRelationship(carol_dislikes_bob.type, carol_dislikes_bob.properties)]
     rels[0].id = alice_knows_bob.id
     rels[1].id = carol_dislikes_bob.id
     path = Path.hydrate([alice, bob, carol], rels, [1, 1, -2, 2])
     assert path.start == alice
     assert path.end == carol
     assert path.nodes == (alice, bob, carol)
     assert path.relationships == (alice_knows_bob, carol_dislikes_bob)
     assert list(path) == [alice_knows_bob, carol_dislikes_bob]
     assert repr(path)
Beispiel #4
0
 def test_can_create_relationship(self):
     alice = Node.hydrate(1, {"Person"}, {"name": "Alice", "age": 33})
     bob = Node.hydrate(2, {"Person"}, {"name": "Bob", "age": 44})
     alice_knows_bob = Relationship(alice.id, bob.id, "KNOWS", {"since": 1999})
     assert alice_knows_bob.start == alice.id
     assert alice_knows_bob.type == "KNOWS"
     assert alice_knows_bob.end == bob.id
     assert set(alice_knows_bob.keys()) == {"since"}
     assert set(alice_knows_bob.values()) == {1999}
     assert set(alice_knows_bob.items()) == {("since", 1999)}
     assert alice_knows_bob.get("since") == 1999
     assert repr(alice_knows_bob)
Beispiel #5
0
def get_relationship(string_entity):
    point_up = None
    if string_entity[:3] == "<-[":
        point_up = False
        string_entity = string_entity[3:]
        rel = string_entity[:string_entity.find(']-')]
        string_entity = string_entity[string_entity.find(']-') + 2:]
    elif string_entity[:2] == "-[":
        point_up = True
        string_entity = string_entity[2:]
        rel = string_entity[:string_entity.find(']-')]
        string_entity = string_entity[string_entity.find(']->') + 3:]
    elif string_entity[0] == "[":
        string_entity = string_entity[1:]
        rel = string_entity[:string_entity.find(']')]
        string_entity = string_entity[string_entity.find(']') + 1:]
    else:
        raise ValueError("Cant identify relationship from: %s" % string_entity)
    type, properties, str = get_labels_and_properties(rel)
    if len(type) > 1:
        raise ValueError("Relationship can only have one type. Got: %s" % type)
    relationship = Relationship(1, 2, type[0], properties=properties)
    return relationship, string_entity, point_up