Beispiel #1
0
    def test_get_measure_names_empty(self, get_nodes_mock):
        get_nodes_mock.return_value = {'nodes': [], 'errors': None}
        measure_names = MeasureNamesOut(measure_names=[])
        measure_name_service = MeasureNameService()

        result = measure_name_service.get_measure_names()

        self.assertEqual(result, measure_names)
        get_nodes_mock.assert_called_once_with("`Measure Name`")
Beispiel #2
0
    def test_get_measure_name_without_error(self, get_node_relationships_mock,
                                            get_node_mock):
        id_node = 1
        get_node_mock.return_value = {
            'id':
            id_node,
            'labels': ['Measure Name'],
            'properties': [{
                'key': 'name',
                'value': 'Familiarity'
            }, {
                'key': 'type',
                'value': 'Addional emotions measure'
            }],
            "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
            }]
        }
        measure_name = MeasureNameOut(
            name="Familiarity",
            type="Addional emotions measure",
            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)
            ])
        measure_name_service = MeasureNameService()

        result = measure_name_service.get_measure_name(id_node)

        self.assertEqual(result, measure_name)
        get_node_mock.assert_called_once_with(id_node)
        get_node_relationships_mock.assert_called_once_with(id_node)
Beispiel #3
0
    def test_get_measure_name_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'])
        measure_name_service = MeasureNameService()

        result = measure_name_service.get_measure_name(id_node)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
Beispiel #4
0
 def set_measure_names(self):
     """
     Initialize values of measure names
     """
     measure_name_service = MeasureNameService()
     created_names = [
         measure_name.name for measure_name in
         measure_name_service.get_measure_names().measure_names
     ]
     [
         measure_name_service.save_measure_name(
             MeasureNameIn(name=measure_name.value[0],
                           type=measure_name.value[1]))
         for measure_name in MeasureName
         if measure_name.value[0] not in created_names
     ]
Beispiel #5
0
    def test_get_measure_name_without_participant_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.")
        measure_name_service = MeasureNameService()

        result = measure_name_service.get_measure_name(id_node)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
Beispiel #6
0
    def test_get_measure_names(self, get_nodes_mock):
        get_nodes_mock.return_value = {
            'nodes': [{
                'id':
                1,
                'labels': ['Measure Name'],
                'properties': [{
                    'key': 'name',
                    'value': 'Familiarity'
                }, {
                    'key': 'type',
                    'value': 'Addional emotions measure'
                }]
            }, {
                'id':
                2,
                'labels': ['Measure Name'],
                'properties': [{
                    'key': 'name',
                    'value': 'Familiarity'
                }, {
                    'key': 'type',
                    'value': 'Addional emotions measure'
                }]
            }],
            'errors':
            None
        }
        measure_name_one = BasicMeasureNameOut(
            name="Familiarity", type="Addional emotions measure", id=1)
        measure_name_two = BasicMeasureNameOut(
            name="Familiarity", type="Addional emotions measure", id=2)
        measure_names = MeasureNamesOut(
            measure_names=[measure_name_one, measure_name_two])
        measure_name_service = MeasureNameService()

        result = measure_name_service.get_measure_names()

        self.assertEqual(result, measure_names)
        get_nodes_mock.assert_called_once_with("`Measure Name`")
class MeasureNameRouter:
    """
    Class for routing measure name based requests

    Attributes:
    measure_name_service (MeasureNameService): Service instance for measure name
    """
    measure_name_service = MeasureNameService()

    @router.get("/measure_names/{measure_name_id}", tags=["measure names"],
                response_model=Union[MeasureNameOut, NotFoundByIdModel])
    async def get_measure_name(self, measure_name_id: int, response: Response):
        """
        Get measure name from database
        """
        get_response = self.measure_name_service.get_measure_name(measure_name_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.get("/measure_names", tags=["measure names"], response_model=MeasureNamesOut)
    async def get_measure_names(self, response: Response):
        """
        Get measure names from database
        """

        get_response = self.measure_name_service.get_measure_names()

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

        return get_response
class MeasureService:
    """
    Object to handle logic of measure requests

    Attributes:
        graph_api_service (GraphApiService): Service used to communicate with Graph API
        measure_name_service (MeasureNameService): Service to manage measure name models
    """
    graph_api_service = GraphApiService()
    measure_name_service = MeasureNameService()

    def save_measure(self, measure: MeasureIn):
        """
        Send request to graph api to create new measure

        Args:
            measure (MeasureIn): Measure to be added

        Returns:
            Result of request as measure object
        """
        node_response = self.graph_api_service.create_node("`Measure`")

        if node_response["errors"] is not None:
            return MeasureOut(**measure.dict(), errors=node_response["errors"])

        measure_id = node_response["id"]

        if measure.measure_name_id is not None and \
                type(self.measure_name_service.get_measure_name(measure.measure_name_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=measure_id,
                end_node=measure.measure_name_id,
                name="hasMeasureName")

        measure.measure_name_id = None
        self.graph_api_service.create_properties(measure_id, measure)

        return self.get_measure(measure_id)

    def get_measures(self):
        """
        Send request to graph api to get measures

        Returns:
            Result of request as list of measures objects
        """
        get_response = self.graph_api_service.get_nodes("`Measure`")

        measures = []

        for measure_node in get_response["nodes"]:
            properties = {'id': measure_node['id']}
            for property in measure_node["properties"]:
                if property["key"] in ["datatype", "range", "unit"]:
                    properties[property["key"]] = property["value"]

            measure = BasicMeasureOut(**properties)
            measures.append(measure)

        return MeasuresOut(measures=measures)

    def get_measure(self, measure_id: int):
        """
        Send request to graph api to get given measure

        Args:
            measure_id (int): Id of measure

        Returns:
            Result of request as measure object
        """
        get_response = self.graph_api_service.get_node(measure_id)

        if get_response["errors"] is not None:
            return NotFoundByIdModel(id=measure_id,
                                     errors=get_response["errors"])
        if get_response["labels"][0] != "Measure":
            return NotFoundByIdModel(id=measure_id, errors="Node not found.")

        measure = {
            'id': get_response['id'],
            'relations': [],
            'reversed_relations': []
        }
        for property in get_response["properties"]:
            if property["key"] in ["datatype", "range", "unit"]:
                measure[property["key"]] = property["value"]

        relations_response = self.graph_api_service.get_node_relationships(
            measure_id)

        for relation in relations_response["relationships"]:
            if relation["start_node"] == measure_id:
                measure['relations'].append(
                    RelationInformation(second_node_id=relation["end_node"],
                                        name=relation["name"],
                                        relation_id=relation["id"]))
            else:
                measure['reversed_relations'].append(
                    RelationInformation(second_node_id=relation["start_node"],
                                        name=relation["name"],
                                        relation_id=relation["id"]))

        return MeasureOut(**measure)

    def delete_measure(self, measure_id: int):
        """
        Send request to graph api to delete given measure

        Args:
            measure_id (int): Id of measure

        Returns:
            Result of request as measure object
        """
        get_response = self.get_measure(measure_id)

        if type(get_response) is NotFoundByIdModel:
            return get_response

        self.graph_api_service.delete_node(measure_id)
        return get_response

    def update_measure(self, measure_id: int, measure: MeasurePropertyIn):
        """
        Send request to graph api to update given measure

        Args:
            measure_id (int): Id of measure
            measure (MeasurePropertyIn): Properties to update

        Returns:
            Result of request as measure object
        """
        get_response = self.get_measure(measure_id)

        if type(get_response) is NotFoundByIdModel:
            return get_response

        self.graph_api_service.delete_node_properties(measure_id)
        self.graph_api_service.create_properties(measure_id, measure)

        measure_result = {
            "id": measure_id,
            "relations": get_response.relations,
            "reversed_relations": get_response.reversed_relations
        }
        measure_result.update(measure.dict())

        return MeasureOut(**measure_result)

    def update_measure_relationships(self, measure_id: int,
                                     measure: MeasureRelationIn):
        """
        Send request to graph api to update given measure

        Args:
            measure_id (int): Id of measure
            measure (MeasureRelationIn): Relationships to update

        Returns:
            Result of request as measure object
        """
        get_response = self.get_measure(measure_id)

        if type(get_response) is NotFoundByIdModel:
            return get_response

        if measure.measure_name_id is not None and \
                type(self.measure_name_service.get_measure_name(
                    measure.measure_name_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=measure_id,
                end_node=measure.measure_name_id,
                name="hasMeasureName")
        return self.get_measure(measure_id)