예제 #1
0
def test_create_undirected_relationship():
    rel = Relationship('KNOWS',
                       Node('User', var='m'),
                       Node('User', var='n'),
                       var='r',
                       directed=False)
    assert str(Create(rel)) == 'CREATE (m)-[r:`KNOWS`]-(n)'
예제 #2
0
def test_full_relationship_create():
    expected_query = ('MATCH (m:`User`)\n'
                      'MATCH (n:`User`)\n'
                      'CREATE (m)-[rel:`KNOWS`]->(n)')
    user_m = Node('User', name=Property(), var='m')
    user_n = user_m.copy(var='n')
    rel = Relationship('KNOWS', user_m, user_n)
    query = Match(user_m) & Match(user_n) & Create(rel)
    assert str(query) == expected_query
    assert len(query.params) == 0
예제 #3
0
def test_merge():
    kev = Node('Person', name='Kevin', var='kev').bind('name')
    ali = Node('Person', name='Ali', var='ali').bind('name')
    rel = Relationship('LOVES', kev, ali, duration='forever').bind('duration')
    expected_stmt = [
        'MATCH (kev:`Person`)',
        '    WHERE kev.name = {kev_name}',
        'MATCH (ali:`Person`)',
        '    WHERE ali.name = {ali_name}',
        'MERGE (kev)-[rel:`LOVES` {duration: {rel_duration}}]->(ali)'
    ]
    query = Match(kev) & Match(ali) & Merge(rel)
    assert str(query) == '\n'.join(expected_stmt)
    assert len(query.params) == 3
    assert 'kev_name' in query.params
    assert query.params['kev_name'] == 'Kevin'
    assert 'ali_name' in query.params
    assert query.params['ali_name'] == 'Ali'
    assert 'rel_duration' in query.params
    assert query.params['rel_duration'] == 'forever'
예제 #4
0
def test_full_match():
    expected_stmt = [
        'MATCH (m:`Person`)',
        '    WHERE m.name = {m_name}',
        'MATCH (n:`Person`)',
        '    WHERE n.name = {n_name}',
        'MATCH (m)-[rel:`KNOWS`]->(n)',
    ]
    person_m = Node('Person', name='Alice', var='m').bind('name')
    person_n = person_m.copy(name='Bob', var='n')
    knows = Relationship('KNOWS', person_m, person_n)
    match = Match(person_m) & Match(person_n) & Match(knows)
    assert str(match) == '\n'.join(expected_stmt)
    assert len(match.params) == 2
    assert 'm_name' in match.params
    assert match.params['m_name'] == 'Alice'
    assert 'n_name' in match.params
    assert match.params['n_name'] == 'Bob'
    match.return_().order_by(person_n['name']).skip(1).limit(1)
    expected_stmt += [
        'RETURN *',
        'ORDER BY n.name',
        'SKIP 1',
        'LIMIT 1',
    ]
    assert str(match) == '\n'.join(expected_stmt)
    assert len(match.params) == 2
    assert 'm_name' in match.params
    assert match.params['m_name'] == 'Alice'
    assert 'n_name' in match.params
    assert match.params['n_name'] == 'Bob'
    expected_stmt[-3] += ' DESC'
    for _ in range(3):
        match.pop()
    match.order_by(person_n['name'], desc=True).skip(1).limit(1)
    assert str(match) == '\n'.join(expected_stmt)
    assert len(match.params) == 2
    assert 'm_name' in match.params
    assert match.params['m_name'] == 'Alice'
    assert 'n_name' in match.params
    assert match.params['n_name'] == 'Bob'
예제 #5
0
def test_create_relationship():
    rel = Relationship('KNOWS', Node('User'), Node('User'))
    with pytest.raises(ValueError):
        create = Create(rel)
    rel.end_node.var = 'end_node'
    assert str(Create(rel)) == 'CREATE (node)-[rel:`KNOWS`]->(end_node)'
예제 #6
0
def test_create_relation_to_nowhere():
    rel = Relationship('KNOWS', Node('User'))
    with pytest.raises(DetachedObjectError):
        create = Create(rel)
예제 #7
0
def test_relationship_set_once():
    rel = Relationship(None)
    rel.type = 'X'
    rel.start_node = Node('x')
    rel.end_node = Node('y')
    with pytest.raises(ImmutableAttributeError):
        rel.directed = False
    with pytest.raises(ImmutableAttributeError):
        rel.type = 'Y'
    with pytest.raises(ImmutableAttributeError):
        rel.start_node = Node('a')
    with pytest.raises(ImmutableAttributeError):
        rel.end_node = Node('b')

    rel = Relationship('X')
    with pytest.raises(ImmutableAttributeError):
        rel.type = 'Y'

    rel = Relationship('X', Node('a'))
    with pytest.raises(ImmutableAttributeError):
        rel.start_node = Node('a')

    rel = Relationship('X', Node('a'), Node('b'))
    with pytest.raises(ImmutableAttributeError):
        rel.end_node = Node('a')

    with pytest.raises(ImmutableAttributeError):
        del rel.type
    with pytest.raises(ImmutableAttributeError):
        del rel.directed
    with pytest.raises(ImmutableAttributeError):
        del rel.start_node
    with pytest.raises(ImmutableAttributeError):
        del rel.end_node
예제 #8
0
def test_optional_match():
    person_m = Node('Person', var='m')
    person_n = person_m.copy(var='n')
    rel = Relationship(None, person_m, person_n)
    match = Match(rel, optional=True)
    assert str(match) == 'OPTIONAL MATCH (m)-[rel]->(n)'
예제 #9
0
def test_matching_anonymous_relationship():
    person_m = Node('Person', var='m')
    person_n = person_m.copy(var='n')
    rel = Relationship(None, person_m, person_n)
    match = Match(rel)
    assert str(match) == 'MATCH (m)-[rel]->(n)'