def test_csv_data_with_good_resource(mock_get_path_to_resource_file, mock_csv_data_from_file, mock_open):
    """Test csv_data() with a good resource that has an uploaded CSV file.

    When given a resource dict for which get_path_to_resource_file() returns a
    path to a file, csv_data() should return the result of calling
    _csv_data_from_file() and passing it the path.

    """
    # Make each of the mocked functions return constant values.
    mock_get_path_to_resource_file.return_value = "path to resource file"
    mock_csv_data_from_file.return_value = "csv data"
    mock_open.return_value = "file data"

    mock_resource_dict = mock.Mock()

    result = helpers.csv_data(mock_resource_dict)

    # csv_data() should call get_path_to_resource_file() once, passing it the
    # resource dict.
    mock_get_path_to_resource_file.assert_called_once_with(mock_resource_dict)

    # csv_data() should call _csv_data_from_file() passing it the opened file.
    mock_csv_data_from_file.assert_called_once_with(mock_open.return_value)

    assert result == mock_csv_data_from_file.return_value, "csv_data() should return what _csv_data_from_file() returns"
Exemple #2
0
def test_csv_data_with_good_resource(mock_get_path_to_resource_file,
                                     mock_csv_data_from_file, mock_open):
    '''Test csv_data() with a good resource that has an uploaded CSV file.

    When given a resource dict for which get_path_to_resource_file() returns a
    path to a file, csv_data() should return the result of calling
    _csv_data_from_file() and passing it the path.

    '''
    # Make each of the mocked functions return constant values.
    mock_get_path_to_resource_file.return_value = 'path to resource file'
    mock_csv_data_from_file.return_value = 'csv data'
    mock_open.return_value = 'file data'

    mock_resource_dict = mock.Mock()

    result = helpers.csv_data(mock_resource_dict)

    # csv_data() should call get_path_to_resource_file() once, passing it the
    # resource dict.
    mock_get_path_to_resource_file.assert_called_once_with(
        mock_resource_dict)

    # csv_data() should call _csv_data_from_file() passing it the opened file.
    mock_csv_data_from_file.assert_called_once_with(
        mock_open.return_value)

    assert result == mock_csv_data_from_file.return_value, (
        "csv_data() should return what _csv_data_from_file() returns")
def test_csv_data_with_bad_resource(mock_get_path_to_resource_file):
    """When called with a resource dict for which get_path_to_resource_file()
    raises ResourceFileDoesNotExistException,  csv_data() should return a
    dict with 'success': False and an error message.

    """
    mock_get_path_to_resource_file.side_effect = exceptions.ResourceFileDoesNotExistException

    result = helpers.csv_data({})

    assert result == {"success": False, "error": "There's no uploaded file for this resource"}
Exemple #4
0
def test_csv_data_with_bad_resource(mock_get_path_to_resource_file):
    '''When called with a resource dict for which get_path_to_resource_file()
    raises ResourceFileDoesNotExistException,  csv_data() should return a
    dict with 'success': False and an error message.

    '''
    mock_get_path_to_resource_file.side_effect = (
        exceptions.ResourceFileDoesNotExistException)

    result = helpers.csv_data({})

    assert result == {'success': False,
                      'error': "There's no uploaded file for this resource"}