Example #1
0
    def test_get_time_series_without_error(self, get_node_relationships_mock,
                                           get_node_mock):
        id_node = 1
        get_node_mock.return_value = {
            'id':
            id_node,
            'labels': ['Time Series'],
            'properties': [{
                'key': 'type',
                'value': "Epoch"
            }, {
                'key': 'source',
                'value': "cos"
            }, {
                'key': 'test',
                'value': 'test2'
            }],
            "errors":
            None,
            'links':
            None
        }
        get_node_relationships_mock.return_value = {
            "relationships": [{
                "start_node": id_node,
                "end_node": 19,
                "name": "testRelation",
                "id": 0,
                "properties": None
            }, {
                "start_node": 15,
                "end_node": id_node,
                "name": "testReversedRelation",
                "id": 0,
                "properties": None
            }]
        }
        additional_properties = [PropertyIn(key='test', value='test2')]
        time_series = TimeSeriesOut(
            id=1,
            type="Epoch",
            source="cos",
            additional_properties=additional_properties,
            relations=[
                RelationInformation(second_node_id=19,
                                    name="testRelation",
                                    relation_id=0)
            ],
            reversed_relations=[
                RelationInformation(second_node_id=15,
                                    name="testReversedRelation",
                                    relation_id=0)
            ])
        time_series_service = TimeSeriesService()

        result = time_series_service.get_time_series(id_node)

        self.assertEqual(result, time_series)
        get_node_mock.assert_called_once_with(id_node)
        get_node_relationships_mock.assert_called_once_with(id_node)
    def test_update_time_series_without_error(self, get_node_relationships_mock, delete_node_properties_mock,
                                                    get_node_mock, create_properties_mock):
        id_node = 1
        create_properties_mock.return_value = {}
        delete_node_properties_mock.return_value = {}
        get_node_relationships_mock.return_value = {"relationships": [
                                                    {"start_node": id_node, "end_node": 19,
                                                     "name": "testRelation", "id": 0,
                                                     "properties": None},
                                                    {"start_node": 15, "end_node": id_node,
                                                     "name": "testReversedRelation", "id": 0,
                                                     "properties": None}]}
        get_node_mock.return_value = {'id': id_node, 'labels': ['Time Series'],
                                      'properties': [{'key': 'type', 'value': "Epoch"},
                                                                                     {'key': 'source', 'value': "cos"}, {'key': 'identifier', 'value': 5}],
                                      "errors": None, 'links': None}
        additional_properties = [PropertyIn(key='identifier', value=5)]
        time_series_in = TimeSeriesPropertyIn(type="Epoch", source="cos", additional_properties=additional_properties)
        time_series_out = TimeSeriesOut(type="Epoch", source="cos", id=id_node, relations=
                                 [RelationInformation(second_node_id=19, name="testRelation", relation_id=0)],
                                                    reversed_relations=
                                 [RelationInformation(second_node_id=15, name="testReversedRelation", relation_id=0)],
                                 additional_properties=additional_properties)
        calls = [mock.call(1)]
        time_series_service = TimeSeriesService()

        result = time_series_service.update_time_series(id_node, time_series_in)

        self.assertEqual(result, time_series_out)
        get_node_mock.assert_has_calls(calls)
        create_properties_mock.assert_called_once_with(id_node, time_series_in)
        get_node_relationships_mock.assert_called_once_with(id_node)
Example #3
0
    def test_save_time_series_without_errors(self, get_node_relationships_mock, get_node_mock,
                                                   create_relationships_mock, create_properties_mock, create_node_mock):
        id_node = 1
        get_node_mock.return_value = {'id': id_node, 'labels': ['Time Series'],
                                      'properties': [{'key': 'type', 'value': "Epoch"},
                                                                                     {'key': 'source', 'value': "cos"}],
                                      "errors": None, 'links': None}
        get_node_relationships_mock.return_value = {"relationships": [
                                                    {"start_node": id_node, "end_node": 19,
                                                     "name": "testRelation", "id": 0,
                                                     "properties": None},
                                                    {"start_node": 15, "end_node": id_node,
                                                     "name": "testReversedRelation", "id": 0,
                                                     "properties": None}]}
        create_node_mock.return_value = {'id': id_node, 'properties': None, "errors": None, 'links': None}
        create_properties_mock.return_value = {'id': id_node, 'errors': None, 'links': None}
        create_relationships_mock.return_value = {'start_node': 1, 'end_node': 2,
                                                  'name': 'hasMeasure', 'errors': None}
        time_series_in = TimeSeriesIn(id=1, type="Epoch", source="cos", observable_information_id=2, measure_id=3)
        time_series_out = TimeSeriesOut(id=1, type="Epoch", source="cos", additional_properties=[],
                                                    relations=
                                                    [RelationInformation(second_node_id=19, name="testRelation",
                                                                         relation_id=0)],
                                                    reversed_relations=
                                                    [RelationInformation(second_node_id=15, name="testReversedRelation",
                                                                         relation_id=0)])
        calls = [mock.call(end_node=3, start_node=1, name="hasMeasure")]
        time_series_service = TimeSeriesService()

        result = time_series_service.save_time_series(time_series_in)

        self.assertEqual(result, time_series_out)
        create_node_mock.assert_called_once_with('`Time Series`')
        create_properties_mock.assert_called_once_with(id_node, time_series_in)
        create_relationships_mock.assert_not_called()
Example #4
0
    def test_get_time_series_nodes_empty(self, get_nodes_mock):
        get_nodes_mock.return_value = {'nodes': []}
        time_series_nodes = TimeSeriesNodesOut(time_series=[])
        time_series_nodes_service = TimeSeriesService()

        result = time_series_nodes_service.get_time_series_nodes()

        self.assertEqual(result, time_series_nodes)
        get_nodes_mock.assert_called_once_with("`Time Series`")
Example #5
0
    def test_save_time_series_with_node_error(self, create_node_mock):
        id_node = 1
        create_node_mock.return_value = {'id': id_node, 'properties': None, "errors": ['error'], 'links': None}
        time_series = TimeSeriesIn(type="Epoch", source="cos", observable_information_id=1, measure_id=2)
        time_series_service = TimeSeriesService()

        result = time_series_service.save_time_series(time_series)

        self.assertEqual(result, TimeSeriesOut(type="Epoch", source="cos", errors=['error']))
        create_node_mock.assert_called_once_with('`Time Series`')
    def test_update_time_series_with_error(self, get_node_mock):
        id_node = 1
        get_node_mock.return_value = {'id': id_node, 'errors': ['error'], 'links': None}
        not_found = NotFoundByIdModel(id=id_node, errors=['error'])
        additional_properties = [PropertyIn(key='identifier', value=5)]
        time_series_in = TimeSeriesPropertyIn(type="Epoch", source="cos", id=id_node, additional_properties=additional_properties)
        time_series_service = TimeSeriesService()

        result = time_series_service.update_time_series(id_node, time_series_in)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
Example #7
0
    def test_delete_time_series_with_error(self, get_node_mock):
        id_node = 1
        get_node_mock.return_value = {
            'id': id_node,
            'errors': ['error'],
            'links': None
        }
        not_found = NotFoundByIdModel(id=id_node, errors=['error'])
        time_series_service = TimeSeriesService()

        result = time_series_service.delete_time_series(id_node)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
Example #8
0
    def test_get_time_series_nodes(self, get_nodes_mock):
        get_nodes_mock.return_value = {
            'nodes': [{
                'id':
                1,
                'labels': ['Time Series'],
                'properties': [{
                    'key': 'type',
                    'value': "Epoch"
                }, {
                    'key': 'source',
                    'value': "cos"
                }, {
                    'key': 'test',
                    'value': 'test'
                }]
            }, {
                'id':
                2,
                'labels': ['Time Series'],
                'properties': [{
                    'key': 'type',
                    'value': "Epoch"
                }, {
                    'key': 'source',
                    'value': "cos"
                }, {
                    'key': 'test2',
                    'value': 'test3'
                }]
            }]
        }
        time_series_one = BasicTimeSeriesOut(
            id=1,
            type="Epoch",
            source="cos",
            additional_properties=[PropertyIn(key='test', value='test')])
        time_series_two = BasicTimeSeriesOut(
            id=2,
            type="Epoch",
            source="cos",
            additional_properties=[PropertyIn(key='test2', value='test3')])
        time_series_nodes = TimeSeriesNodesOut(
            time_series_nodes=[time_series_one, time_series_two])
        time_series_nodes_service = TimeSeriesService()

        result = time_series_nodes_service.get_time_series_nodes()

        self.assertEqual(result, time_series_nodes)
        get_nodes_mock.assert_called_once_with("`Time Series`")
Example #9
0
    def test_delete_time_series_without_label(self, get_node_mock):
        id_node = 1
        get_node_mock.return_value = {
            'id': id_node,
            'labels': ['Test'],
            'properties': None,
            "errors": None,
            'links': None
        }
        not_found = NotFoundByIdModel(id=id_node, errors="Node not found.")
        time_series_service = TimeSeriesService()

        result = time_series_service.delete_time_series(id_node)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
class TimeSeriesRouter:
    """
    Class for routing time series based requests

    Attributes:
        time_series_service (TimeSeriesService): Service instance for time series
    """
    time_series_service = TimeSeriesService()

    @router.post("/time_series", tags=["time series"], response_model=TimeSeriesOut)
    async def create_time_series(self, time_series: TimeSeriesIn, response: Response):
        """
        Create time series in database
        """

        create_response = self.time_series_service.save_time_series(time_series)
        if create_response.errors is not None:
            response.status_code = 422

        # add links from hateoas
        create_response.links = get_links(router)

        return create_response

    @router.get("/time_series", tags=["time series"], response_model=TimeSeriesNodesOut)
    async def get_time_series_nodes(self, response: Response):
        """
        Get time series nodes from database
        """

        get_response = self.time_series_service.get_time_series_nodes()

        # add links from hateoas
        get_response.links = get_links(router)

        return get_response

    @router.get("/time_series/{time_series_id}", tags=["time series"],
                response_model=Union[TimeSeriesOut, NotFoundByIdModel])
    async def get_time_series(self, time_series_id: int, response: Response):
        """
        Get time series from database
        """

        get_response = self.time_series_service.get_time_series(time_series_id)
        if get_response.errors is not None:
            response.status_code = 404

        # add links from hateoas
        get_response.links = get_links(router)

        return get_response

    @router.delete("/time_series/{time_series_id}", tags=["time series"],
                   response_model=Union[TimeSeriesOut, NotFoundByIdModel])
    async def delete_time_series(self, time_series_id: int, response: Response):
        """
        Delete time series from database
        """
        get_response = self.time_series_service.delete_time_series(time_series_id)
        if get_response.errors is not None:
            response.status_code = 404

        # add links from hateoas
        get_response.links = get_links(router)

        return get_response

    @router.put("/time_series/{time_series_id}", tags=["time series"],
                response_model=Union[TimeSeriesOut, NotFoundByIdModel])
    async def update_time_series(self, time_series_id: int, time_series: TimeSeriesPropertyIn,
                                       response: Response):
        """
        Update time series model in database
        """
        update_response = self.time_series_service.update_time_series(time_series_id,
                                                                                  time_series)
        if update_response.errors is not None:
            response.status_code = 404

        # add links from hateoas
        update_response.links = get_links(router)

        return update_response

    @router.put("/time_series/{time_series_id}/relationships", tags=["time series"],
                response_model=Union[TimeSeriesOut, NotFoundByIdModel])
    async def update_time_series_relationships(self, time_series_id: int,
                                                     time_series: TimeSeriesRelationIn, response: Response):
        """
        Update time series relations in database
        """
        update_response = self.time_series_service.update_time_series_relationships(time_series_id,
                                                                                                time_series)
        if update_response.errors is not None:
            response.status_code = 404

        # add links from hateoas
        update_response.links = get_links(router)

        return update_response