Exemplo n.º 1
0
class UserServiceUnitTest(unittest.TestCase):

    def setUp(self):
        self.stub_user_query_repository = mock.Mock(spec=IUserQueryRepository)
        self.stub_user_command_repository = mock.Mock(spec=IUserCommandRepository)
        self.sut = UserService(self.stub_user_query_repository, self.stub_user_command_repository)

    @data(
        {'email': None, 'password': '******'},
        {'email': '*****@*****.**', 'password': None},
        {'email': None, 'password': None}
    )
    def test_getUserByCredentials_calledWithNoneParams_raiseValueError(self, input):
        self.assertRaises(ValueError, self.sut.get_user_by_credentials,
                          email=input['email'], password=input['password'])

    def test_getUserByCredentials_calledWithQueryRepositoryWhichRaisesQueryError_raiseServiceError(self):
        self.stub_user_query_repository.get_user_by_credentials.side_effect = QueryError()
        self.assertRaises(ServiceError, self.sut.get_user_by_credentials, email='*****@*****.**', password='******')

    def test_getUserByCredentials_calledWithValidParams_innerQueryRepositoryCalledWithValidParams(self):
        self.sut.get_user_by_credentials('*****@*****.**', 'testpwd')
        self.stub_user_query_repository.get_user_by_credentials.assert_called_once_with('*****@*****.**',
                                                                                        'testpwd')

    def test_getUserByAuthToken_calledWithNoneParams_raiseValueError(self):
        self.assertRaises(ValueError, self.sut.get_user_by_auth_token,
                          token=None)

    def test_getUserByAuthToken_calledWithQueryRepositoryWhichRaisesQueryError_raiseServiceError(self):
        self.stub_user_query_repository.get_user_by_auth_token.side_effect = QueryError()
        self.assertRaises(ServiceError, self.sut.get_user_by_auth_token, token='faketoken')

    def test_getUserByAuthToken_calledWithValidParams_innerQueryRepositoryCalledWithValidParams(self):
        self.sut.get_user_by_auth_token('faketoken')
        self.stub_user_query_repository.get_user_by_auth_token.assert_called_once_with('faketoken')

    def test_createUser_calledWithNoneUser_raiseValueError(self):
        self.assertRaises(ValueError, self.sut.create_user, None)

    def test_createUser_calledWithExistentEmail_raiseServiceError(self):
        self.stub_user_query_repository.get_user_by_nickname.return_value = None
        self.stub_user_query_repository.get_user_by_email.return_value = self.__get_user_test_instance()
        self.assertRaises(ServiceError, self.sut.create_user, self.__get_user_test_instance())

    def test_createUser_calledWithCommandRepositoryWhichRaisesCommandError_raiseServiceError(self):
        self.stub_user_command_repository.create_user.side_effect = CommandError()
        self.assertRaises(ServiceError, self.sut.create_user, self.__get_user_test_instance())

    def test_createUser_calledWithValidParams_correctCallToInnerCommandRepositoryWithRightParams(self):
        self.stub_user_query_repository.get_user_by_nickname.return_value = None
        self.stub_user_query_repository.get_user_by_email.return_value = None
        test_user = self.__get_user_test_instance()
        self.sut.create_user(test_user)
        self.stub_user_command_repository.create_user.assert_called_once_with(test_user)

    def test_createUser_calledWithExistentNickname_raiseServiceError(self):
        self.stub_user_query_repository.get_user_by_nickname.return_value = self.__get_user_test_instance()
        self.stub_user_query_repository.get_user_by_email.return_value = None
        self.assertRaises(ServiceError, self.sut.create_user, self.__get_user_test_instance())

    @data(
        {'user_id': None, 'score': 10},
        {'user_id': 'testid', 'score': None},
    )
    def test_incrementUserScore_calledWithNoneParams_raiseValueError(self, input):
        self.assertRaises(ValueError, self.sut.increment_user_score,
                          user_id=input['user_id'], score=input['score'])

    def test_incrementUserScore_calledWithCommandRepositoryWhichRaisesCommandError_raiseServiceError(self):
        self.stub_user_command_repository.increment_user_score.side_effect = CommandError()
        self.assertRaises(ServiceError, self.sut.increment_user_score, user_id='test', score=10)

    def test_incrementUserScore_calledWithValidParams_innerCommandRepositoryCalledWithValidParams(self):
        self.sut.increment_user_score('test', 10)
        self.stub_user_command_repository.increment_user_score.assert_called_once_with('test',
                                                                                       10)

    def test_getRanking_calledWithQueryRepositoryWhichRaisesQueryError_raiseServiceError(self):
        self.stub_user_query_repository.get_ranking.side_effect = QueryError()
        self.assertRaises(ServiceError, self.sut.get_ranking)

    def test_getRanking_called_innerQueryRepositoryCalled(self):
        self.sut.get_ranking()
        self.stub_user_query_repository.get_ranking.assert_called_once_with()

    def __get_user_test_instance(self):
        return User("*****@*****.**", "pwdpwdpwd", "master", "testnickname")
class ExerciseEvaluationService:
    def __init__(self,
                 exercise_evaluation_query_repository=None,
                 exercise_evaluation_command_repository=None,
                 user_service=None):
        if exercise_evaluation_query_repository is not None:
            self.__exercise_evaluation_query_repository = exercise_evaluation_query_repository
        else:
            exercise_evaluation_query_repository_factory = ExerciseEvaluationQueryRepositoryFactory(
            )
            self.__exercise_evaluation_query_repository = exercise_evaluation_query_repository_factory.create_exercise_evaluation_query_repository(
            )

        if exercise_evaluation_command_repository is not None:
            self.__exercise_evaluation_command_repository = exercise_evaluation_command_repository
        else:
            exercise_evaluation_command_repository_factory = ExerciseEvaluationCommandRepositoryFactory(
            )
            self.__exercise_evaluation_command_repository = exercise_evaluation_command_repository_factory.create_exercise_evaluation_command_repository(
            )

        if user_service is not None:
            self.__user_service = user_service
        else:
            self.__user_service = UserService()

    def get_exercise_evaluation(self, user_id, exercise_id):
        if user_id is None:
            raise ValueError('user_id cannot be None')
        if exercise_id is None:
            raise ValueError('exercise_id cannot be None')
        try:
            return self.__exercise_evaluation_query_repository.get_exercise_evaluation(
                user_id, exercise_id)
        except QueryError as qe:
            raise ServiceError(str(qe))

    def create_exercise_evaluation(self, exercise_evaluation):
        if exercise_evaluation is None:
            raise ValueError('Exercise evaluation cannot be None')
        try:
            self.__exercise_evaluation_command_repository.create_exercise_evaluation(
                exercise_evaluation=exercise_evaluation)
        except CommandError as ce:
            raise ServiceError(str(ce))

    def increment_exercise_evaluation_attempts(self, exercise_evaluation):
        if exercise_evaluation is None:
            raise ValueError('Exercise evaluation cannot be None')
        try:
            self.__exercise_evaluation_command_repository.increment_exercise_evaluation_attempts(
                exercise_evaluation=exercise_evaluation)
        except CommandError as ce:
            raise ServiceError(str(ce))

    def update_exercise_evaluation_as_solved(self, user_id, exercise_id,
                                             time_left):
        if user_id is None:
            raise ValueError('user_id cannot be None')
        if exercise_id is None:
            raise ValueError('exercise_id cannot be None')
        if time_left is None:
            raise ValueError('leftover_time cannot be None')
        exercise_evaluation = self.get_exercise_evaluation(
            user_id, exercise_id)
        score = self.__calculate_exercise_evaluation_score(
            exercise_evaluation.get_attempt(), time_left)
        try:
            self.__exercise_evaluation_command_repository.update_exercise_evaluation_as_solved(
                exercise_evaluation, score)
            self.__user_service.increment_user_score(user_id, score)
        except CommandError as ce:
            raise ServiceError(str(ce))

    def __calculate_exercise_evaluation_score(self, attempt, time_left):
        return int(float(time_left) / attempt)