def test_can_bind_relationship_to_resource(): uri = "http://localhost:7474/db/relationship/1" relationship = Relationship({}, "", {}) relationship.bind(uri) assert relationship.bound assert isinstance(relationship.resource, Resource) assert relationship.resource.uri == uri relationship.unbind() assert not relationship.bound with pytest.raises(BindError): r = relationship.resource
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
def test_can_bind_relationship_to_resource(): uri = "http://localhost:7474/db/relationship/1" metadata = { "start": "http://localhost:7474/db/node/1", "end": "http://localhost:7474/db/node/2", } relationship = Relationship({}, "", {}) # Pass in metadata to avoid callback to server relationship.bind(uri, metadata=metadata) assert relationship.bound assert isinstance(relationship.resource, Resource) assert relationship.resource.uri == uri relationship.unbind() assert not relationship.bound with pytest.raises(BindError): _ = relationship.resource
def test_can_write_relationship_with_properties(): r = Representation() r.write( Relationship({"name": "Fred"}, ("LIVES WITH", { "place": "Bedrock" }), {"name": "Wilma"})) written = repr(r) assert written == '({name:"Fred"})-[:`LIVES WITH` {place:"Bedrock"}]->({name:"Wilma"})'
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
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
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
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
def _create_in_index(self, cls, index, key, value, abstract, query=None): uri = self._uri_for(self._index(cls, index), query=query) if cls is Node: node = Node.cast(abstract) return self.append_post(uri, { "key": key, "value": value, "properties": node.properties, }) elif cls is Relationship: relationship = Relationship.cast(abstract) return self.append_post(uri, { "key": key, "value": value, "start": self._uri_for(abstract.start_node), "type": str(abstract.type), "end": self._uri_for(abstract.end_node), "properties": relationship.properties, }) else: raise TypeError(cls)
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
def _create_in_index(self, cls, index, key, value, abstract, query=None): uri = self._uri_for(self._index(cls, index), query=query) if cls is Node: node = Node.cast(abstract) return self.append_post(uri, { "key": key, "value": value, "properties": node.properties, }) elif cls is Relationship: relationship = Relationship.cast(abstract) return self.append_post( uri, { "key": key, "value": value, "start": self._uri_for(abstract.start_node), "type": str(abstract.type), "end": self._uri_for(abstract.end_node), "properties": relationship.properties, }) else: raise TypeError(cls)
def __init__(self, *type_, **properties): CypherTask.__init__(self) self.__relationship = Relationship(*labels, **properties) self.__return = False
def test_can_write_simple_relationship(): r = Representation() r.write(Relationship({}, "KNOWS", {})) written = repr(r) assert written == "()-[:KNOWS]->()"