Exemplo n.º 1
0
    def test_given_invalid_mandal_id_raises_exception(self, get_user_dto):

        # Arrange
        invalid_mandal_id = -1
        user_id = 1
        for_date = "2020/05/27"
        total_confirmed = 10
        total_recovered = 2
        total_deaths = 3
        mandal_storage = create_autospec(MandalStorageInterface)
        presenter = create_autospec(PresenterInterface)
        get_user_dto.return_value = UserDetailsDTO(user_id=1, is_admin=True)
        mandal_storage.is_valid_mandal_id.side_effect = InvalidMandal
        presenter.raise_exception_for_invalid_mandal_id.side_effect = \
            BadRequest

        interactor = CreateOrUpdateMandalStatisticsInteractor(
            mandal_storage=mandal_storage, presenter=presenter)

        # Act
        with pytest.raises(BadRequest):
            interactor.create_or_update_mandal_statistics(
                user_id=user_id,
                for_date=for_date,
                total_confirmed=total_confirmed,
                total_recovered=total_recovered,
                total_deaths=total_deaths,
                mandal_id=invalid_mandal_id)

        # Assert
        mandal_storage.is_valid_mandal_id.assert_called_once_with(
            mandal_id=invalid_mandal_id)
        presenter.raise_exception_for_invalid_mandal_id.assert_called_once()
Exemplo n.º 2
0
    def test_given_invalid_admin_raises_exception(self, get_user_dto):

        # Arrange
        invalid_user_admin = 1
        mandal_id = 1
        for_date = "2020/05/27"
        total_confirmed = 10
        total_recovered = 2
        total_deaths = 3
        mandal_storage = create_autospec(MandalStorageInterface)
        presenter = create_autospec(PresenterInterface)
        get_user_dto.return_value = UserDetailsDTO(user_id=1, is_admin=False)
        presenter.raise_exception_for_invalid_user_admin.side_effect = \
            Forbidden

        interactor = CreateOrUpdateMandalStatisticsInteractor(
            mandal_storage=mandal_storage, presenter=presenter)

        # Act
        with pytest.raises(Forbidden):
            interactor.create_or_update_mandal_statistics(
                user_id=invalid_user_admin,
                for_date=for_date,
                total_confirmed=total_confirmed,
                total_recovered=total_recovered,
                total_deaths=total_deaths,
                mandal_id=mandal_id)

        # Assert
        presenter.raise_exception_for_invalid_user_admin.assert_called_once()
Exemplo n.º 3
0
def test_given_invalid_admin_raises_exception(get_user_dto):

    # Arrange
    invalid_user_admin = 1
    storage = create_autospec(MandalStorageInterface)
    presenter = create_autospec(PresenterInterface)
    presenter.raise_exception_for_invalid_user_admin.side_effect = \
        Forbidden
    get_user_dto.return_value = UserDetailsDTO(user_id=1, is_admin=False)
    interactor = DailyStatisticsInteractor(storage=storage,
                                           presenter=presenter)

    # Act
    with pytest.raises(Forbidden):
        interactor.get_daily_statistics(user_id=invalid_user_admin)

    # Assert
    presenter.raise_exception_for_invalid_user_admin.assert_called_once()
Exemplo n.º 4
0
    def test_given_positive_number_or_zero_as_input_creates_mandal_statistics(
            self, get_user_dto, user_id, mandal_id, total_confirmed,
            total_recovered, total_deaths, for_date):

        # Arrange
        mandal_id = mandal_id
        for_date = for_date
        total_confirmed = total_confirmed
        total_recovered = total_recovered
        total_deaths = total_deaths
        user_details_dto = UserDetailsDTO(user_id=1, is_admin=True)
        mandal_storage = create_autospec(MandalStorageInterface)
        presenter = create_autospec(PresenterInterface)
        mandal_storage.is_mandal_stats_exists.side_effect = \
            InvalidMandalStatistics

        get_user_dto.return_value = user_details_dto
        interactor = CreateOrUpdateMandalStatisticsInteractor(
            mandal_storage=mandal_storage, presenter=presenter)

        # Act
        interactor.create_or_update_mandal_statistics(
            user_id=user_id,
            for_date=for_date,
            total_confirmed=total_confirmed,
            total_recovered=total_recovered,
            total_deaths=total_deaths,
            mandal_id=mandal_id)

        # Assert
        mandal_storage.is_valid_mandal_id.assert_called_once_with(
            mandal_id=mandal_id)
        mandal_storage.is_mandal_stats_exists.assert_called_once_with(
            mandal_id=mandal_id, for_date=for_date)
        mandal_storage.create_mandal_statistics.assert_called_once_with(
            for_date=for_date,
            total_confirmed=total_confirmed,
            total_recovered=total_recovered,
            total_deaths=total_deaths,
            mandal_id=mandal_id)
Exemplo n.º 5
0
    def test_given_update_mandal_stats_if_already_exists_results_update_reaction(
            self, get_user_dto):

        # Arrange
        mandal_id = 1
        user_id = 1
        for_date = "2020/05/27"
        total_confirmed = 10
        total_recovered = 2
        total_deaths = 3
        get_user_dto.return_value = UserDetailsDTO(user_id=1, is_admin=True)
        mandal_storage = create_autospec(MandalStorageInterface)
        presenter = create_autospec(PresenterInterface)
        mandal_storage.is_mandal_stats_exists.return_value = True

        interactor = CreateOrUpdateMandalStatisticsInteractor(
            mandal_storage=mandal_storage, presenter=presenter)

        # Act
        interactor.create_or_update_mandal_statistics(
            user_id=user_id,
            for_date=for_date,
            total_confirmed=total_confirmed,
            total_recovered=total_recovered,
            total_deaths=total_deaths,
            mandal_id=mandal_id)

        # Assert
        mandal_storage.is_valid_mandal_id.assert_called_once_with(
            mandal_id=mandal_id)
        mandal_storage.is_mandal_stats_exists.assert_called_once_with(
            mandal_id=mandal_id, for_date=for_date)
        mandal_storage.update_mandal_statistics.assert_called_once_with(
            for_date=for_date,
            total_confirmed=total_confirmed,
            total_recovered=total_recovered,
            total_deaths=total_deaths,
            mandal_id=mandal_id)
Exemplo n.º 6
0
def test_get_daily_statistics_with_valid_user_details_returns_statistics_dict(
        get_user_dto, get_daily_statistics_response,
        get_daily_statistics_dtos):

    # Arrange
    user_id = 1
    expected_dict = get_daily_statistics_response
    storage = create_autospec(MandalStorageInterface)
    presenter = create_autospec(PresenterInterface)
    get_user_dto.return_value = UserDetailsDTO(user_id=1, is_admin=False)
    storage.get_daily_statistics.return_value = get_daily_statistics_dtos
    presenter.get_daily_statistics_response.return_value = \
        get_daily_statistics_response
    interactor = DailyStatisticsInteractor(storage=storage,
                                           presenter=presenter)

    # Act
    response = interactor.get_daily_statistics(user_id=user_id)

    # Assert
    assert expected_dict == response
    storage.get_daily_statistics.assert_called_once()
    presenter.get_daily_statistics_response.assert_called_once_with(
        daily_statistics_dtos=get_daily_statistics_dtos)
 def _convert_user_object_to_dto(user):
     return UserDetailsDTO(user_id=user.id, is_admin=user.is_admin)
Exemplo n.º 8
0
def user_dto():
    user_dto = UserDetailsDTO(
        user_id=1,
        is_admin=False
    )
    return user_dto