def post(request, format='csv'):  # pylint: disable=redefined-builtin, unused-argument
        file_obj = request.FILES.get('file')
        if not file_obj:
            return Response(status=status.HTTP_422_UNPROCESSABLE_ENTITY,
                            data={'message': 'No file uploaded.'})
        # do some stuff with uploaded file
        result = import_csv(file_obj)

        if result['successful_imports'] > 0:
            response_status = status.HTTP_200_OK
        else:
            response_status = status.HTTP_422_UNPROCESSABLE_ENTITY

        return Response(status=response_status, data=result)
def test_csv_import_with_errors_when_not_all_fields_on_second_line():
    with open(ERROR_FILE_3, 'r') as error_file:
        result = import_csv(error_file)
        assert result == {
            'successful_imports':
            0,
            'total_errors':
            1,
            'imported': [],
            'errors': [
                f'Line 2 does not have all expected fields. '
                f'Remember the CSV separator must be '
                f'"{CSV_SEPARATOR}".'
            ]
        }
def test_csv_import_with_errors_when_not_expected_columns():
    with open(ERROR_FILE_1, 'r') as error_file:
        result = import_csv(error_file)
        assert result == {
            'successful_imports':
            0,
            'total_errors':
            1,
            'imported': [],
            'errors': [
                "The CSV does not have all expected columns, "
                "which are: brand model color material "
                "cost_price sell_price taxes"
            ]
        }
def test_csv_import_with_errors_when_columns_not_expected_order():
    with open(ERROR_FILE_2, 'r') as error_file:
        result = import_csv(error_file)
        assert result == {
            'successful_imports':
            0,
            'total_errors':
            1,
            'imported': [],
            'errors': [
                "The CSV does not have columns "
                "on expected order, which is: "
                "brand model color material "
                "cost_price sell_price taxes"
            ]
        }
def test_csv_import_with_multiple_lines_success():
    with open(SUCCESS_FILE_4, 'r') as success_file:
        result = import_csv(success_file)
        assert Pants.objects.count() == 11
        assert result['successful_imports'] == 11
        assert result['total_errors'] == 1
def test_csv_import_with_success_when_double_quotes_on_fields(file_name):  # pylint: disable=unused-argument
    with open(SUCCESS_FILE_1, 'r') as success_file:
        result = import_csv(success_file)
        assert Pants.objects.count() > 0
        assert result['successful_imports'] == 1
        assert result['total_errors'] == 0