Ejemplo n.º 1
0
def test_can_create_naked_node(graph):
    node = Node()
    statement = CreateStatement(graph)
    statement.create(node)
    created = statement.execute()
    assert created == (node,)
    assert node.bound
Ejemplo n.º 2
0
def test_can_create_naked_node(graph):
    node = Node()
    statement = CreateStatement(graph)
    statement.create(node)
    created = statement.execute()
    assert created == (node, )
    assert node.bound
Ejemplo n.º 3
0
def test_can_create_node_with_properties(graph):
    node = Node(name="Alice")
    statement = CreateStatement(graph)
    statement.create(node)
    created = statement.execute()
    assert created == (node,)
    assert node.bound
Ejemplo n.º 4
0
def test_can_create_node_with_properties(graph):
    node = Node(name="Alice")
    statement = CreateStatement(graph)
    statement.create(node)
    created = statement.execute()
    assert created == (node, )
    assert node.bound
Ejemplo n.º 5
0
def test_cannot_create_none(graph):
    statement = CreateStatement(graph)
    try:
        statement.create(None)
    except TypeError:
        assert True
    else:
        assert False
Ejemplo n.º 6
0
def test_cannot_create_uncastable_type(graph):
    statement = CreateStatement(graph)
    try:
        statement.create("this is not a valid thing to create")
    except TypeError:
        assert True
    else:
        assert False
Ejemplo n.º 7
0
def test_cannot_create_none(graph):
    statement = CreateStatement(graph)
    try:
        statement.create(None)
    except TypeError:
        assert True
    else:
        assert False
Ejemplo n.º 8
0
def test_cannot_create_uncastable_type(graph):
    statement = CreateStatement(graph)
    try:
        statement.create("this is not a valid thing to create")
    except TypeError:
        assert True
    else:
        assert False
Ejemplo n.º 9
0
def test_can_create_node_with_label(graph):
    if not graph.supports_node_labels:
        return
    node = Node("Person", name="Alice")
    statement = CreateStatement(graph)
    statement.create(node)
    created = statement.execute()
    assert created == (node,)
    assert node.bound
Ejemplo n.º 10
0
def test_cannot_create_unique_node(graph):
    node = Node(name="Alice")
    statement = CreateStatement(graph)
    try:
        statement.create_unique(node)
    except TypeError:
        assert True
    else:
        assert False
Ejemplo n.º 11
0
def test_cannot_create_unique_zero_length_path(graph):
    path = Path(Node())
    statement = CreateStatement(graph)
    try:
        statement.create_unique(path)
    except ValueError:
        assert True
    else:
        assert False
Ejemplo n.º 12
0
def test_cannot_use_a_pointer_that_is_out_of_range(graph):
    broken_relationship = Relationship(10, "KNOWS", 11)
    statement = CreateStatement(graph)
    try:
        statement.create(broken_relationship)
    except IndexError:
        assert True
    else:
        assert False
Ejemplo n.º 13
0
def test_cannot_use_a_pointer_that_is_out_of_range(graph):
    broken_relationship = Relationship(10, "KNOWS", 11)
    statement = CreateStatement(graph)
    try:
        statement.create(broken_relationship)
    except IndexError:
        assert True
    else:
        assert False
Ejemplo n.º 14
0
def test_cannot_create_unique_zero_length_path(graph):
    path = Path(Node())
    statement = CreateStatement(graph)
    try:
        statement.create_unique(path)
    except ValueError:
        assert True
    else:
        assert False
Ejemplo n.º 15
0
def test_cannot_create_unique_node(graph):
    node = Node(name="Alice")
    statement = CreateStatement(graph)
    try:
        statement.create_unique(node)
    except TypeError:
        assert True
    else:
        assert False
Ejemplo n.º 16
0
def test_can_create_node_with_label(graph):
    if not graph.supports_node_labels:
        return
    node = Node("Person", name="Alice")
    statement = CreateStatement(graph)
    statement.create(node)
    created = statement.execute()
    assert created == (node, )
    assert node.bound
Ejemplo n.º 17
0
def test_a_unique_relationship_is_really_unique(graph):
    results = graph.cypher.stream("CREATE (a)-[ab:KNOWS]->(b) RETURN a, ab, b")
    alice, alice_knows_bob, bob = next(results).values
    assert alice.degree == 1
    assert bob.degree == 1
    statement = CreateStatement(graph)
    statement.create_unique(Relationship(alice, "KNOWS", bob))
    statement.execute()
    assert alice.degree == 1
    assert bob.degree == 1
Ejemplo n.º 18
0
def test_can_create_a_relationship_to_two_existing_nodes(graph):
    alice = graph.cypher.execute_one("CREATE (a {name:'Alice'}) RETURN a")
    bob = graph.cypher.execute_one("CREATE (b {name:'Bob'}) RETURN b")
    alice_knows_bob = Relationship(alice, "KNOWS", bob)
    statement = CreateStatement(graph)
    statement.create(alice_knows_bob)
    created = statement.execute()
    assert created == (alice_knows_bob, )
    assert alice_knows_bob.bound
    assert alice_knows_bob.start_node is alice
    assert alice_knows_bob.end_node is bob
Ejemplo n.º 19
0
def test_can_create_a_relationship_to_two_existing_nodes(graph):
    alice = graph.cypher.execute_one("CREATE (a {name:'Alice'}) RETURN a")
    bob = graph.cypher.execute_one("CREATE (b {name:'Bob'}) RETURN b")
    alice_knows_bob = Relationship(alice, "KNOWS", bob)
    statement = CreateStatement(graph)
    statement.create(alice_knows_bob)
    created = statement.execute()
    assert created == (alice_knows_bob,)
    assert alice_knows_bob.bound
    assert alice_knows_bob.start_node is alice
    assert alice_knows_bob.end_node is bob
Ejemplo n.º 20
0
def test_can_pass_entities_that_already_exist(graph):
    results = graph.cypher.stream("CREATE (a)-[ab:KNOWS]->(b) RETURN a, ab, b")
    alice, alice_knows_bob, bob = next(results).values
    statement = CreateStatement(graph)
    statement.create(alice)
    statement.create(bob)
    statement.create(alice_knows_bob)
    created = statement.execute()
    assert created == (alice, bob, alice_knows_bob)
Ejemplo n.º 21
0
def test_cannot_use_a_pointer_that_does_not_refer_to_a_node(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    alice_knows_bob = Relationship(0, "KNOWS", 1)
    broken_relationship = Relationship(0, "KNOWS", 2)
    statement = CreateStatement(graph)
    statement.create(alice)
    statement.create(bob)
    statement.create(alice_knows_bob)
    try:
        statement.create(broken_relationship)
    except ValueError:
        assert True
    else:
        assert False
Ejemplo n.º 22
0
def test_can_pass_entities_that_already_exist(graph):
    results = graph.cypher.stream("CREATE (a)-[ab:KNOWS]->(b) RETURN a, ab, b")
    alice, alice_knows_bob, bob = next(results).values
    statement = CreateStatement(graph)
    statement.create(alice)
    statement.create(bob)
    statement.create(alice_knows_bob)
    created = statement.execute()
    assert created == (alice, bob, alice_knows_bob)
Ejemplo n.º 23
0
def test_unique_path_not_unique_exception(graph):
    results = graph.cypher.stream("CREATE (a)-[ab:KNOWS]->(b), (a)-[:KNOWS]->(b) RETURN a, ab, b")
    alice, alice_knows_bob, bob = next(results).values
    assert alice.degree == 2
    assert bob.degree == 2
    statement = CreateStatement(graph)
    statement.create_unique(Relationship(alice, "KNOWS", bob))
    try:
        statement.execute()
    except CypherError as error:
        assert error.exception == "UniquePathNotUniqueException"
        assert error.fullname in [None, "org.neo4j.cypher.UniquePathNotUniqueException"]
        assert error.statement == ("START _0n0=node({_0n0}),_0n1=node({_0n1})\n"
                                   "CREATE UNIQUE (_0n0)-[_0r0:KNOWS]->(_0n1)\n"
                                   "RETURN _0n0,_0n1,_0r0")
    else:
        assert False
Ejemplo n.º 24
0
def test_can_create_two_nodes_and_a_relationship(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    alice_knows_bob = Relationship(alice, "KNOWS", bob)
    statement = CreateStatement(graph)
    statement.create(alice)
    statement.create(bob)
    statement.create(alice_knows_bob)
    created = statement.execute()
    assert created == (alice, bob, alice_knows_bob)
    assert alice.bound
    assert bob.bound
    assert alice_knows_bob.bound
    assert alice_knows_bob.start_node is alice
    assert alice_knows_bob.end_node is bob
Ejemplo n.º 25
0
def test_cannot_use_a_pointer_that_does_not_refer_to_a_node(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    alice_knows_bob = Relationship(0, "KNOWS", 1)
    broken_relationship = Relationship(0, "KNOWS", 2)
    statement = CreateStatement(graph)
    statement.create(alice)
    statement.create(bob)
    statement.create(alice_knows_bob)
    try:
        statement.create(broken_relationship)
    except ValueError:
        assert True
    else:
        assert False
Ejemplo n.º 26
0
def test_can_create_a_path_with_existing_nodes(graph):
    alice = graph.cypher.execute_one("CREATE (a {name:'Alice'}) RETURN a")
    bob = Node(name="Bob")
    carol = graph.cypher.execute_one("CREATE (c {name:'Carol'}) RETURN c")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    statement = CreateStatement(graph)
    statement.create(path)
    created = statement.execute()
    assert created == (path, )
    assert bob.bound
    assert dave.bound
    ab, cb, cd = path.relationships
    assert ab.start_node is alice
    assert ab.end_node is bob
    assert cb.start_node is carol
    assert cb.end_node is bob
    assert cd.start_node is carol
    assert cd.end_node is dave
Ejemplo n.º 27
0
def test_can_create_a_path_with_existing_nodes(graph):
    alice = graph.cypher.execute_one("CREATE (a {name:'Alice'}) RETURN a")
    bob = Node(name="Bob")
    carol = graph.cypher.execute_one("CREATE (c {name:'Carol'}) RETURN c")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    statement = CreateStatement(graph)
    statement.create(path)
    created = statement.execute()
    assert created == (path,)
    assert bob.bound
    assert dave.bound
    ab, cb, cd = path.relationships
    assert ab.start_node is alice
    assert ab.end_node is bob
    assert cb.start_node is carol
    assert cb.end_node is bob
    assert cd.start_node is carol
    assert cd.end_node is dave
Ejemplo n.º 28
0
def test_can_create_an_entirely_new_path(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    statement = CreateStatement(graph)
    statement.create(path)
    created = statement.execute()
    assert created == (path, )
    assert alice.bound
    assert bob.bound
    assert carol.bound
    assert dave.bound
    ab, cb, cd = path.relationships
    assert ab.start_node is alice
    assert ab.end_node is bob
    assert cb.start_node is carol
    assert cb.end_node is bob
    assert cd.start_node is carol
    assert cd.end_node is dave
Ejemplo n.º 29
0
def test_can_create_an_entirely_new_path(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    carol = Node(name="Carol")
    dave = Node(name="Dave")
    path = Path(alice, "LOVES", bob, Rev("HATES"), carol, "KNOWS", dave)
    statement = CreateStatement(graph)
    statement.create(path)
    created = statement.execute()
    assert created == (path,)
    assert alice.bound
    assert bob.bound
    assert carol.bound
    assert dave.bound
    ab, cb, cd = path.relationships
    assert ab.start_node is alice
    assert ab.end_node is bob
    assert cb.start_node is carol
    assert cb.end_node is bob
    assert cd.start_node is carol
    assert cd.end_node is dave
Ejemplo n.º 30
0
def test_can_create_two_nodes_and_a_relationship(graph):
    alice = Node(name="Alice")
    bob = Node(name="Bob")
    alice_knows_bob = Relationship(alice, "KNOWS", bob)
    statement = CreateStatement(graph)
    statement.create(alice)
    statement.create(bob)
    statement.create(alice_knows_bob)
    created = statement.execute()
    assert created == (alice, bob, alice_knows_bob)
    assert alice.bound
    assert bob.bound
    assert alice_knows_bob.bound
    assert alice_knows_bob.start_node is alice
    assert alice_knows_bob.end_node is bob
Ejemplo n.º 31
0
def test_a_unique_relationship_is_really_unique(graph):
    results = graph.cypher.stream("CREATE (a)-[ab:KNOWS]->(b) RETURN a, ab, b")
    alice, alice_knows_bob, bob = next(results).values
    assert alice.degree == 1
    assert bob.degree == 1
    statement = CreateStatement(graph)
    statement.create_unique(Relationship(alice, "KNOWS", bob))
    statement.execute()
    assert alice.degree == 1
    assert bob.degree == 1
Ejemplo n.º 32
0
def test_unique_path_not_unique_exception(graph):
    results = graph.cypher.stream(
        "CREATE (a)-[ab:KNOWS]->(b), (a)-[:KNOWS]->(b) RETURN a, ab, b")
    alice, alice_knows_bob, bob = next(results).values
    assert alice.degree == 2
    assert bob.degree == 2
    statement = CreateStatement(graph)
    statement.create_unique(Relationship(alice, "KNOWS", bob))
    try:
        statement.execute()
    except CypherError as error:
        assert error.exception == "UniquePathNotUniqueException"
        assert error.fullname in [
            None, "org.neo4j.cypher.UniquePathNotUniqueException"
        ]
        assert error.statement == (
            "START _0n0=node({_0n0}),_0n1=node({_0n1})\n"
            "CREATE UNIQUE (_0n0)-[_0r0:KNOWS]->(_0n1)\n"
            "RETURN _0n0,_0n1,_0r0")
    else:
        assert False
Ejemplo n.º 33
0
def test_statement_representation_returns_cypher(graph):
    node = Node()
    statement = CreateStatement(graph)
    statement.create(node)
    rep = repr(statement)
    assert rep == 'CREATE (_0)\nRETURN _0'
Ejemplo n.º 34
0
def test_empty_statement_returns_empty_tuple(graph):
    statement = CreateStatement(graph)
    created = statement.execute()
    assert created == ()
Ejemplo n.º 35
0
    }),
    Relationship(nodes["a"], "EDGE", nodes["d"])
]

# Relationship(nodes["b"], "RELATIONSHIP", nodes["c"]),
# Relationship(nodes["b"], "RELATIONSHIP", nodes["d"]),
# Relationship(nodes["b"], "RELATIONSHIP", nodes["e"]),
# Relationship(nodes["c"], "RELATIONSHIP", nodes["e"]),
# Relationship(nodes["d"], "RELATIONSHIP", nodes["c"]),
# Relationship(nodes["d"], "RELATIONSHIP", nodes["f"]),
# Relationship(nodes["e"], "RELATIONSHIP", nodes["g"]),
#      Relationship(nodes["e"], "RELATIONSHIP", nodes["f"]),
#      Relationship(nodes["f"], "RELATIONSHIP", nodes["g"]),
#      Relationship(nodes["g"], "RELATIONSHIP", nodes["a"])]

builder = CreateStatement(graph)

# graph.create(nodeDict.values())
for node in nodes.values():
    builder.create(node)
for edge in edges:
    builder.create(edge)
builder.execute()


def edge_query(source, target):
    return (
        "MATCH (source:NETWORK_NODE {id: '" + source + "'}) "
        "MATCH (target:NETWORK_NODE {id: '" + target + "'}) "
        "MERGE (source)-[rel:EDGE]->(target) "
        "ON CREATE SET rel._isSetEdge=true, rel.pathways=[\"hsa02\"], rel.try=true "
Ejemplo n.º 36
0
def test_empty_statement_returns_empty_tuple(graph):
    statement = CreateStatement(graph)
    created = statement.execute()
    assert created == ()
Ejemplo n.º 37
0
    Relationship(nodes["a"], "EDGE", nodes["b"], **{"_isNetworkEdge": True, "pathways": ["hsa01"]}),
    Relationship(nodes["a"], "EDGE", nodes["d"]),
]
# ,
# Relationship(nodes["b"], "RELATIONSHIP", nodes["c"]),
# Relationship(nodes["b"], "RELATIONSHIP", nodes["d"]),
# Relationship(nodes["b"], "RELATIONSHIP", nodes["e"]),
# Relationship(nodes["c"], "RELATIONSHIP", nodes["e"]),
# Relationship(nodes["d"], "RELATIONSHIP", nodes["c"]),
# Relationship(nodes["d"], "RELATIONSHIP", nodes["f"]),
# Relationship(nodes["e"], "RELATIONSHIP", nodes["g"]),
#      Relationship(nodes["e"], "RELATIONSHIP", nodes["f"]),
#      Relationship(nodes["f"], "RELATIONSHIP", nodes["g"]),
#      Relationship(nodes["g"], "RELATIONSHIP", nodes["a"])]

builder = CreateStatement(graph)

# graph.create(nodeDict.values())
for node in nodes.values():
    builder.create(node)
for edge in edges:
    builder.create(edge)
builder.execute()


def edge_query(source, target):
    return (
        "MATCH (source:NETWORK_NODE {id: '" + source + "'}) "
        "MATCH (target:NETWORK_NODE {id: '" + target + "'}) "
        "MERGE (source)-[rel:EDGE]->(target) "
        'ON CREATE SET rel._isSetEdge=true, rel.pathways=["hsa02"], rel.try=true '
Ejemplo n.º 38
0
def test_statement_representation_returns_cypher(graph):
    node = Node()
    statement = CreateStatement(graph)
    statement.create(node)
    rep = repr(statement)
    assert rep == 'CREATE (_0)\nRETURN _0'