简体   繁体   中英

Django REST UnitTest No file was submitted

I successfully implement with small cases. Then I started to work with bigger structure. And I got the error.
Error:
No file was submitted.

import tempfile
from unittest import skip

from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from model_mommy import mommy
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase, APIClient


class CustomerFromExcelViewsetTest(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.soken_staff = mommy.make(User, username='spearhead')
        self.user = mommy.make(User, username='Justin')
        settings.MEDIA_ROOT = tempfile.mkdtemp()

    def test_upload_file(self):
        """Expect created_user, and updated_user correct set"""
        file = File(open('./soken_web/apps/uploaded_files/complete-customer.xlsx', 'rb'))
        uploaded_file = SimpleUploadedFile('new_excel.xlsx', file.read(), content_type='multipart/form-data')
        data = {
            file: uploaded_file,
        }
        self.client.force_authenticate(user=self.user)
        response = self.client.post(reverse('api:customer_from_excel-list'), data, format='multipart')
        response.render()

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)

Here they are the models , serializers , and viewsets

models.py
https://gist.github.com/elcolie/52daf2bd144af82b348f7353656be434

serializers.py
https://gist.github.com/elcolie/7f097642c4a752e76044c6938c49e097

viewsets.py
https://gist.github.com/elcolie/34fa66632209f14624899d997919d3fb

After a day I could not figure out where is the that bug.

References:
DRF APITestCase not use `multipart` with other param

looks like you missed quotes in data dict. It should be:

data = {
    'file': uploaded_file,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM