示例#1
0
    def get_node(self, node_id: int):
        """
        Send request to database by its API to acquire node with given id

        Args:
            node_id (int): Id by which it is searched for in the database

        Returns:
            Acquired node in NodeOut model
        """
        response = self.db.get_node(node_id)

        if len(response['results'][0]["data"]) == 0:
            return NodeOut(errors="Node not found")

        node = response['results'][0]["data"][0]
        properties = [
            PropertyIn(key=property[0], value=property[1])
            for property in node["row"][0].items()
        ]
        result = NodeOut(id=node_id,
                         properties=properties,
                         labels={node["row"][1][0]})

        return result
示例#2
0
    def get_nodes(self, label: str):
        """
        Send request to database by its API to acquire all nodes with given label

        Args:
            label (str): Label by which it is searched for in the database

        Returns:
            List of acquired nodes in NodesOut model
        """
        response = self.db.get_nodes(label)

        if len(response["errors"]) > 0:
            return NodesOut(errors=response["errors"])

        result = NodesOut(nodes=[])
        for node in response["results"][0]["data"]:
            properties = [
                PropertyIn(key=property[0], value=property[1])
                for property in node["row"][0].items()
            ]
            result.nodes.append(
                BasicNodeOut(labels={label},
                             id=node["meta"][0]["id"],
                             properties=properties))

        return result
    def save_properties(self, id: int, properties: List[PropertyIn]):
        """
        Send request to database by its API to create new properties

        Args:
            id (int): Id of the relationship

            properties (List[PropertyIn]): List of properties for the relationship of given id

        Returns:
            Result of request as relationship object
        """
        if self.db.relationship_exist(id):
            response = self.db.create_relationship_properties(id, properties)
            if len(response["errors"]) > 0:
                result = RelationshipOut(errors=response["errors"])
            else:
                response_data = response["results"][0]["data"][0]["row"]
                response_properties = list((map(
                    lambda property: PropertyIn(key=property[0],
                                                value=property[1]),
                    response_data[3].items())))
                result = RelationshipOut(start_node=response_data[0],
                                         end_node=response_data[2],
                                         name=response_data[1],
                                         id=id,
                                         properties=response_properties)
        else:
            result = RelationshipOut(id=id,
                                     errors={"errors": "not matching id"})

        return result
示例#4
0
    def save_properties(self, id: int, properties: List[PropertyIn]):
        """
        Send request to database by its API to create new properties

        Args:
            id (int): Id of the node
            properties (List[PropertyIn]): List of properties for the node of given id

        Returns:
            Result of request as node object
        """
        if self.db.node_exists(id):
            response = self.db.create_node_properties(id, properties)
            if len(response["errors"]) > 0:
                result = NodeOut(errors=response["errors"])
            else:
                response_data = response["results"][0]["data"][0]["row"]
                response_properties = list((map(
                    lambda property: PropertyIn(key=property[0],
                                                value=property[1]),
                    response_data[1].items())))
                result = NodeOut(labels=set(response_data[0]),
                                 id=id,
                                 properties=response_properties)
        else:
            result = NodeOut(id=id, errors={"errors": "not matching id"})

        return result
    def test_update_experiment_without_error(self, update_experiment_mock):
        experiment_id = 1
        update_experiment_mock.return_value = ExperimentOut(experiment_name="test",
                                                            additional_properties=[PropertyIn(key="test", value="test")],
                                                            id=experiment_id)
        response = Response()
        experiment = ExperimentIn(experiment_name="test", additional_properties=[PropertyIn(key="test", value="test")])
        experiment_router = ExperimentRouter()

        result = asyncio.run(experiment_router.update_experiment(experiment_id, experiment, response))

        self.assertEqual(result, ExperimentOut(experiment_name="test",
                                               additional_properties=[PropertyIn(key="test", value="test")],
                                               id=experiment_id, links=get_links(router)))
        update_experiment_mock.assert_called_once_with(experiment_id, experiment)
        self.assertEqual(response.status_code, 200)
示例#6
0
    def test_update_registered_data_without_error(self,
                                                  update_registered_data_mock):
        registered_data_id = 1
        additional_properties = [PropertyIn(key="test", value="test")]
        update_registered_data_mock.return_value = RegisteredDataOut(
            source='url',
            additional_properties=additional_properties,
            id=registered_data_id)
        response = Response()
        registered_data = RegisteredDataIn(
            source='url', additional_properties=additional_properties)
        registered_data_router = RegisteredDataRouter()

        result = asyncio.run(
            registered_data_router.update_registered_data(
                registered_data_id, registered_data, response))

        self.assertEqual(
            result,
            RegisteredDataOut(source='url',
                              additional_properties=additional_properties,
                              id=registered_data_id,
                              links=get_links(router)))
        update_registered_data_mock.assert_called_once_with(
            registered_data_id, registered_data)
        self.assertEqual(response.status_code, 200)
    def test_get_experiment_with_error(self, get_experiment_mock):
        get_experiment_mock.return_value = ExperimentOut(
            experiment_name="test",
            additional_properties=[PropertyIn(key="test", value="test")],
            errors={'errors': ['test']})
        response = Response()
        experiment_id = 1
        experiment_router = ExperimentRouter()

        result = asyncio.run(
            experiment_router.get_experiment(experiment_id, response))

        self.assertEqual(
            result,
            ExperimentOut(
                experiment_name="test",
                additional_properties=[PropertyIn(key="test", value="test")],
                errors={'errors': ['test']},
                links=get_links(router)))
        get_experiment_mock.assert_called_once_with(experiment_id)
        self.assertEqual(response.status_code, 404)
    def test_create_experiment_with_error(self, save_experiment_mock):
        save_experiment_mock.return_value = ExperimentOut(
            experiment_name="test",
            additional_properties=[PropertyIn(key="test", value="test")],
            errors="error")
        response = Response()
        experiment = ExperimentIn(
            experiment_name="test",
            additional_properties=[PropertyIn(key="test", value="test")])
        experiment_router = ExperimentRouter()

        result = asyncio.run(
            experiment_router.create_experiment(experiment, response))

        self.assertEqual(
            result,
            ExperimentOut(
                experiment_name="test",
                additional_properties=[PropertyIn(key="test", value="test")],
                errors="error",
                links=get_links(router)))
        save_experiment_mock.assert_called_once_with(experiment)
        self.assertEqual(response.status_code, 422)
    def test_create_node_properties(self, requests_mock):
        requests_mock.post.return_value = self.response
        commit_body = {
            "statements": [{
                "statement":
                'MATCH (x) where id(x)=2 SET x.key="value", ' +
                'x.test="Test" return labels(x), x'
            }]
        }
        properties = [
            PropertyIn(key="key", value="value"),
            PropertyIn(key="test", value="Test")
        ]
        object_id = 2

        result = self.database_service.create_node_properties(
            object_id, properties)

        self.assertEqual(result, self.response_content)
        requests_mock.post.assert_called_with(
            url=self.database_service.database_url,
            json=commit_body,
            auth=self.database_service.database_auth)
    def test_get_registered_data_with_error(self, get_registered_data_mock):
        additional_properties = [PropertyIn(key="test", value="test")]
        get_registered_data_mock.return_value = RegisteredDataOut(source='url',
                                                                  additional_properties=additional_properties,
                                                                  errors={'errors': ['test']})
        response = Response()
        registered_data_id = 1
        registered_data_router = RegisteredDataRouter()

        result = asyncio.run(registered_data_router.get_registered_data(registered_data_id, response))

        self.assertEqual(result, RegisteredDataOut(source='url', additional_properties=additional_properties,
                                                   errors={'errors': ['test']},  links=get_links(router)))
        get_registered_data_mock.assert_called_once_with(registered_data_id)
        self.assertEqual(response.status_code, 404)