def create(self, abstract): """ Create a node or relationship based on the abstract entity provided. For example:: batch = WriteBatch(graph) a = batch.create(node(name="Alice")) b = batch.create(node(name="Bob")) batch.create(rel(a, "KNOWS", b)) results = batch.submit() :param abstract: node or relationship :type abstract: abstract :return: batch request object """ entity = _cast(abstract, abstract=True) if isinstance(entity, Node): uri = self._uri_for(Resource(self.graph.resource.metadata["node"])) body = entity.properties elif isinstance(entity, Relationship): uri = self._uri_for(entity.start_node, "relationships") body = { "type": entity.type, "to": self._uri_for(entity.end_node) } if entity.properties: body["data"] = entity.properties else: raise TypeError(entity) return self.append_post(uri, body)
def get_or_create(self, rel_abstract): """ Use the abstract supplied to create a new relationship if one does not already exist. :param rel_abstract: relationship abstract to be fetched or created """ rel = _cast(rel_abstract, cls=Relationship, abstract=True) if not (isinstance(rel.start_node, Node) or rel.start_node is None): raise TypeError("Relationship start node must be a " "Node instance or None") if not (isinstance(rel.end_node, Node) or rel.end_node is None): raise TypeError("Relationship end node must be a " "Node instance or None") if rel.start_node and rel.end_node: query = ("START a=node({A}), b=node({B}) " "CREATE UNIQUE (a)-[ab:`" + str(rel.type) + "` {P}]->(b) " "RETURN ab") elif rel.start_node: query = ("START a=node({A}) " "CREATE UNIQUE (a)-[ab:`" + str(rel.type) + "` {P}]->() " "RETURN ab") elif rel.end_node: query = ("START b=node({B}) " "CREATE UNIQUE ()-[ab:`" + str(rel.type) + "` {P}]->(b) " "RETURN ab") else: raise ValueError("Either start node or end node must be " "specified for a unique relationship") params = {"P": compact(rel._properties or {})} if rel.start_node: params["A"] = rel.start_node._id if rel.end_node: params["B"] = rel.end_node._id return self.append_cypher(query, params)
def test_can_cast_3_tuple(self): casted = neo4j._cast(("Alice", "KNOWS", "Bob")) assert isinstance(casted, neo4j.Relationship) assert casted.is_abstract() assert casted.start_node == "Alice" assert casted.type == "KNOWS" assert casted.end_node == "Bob"
def test_can_cast_node(): graph_db = neo4j.GraphDatabaseService() alice, = graph_db.create({"name": "Alice"}) casted = neo4j._cast(alice) assert isinstance(casted, neo4j.Node) assert not casted.is_abstract assert casted["name"] == "Alice"
def test_can_cast_node(self): graph_db = neo4j.GraphDatabaseService() alice, = graph_db.create({"name": "Alice"}) casted = neo4j._cast(alice) assert isinstance(casted, neo4j.Node) assert not casted.is_abstract() assert casted["name"] == "Alice"
def test_can_cast_3_tuple(): casted = neo4j._cast(("Alice", "KNOWS", "Bob")) assert isinstance(casted, neo4j.Relationship) assert casted.is_abstract assert casted.start_node == "Alice" assert casted.type == "KNOWS" assert casted.end_node == "Bob"
def create(self, abstract): """ Create a node or relationship based on the abstract entity provided. For example:: batch = WriteBatch(graph) a = batch.create(node(name="Alice")) b = batch.create(node(name="Bob")) batch.create(rel(a, "KNOWS", b)) results = batch.submit() :param abstract: node or relationship :type abstract: abstract :return: batch request object """ entity = _cast(abstract, abstract=True) if isinstance(entity, Node): uri = self._uri_for(Resource(self.graph.resource.metadata["node"])) body = entity.properties elif isinstance(entity, Relationship): uri = self._uri_for(entity.start_node, "relationships") body = {"type": entity.type, "to": self._uri_for(entity.end_node)} if entity.properties: body["data"] = entity.properties else: raise TypeError(entity) return self.append_post(uri, body)
def test_can_cast_4_tuple(): casted = neo4j._cast(("Alice", "KNOWS", "Bob", {"since": 1999})) assert isinstance(casted, neo4j.Relationship) assert casted.is_abstract assert casted.start_node == "Alice" assert casted.type == "KNOWS" assert casted.end_node == "Bob" assert casted["since"] == 1999
def test_can_cast_rel(graph): a, b, ab = graph.create({}, {}, (0, "KNOWS", 1)) casted = neo4j._cast(ab) assert isinstance(casted, neo4j.Relationship) assert not casted.is_abstract assert casted.start_node == a assert casted.type == "KNOWS" assert casted.end_node == b
def test_can_cast_4_tuple(self): casted = neo4j._cast(("Alice", "KNOWS", "Bob", {"since": 1999})) assert isinstance(casted, neo4j.Relationship) assert casted.is_abstract() assert casted.start_node == "Alice" assert casted.type == "KNOWS" assert casted.end_node == "Bob" assert casted["since"] == 1999
def test_can_cast_rel(self): graph_db = neo4j.GraphDatabaseService() a, b, ab = graph_db.create({}, {}, (0, "KNOWS", 1)) casted = neo4j._cast(ab) assert isinstance(casted, neo4j.Relationship) assert not casted.is_abstract() assert casted.start_node == a assert casted.type == "KNOWS" assert casted.end_node == b
def test_can_cast_rel(): graph_db = neo4j.GraphDatabaseService() a, b, ab = graph_db.create({}, {}, (0, "KNOWS", 1)) casted = neo4j._cast(ab) assert isinstance(casted, neo4j.Relationship) assert not casted.is_abstract assert casted.start_node == a assert casted.type == "KNOWS" assert casted.end_node == b
def test_can_cast_5_tuple(self): casted = neo4j._cast(("Alice", "KNOWS", "Bob", {"Friendship"}, { "since": 1999 })) assert isinstance(casted, neo4j.Relationship) assert casted.is_abstract() assert casted.start_node == "Alice" assert casted.type == "KNOWS" assert casted.end_node == "Bob" assert "Friendship" in casted._labels assert casted["since"] == 1999
def get_or_create(self, rel_abstract): """ Use the abstract supplied to create a new relationship if one does not already exist. :param rel_abstract: relationship abstract to be fetched or created """ rel = _cast(rel_abstract, cls=Relationship, abstract=True) if not (isinstance(rel.start_node, Node) or rel.start_node is None): raise TypeError("Relationship start node must be a " "Node instance or None") if not (isinstance(rel.end_node, Node) or rel.end_node is None): raise TypeError("Relationship end node must be a " "Node instance or None") if rel.start_node and rel.end_node: query = ( "START a=node({A}), b=node({B}) " "CREATE UNIQUE (a)-[ab:`" + str(rel.type) + "` {P}]->(b) " "RETURN ab" ) elif rel.start_node: query = ( "START a=node({A}) " "CREATE UNIQUE (a)-[ab:`" + str(rel.type) + "` {P}]->() " "RETURN ab" ) elif rel.end_node: query = ( "START b=node({B}) " "CREATE UNIQUE ()-[ab:`" + str(rel.type) + "` {P}]->(b) " "RETURN ab" ) else: raise ValueError("Either start node or end node must be " "specified for a unique relationship") params = {"P": compact(rel._properties or {})} if rel.start_node: params["A"] = rel.start_node._id if rel.end_node: params["B"] = rel.end_node._id return self.append_cypher(query, params)
def test_can_cast_dict(self): casted = neo4j._cast({"name": "Alice"}) assert isinstance(casted, neo4j.Node) assert casted.is_abstract() assert casted["name"] == "Alice"
def test_can_cast_dict(): casted = neo4j._cast({"name": "Alice"}) assert isinstance(casted, neo4j.Node) assert casted.is_abstract assert casted["name"] == "Alice"
def test_can_cast_node(graph): alice, = graph.create({"name": "Alice"}) casted = neo4j._cast(alice) assert isinstance(casted, neo4j.Node) assert not casted.is_abstract assert casted["name"] == "Alice"