Example #1
0
    def test_delete_personality_with_error(self, get_node_relationships_mock,
                                           get_node_mock):
        id_node = 1
        get_node_mock.return_value = {
            'id': id_node,
            'errors': ['error'],
            '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
            }]
        }
        not_found = NotFoundByIdModel(id=id_node, errors=['error'])
        personality_service = PersonalityService()

        result = personality_service.delete_personality(id_node)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
    def test_get_personality_big_five_without_error(self, get_node_relationships_mock, get_node_mock):
        id_node = 1
        get_node_mock.return_value = {'id': id_node, 'labels': ['Personality'],
                                      'properties': [{'key': 'agreeableness', 'value': 2.5},
                                                     {'key': 'conscientiousness', 'value': 2.5},
                                                     {'key': 'extroversion', 'value': 2.5},
                                                     {'key': 'neuroticism', 'value': 2.5},
                                                     {'key': 'openess', 'value': 2.5}],
                                      '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}]}
        personality = PersonalityBigFiveOut(agreeableness=2.5, conscientiousness=2.5,extroversion=2.5, neuroticism=2.5,
                                            openess=2.5, 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)])
        personality_service = PersonalityService()

        result = personality_service.get_personality(id_node)

        self.assertEqual(result, personality)
        get_node_mock.assert_called_once_with(id_node)
        get_node_relationships_mock.assert_called_once_with(id_node)
Example #3
0
    def test_update_personality_panas_without_error(
            self, get_node_relationships_mock, get_node_mock,
            create_properties_mock):
        id_node = 1
        create_properties_mock.return_value = {}
        get_node_mock.return_value = {
            'id':
            id_node,
            'labels': ['Personality'],
            'properties': [{
                'key': 'negative_affect',
                'value': 0.5
            }, {
                'key': 'positive_affect',
                'value': 0.5
            }],
            '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
            }]
        }
        personality_in = PersonalityPanasIn(negative_affect=0.5,
                                            positive_affect=0.5)
        personality_out = PersonalityPanasOut(
            negative_affect=0.5,
            positive_affect=0.5,
            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)
            ])
        personality_service = PersonalityService()

        result = personality_service.update_personality_panas(
            id_node, personality_in)

        self.assertEqual(result, personality_out)
        get_node_mock.assert_called_once_with(id_node)
        create_properties_mock.assert_called_once_with(id_node, personality_in)
Example #4
0
    def test_update_personality_panas_without_appearance_label(
            self, get_node_relationships_mock, get_node_mock):
        id_node = 1
        get_node_mock.return_value = {
            'id': id_node,
            'labels': ['Test'],
            'properties': None,
            "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
            }]
        }
        not_found = NotFoundByIdModel(id=id_node, errors="Node not found.")
        personality_in = PersonalityPanasIn(negative_affect=0.5,
                                            positive_affect=0.5)
        personality_service = PersonalityService()

        result = personality_service.update_personality_panas(
            id_node, personality_in)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
    def test_get_personalities_empty(self, get_nodes_mock):
        get_nodes_mock.return_value = {'nodes': []}
        personalities = PersonalitiesOut(personality=[])
        personality_service = PersonalityService()

        result = personality_service.get_personalities()

        self.assertEqual(result, personalities)
        get_nodes_mock.assert_called_once_with("Personality")
    def test_get_personality_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'])
        personality_service = PersonalityService()

        result = personality_service.get_personality(id_node)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
    def test_get_personality_without_appearance_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.")
        personality_service = PersonalityService()

        result = personality_service.get_personality(id_node)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
Example #8
0
    def test_save_personality_big_five_without_error(self,
                                                     create_properties_mock,
                                                     create_node_mock):
        id_node = 1
        create_node_mock.return_value = {
            'id': id_node,
            'properties': None,
            "errors": None,
            'links': None
        }
        create_properties_mock.return_value = {
            'id':
            id_node,
            'properties': [{
                'key': 'agreeableness',
                'value': 0.5
            }, {
                'key': 'conscientiousness',
                'value': 0.5
            }, {
                'key': 'extroversion',
                'value': 0.5
            }, {
                'key': 'neuroticism',
                'value': 0.5
            }, {
                'key': 'openess',
                'value': 0.5
            }],
            "errors":
            None,
            'links':
            None
        }
        personality = PersonalityBigFiveIn(agreeableness=0.5,
                                           conscientiousness=0.5,
                                           extroversion=0.5,
                                           neuroticism=0.5,
                                           openess=0.5)
        personality_service = PersonalityService()

        result = personality_service.save_personality_big_five(personality)

        self.assertEqual(
            result,
            PersonalityBigFiveOut(agreeableness=0.5,
                                  conscientiousness=0.5,
                                  extroversion=0.5,
                                  neuroticism=0.5,
                                  openess=0.5,
                                  id=id_node))
        create_node_mock.assert_called_once_with('Personality')
        create_properties_mock.assert_called_once_with(id_node, personality)
Example #9
0
    def test_update_personality_big_five_with_wrong_range(self):
        id_node = 1
        personality_in = PersonalityBigFiveIn(agreeableness=2.5,
                                              conscientiousness=2.5,
                                              extroversion=2.5,
                                              neuroticism=2.5,
                                              openess=2.5)
        personality_out = PersonalityBigFiveOut(
            agreeableness=2.5,
            conscientiousness=2.5,
            extroversion=2.5,
            neuroticism=2.5,
            openess=2.5,
            errors="Value not between 0 and 1")
        personality_service = PersonalityService()

        result = personality_service.update_personality_big_five(
            id_node, personality_in)

        self.assertEqual(result, personality_out)
    def test_get_personalities(self, get_nodes_mock):
        get_nodes_mock.return_value = {'nodes': [{'id': 1, 'labels': ['Personality'],
                                                  'properties': [{'key': 'agreeableness', 'value': 2.5},
                                                     {'key': 'conscientiousness', 'value': 2.5},
                                                     {'key': 'extroversion', 'value': 2.5},
                                                     {'key': 'neuroticism', 'value': 2.5},
                                                     {'key': 'openess', 'value': 2.5}]},
                                                 {'id': 2, 'labels': ['Personality'],
                                                  'properties': [{'key': 'negative_affect', 'value': 0.5},
                                                     {'key': 'positive_affect', 'value': 0.5}]}]}
        personality_big_five = BasicPersonalityBigFiveOut(agreeableness=2.5, conscientiousness=2.5, extroversion=2.5,
                                                          neuroticism=2.5, openess=2.5, id=1)
        personality_panas = BasicPersonalityPanasOut(negative_affect=0.5, positive_affect=0.5, id=2)
        personalities = PersonalitiesOut(personalities=[personality_big_five, personality_panas])
        personality_service = PersonalityService()

        result = personality_service.get_personalities()

        self.assertEqual(result, personalities)
        get_nodes_mock.assert_called_once_with("Personality")
Example #11
0
    def test_update_personality_big_five_without_appearance_label(
            self, get_node_relationships_mock, get_node_mock):
        id_node = 1
        get_node_mock.return_value = {
            'id': id_node,
            'labels': ['Test'],
            'properties': None,
            "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
            }]
        }
        not_found = NotFoundByIdModel(id=id_node, errors="Node not found.")
        personality_in = PersonalityBigFiveIn(agreeableness=0.5,
                                              conscientiousness=0.5,
                                              extroversion=0.5,
                                              neuroticism=0.5,
                                              openess=0.5)
        personality_service = PersonalityService()

        result = personality_service.update_personality_big_five(
            id_node, personality_in)

        self.assertEqual(result, not_found)
        get_node_mock.assert_called_once_with(id_node)
Example #12
0
    def test_save_personality_panas_without_error(self, create_properties_mock,
                                                  create_node_mock):
        id_node = 1
        create_node_mock.return_value = {
            'id': id_node,
            'properties': None,
            "errors": None,
            'links': None
        }
        create_properties_mock.return_value = {
            'id':
            id_node,
            'properties': [{
                'key': 'negative_affect',
                'value': 0.5
            }, {
                'key': 'positive_affect',
                'value': 0.5
            }],
            "errors":
            None,
            'links':
            None
        }
        personality = PersonalityPanasIn(negative_affect=0.5,
                                         positive_affect=0.5)
        personality_service = PersonalityService()

        result = personality_service.save_personality_panas(personality)

        self.assertEqual(
            result,
            PersonalityPanasOut(negative_affect=0.5,
                                positive_affect=0.5,
                                id=id_node))
        create_node_mock.assert_called_once_with('Personality')
        create_properties_mock.assert_called_once_with(id_node, personality)
class PersonalityRouter:
    """
    Class for routing personality based requests

    Attributes:
        personality_service (PersonalityService): Service instance for personality
    """
    personality_service = PersonalityService()

    @router.post("/personality/big_five_model",
                 tags=["personality"],
                 response_model=PersonalityBigFiveOut)
    async def create_personality_big_five(self,
                                          personality: PersonalityBigFiveIn,
                                          response: Response):
        """
        Create personality big five model in database
        """

        create_response = self.personality_service.save_personality_big_five(
            personality)
        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.post("/personality/panas_model",
                 tags=["personality"],
                 response_model=PersonalityPanasOut)
    async def create_personality_panas(self, personality: PersonalityPanasIn,
                                       response: Response):
        """
        Create personality panas model in database
        """

        create_response = self.personality_service.save_personality_panas(
            personality)
        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("/personality/{personality_id}",
                tags=["personality"],
                response_model=Union[PersonalityBigFiveOut,
                                     PersonalityPanasOut, NotFoundByIdModel])
    async def get_personality(self, personality_id: int, response: Response):
        """
        Get personality from database
        """

        get_response = self.personality_service.get_personality(personality_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("/personality",
                tags=["personality"],
                response_model=PersonalitiesOut)
    async def get_personalities(self, response: Response):
        """
        Get personalities from database
        """

        get_response = self.personality_service.get_personalities()

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

        return get_response

    @router.delete("/personality/{personality_id}",
                   tags=["personality"],
                   response_model=Union[PersonalityBigFiveOut,
                                        PersonalityPanasOut,
                                        NotFoundByIdModel])
    async def delete_personality(self, personality_id: int,
                                 response: Response):
        """
        Delete personality from database
        """
        get_response = self.personality_service.delete_personality(
            personality_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("/personality/big_five_model/{personality_id}",
                tags=["personality"],
                response_model=Union[PersonalityBigFiveOut, NotFoundByIdModel])
    async def update_personality_big_five(self, personality_id: int,
                                          personality: PersonalityBigFiveIn,
                                          response: Response):
        """
        Update personality big five model in database
        """
        update_response = self.personality_service.update_personality_big_five(
            personality_id, personality)
        if update_response.errors is not None:
            response.status_code = 404 if type(
                update_response) == NotFoundByIdModel else 422

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

        return update_response

    @router.put("/personality/panas_model/{personality_id}",
                tags=["personality"],
                response_model=Union[PersonalityPanasOut, NotFoundByIdModel])
    async def update_personality_panas(self, personality_id: int,
                                       personality: PersonalityPanasIn,
                                       response: Response):
        """
        Update personality panas model in database
        """
        update_response = self.personality_service.update_personality_panas(
            personality_id, personality)
        if update_response.errors is not None:
            response.status_code = 404 if type(
                update_response) == NotFoundByIdModel else 422

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

        return update_response
Example #14
0
class ParticipantStateService:
    """
    Object to handle logic of participant state requests

    Attributes:
        graph_api_service (GraphApiService): Service used to communicate with Graph API
        participant_service (ParticipantService): Service to manage participant models
        appearance_service (AppearanceService): Service to manage appearance models
        personality_service (PersonalityService): Service to manage personality models
    """
    graph_api_service = GraphApiService()
    participant_service = ParticipantService()
    appearance_service = AppearanceService()
    personality_service = PersonalityService()

    def save_participant_state(self, participant_state: ParticipantStateIn):
        """
        Send request to graph api to create new participant state

        Args:
            participant_state (ParticipantStateIn): Participant state to be added

        Returns:
            Result of request as participant state object
        """
        node_response = self.graph_api_service.create_node(
            "`Participant State`")

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

        participant_state_id = node_response["id"]

        if participant_state.participant_id is not None and \
                type(self.participant_service.get_participant(participant_state.participant_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=participant_state_id,
                end_node=participant_state.participant_id,
                name="hasParticipant")
        if participant_state.personality_id is not None and \
                type(self.personality_service.get_personality(participant_state.personality_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=participant_state_id,
                end_node=participant_state.personality_id,
                name="hasPersonality")
        if participant_state.appearance_id is not None and \
                type(self.appearance_service.get_appearance(participant_state.appearance_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=participant_state_id,
                end_node=participant_state.appearance_id,
                name="hasAppearance")

        participant_state.participant_id = participant_state.personality_id = participant_state.appearance_id = None
        self.graph_api_service.create_properties(participant_state_id,
                                                 participant_state)

        return self.get_participant_state(participant_state_id)

    def get_participant_states(self):
        """
        Send request to graph api to get participant states

        Returns:
            Result of request as list of participant states objects
        """
        get_response = self.graph_api_service.get_nodes("`Participant State`")

        participant_states = []

        for participant_state_node in get_response["nodes"]:
            properties = {
                'id': participant_state_node['id'],
                'additional_properties': []
            }
            for property in participant_state_node["properties"]:
                if property["key"] == "age":
                    properties[property["key"]] = property["value"]
                else:
                    properties['additional_properties'].append({
                        'key':
                        property['key'],
                        'value':
                        property['value']
                    })
            participant_state = BasicParticipantStateOut(**properties)
            participant_states.append(participant_state)

        return ParticipantStatesOut(participant_states=participant_states)

    def get_participant_state(self, participant_state_id: int):
        """
        Send request to graph api to get given participant state

        Args:
            participant_state_id (int): Id of participant state

        Returns:
            Result of request as participant state object
        """
        get_response = self.graph_api_service.get_node(participant_state_id)

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

        participant_state = {
            'id': get_response['id'],
            'additional_properties': [],
            'relations': [],
            'reversed_relations': []
        }
        for property in get_response["properties"]:
            if property["key"] == "age":
                participant_state[property["key"]] = property["value"]
            else:
                participant_state['additional_properties'].append({
                    'key':
                    property['key'],
                    'value':
                    property['value']
                })

        relations_response = self.graph_api_service.get_node_relationships(
            participant_state_id)

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

        return ParticipantStateOut(**participant_state)

    def delete_participant_state(self, participant_state_id: int):
        """
        Send request to graph api to delete given participant state

        Args:
            participant_state_id (int): Id of participant state

        Returns:
            Result of request as participant state object
        """
        get_response = self.get_participant_state(participant_state_id)

        if type(get_response) is NotFoundByIdModel:
            return get_response

        self.graph_api_service.delete_node(participant_state_id)
        return get_response

    def update_participant_state(
            self, participant_state_id: int,
            participant_state: ParticipantStatePropertyIn):
        """
        Send request to graph api to update given participant state

        Args:
            participant_state_id (int): Id of participant state
            participant_state (ParticipantStatePropertyIn): Properties to update

        Returns:
            Result of request as participant state object
        """
        get_response = self.get_participant_state(participant_state_id)

        if type(get_response) is NotFoundByIdModel:
            return get_response

        self.graph_api_service.delete_node_properties(participant_state_id)
        self.graph_api_service.create_properties(participant_state_id,
                                                 participant_state)

        participant_state_result = {
            "id": participant_state_id,
            "relations": get_response.relations,
            "reversed_relations": get_response.reversed_relations
        }
        participant_state_result.update(participant_state.dict())

        return ParticipantStateOut(**participant_state_result)

    def update_participant_state_relationships(
            self, participant_state_id: int,
            participant_state: ParticipantStateRelationIn):
        """
        Send request to graph api to update given participant state

        Args:
            participant_state_id (int): Id of participant state
            participant_state (ParticipantStateRelationIn): Relationships to update

        Returns:
            Result of request as participant state object
        """
        get_response = self.get_participant_state(participant_state_id)

        if type(get_response) is NotFoundByIdModel:
            return get_response

        if participant_state.participant_id is not None and \
                type(self.participant_service.get_participant(
                    participant_state.participant_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=participant_state_id,
                end_node=participant_state.participant_id,
                name="hasParticipant")
        if participant_state.personality_id is not None and \
                type(self.personality_service.get_personality(participant_state.personality_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=participant_state_id,
                end_node=participant_state.personality_id,
                name="hasPersonality")
        if participant_state.appearance_id is not None and \
                type(self.appearance_service.get_appearance(participant_state.appearance_id)) is not NotFoundByIdModel:
            self.graph_api_service.create_relationships(
                start_node=participant_state_id,
                end_node=participant_state.appearance_id,
                name="hasAppearance")

        return self.get_participant_state(participant_state_id)