How to generate a file upload (test) request with Django REST Framework's APIRequestFactory?

主宰稳场 提交于 2021-01-26 11:08:50

问题


I have developed an API (Python 3.5, Django 1.10, DRF 3.4.2) that uploads a video file to my media path when I request it from my UI. That part is working fine. I try to write a test for this feature but cannot get it to run successfully.

#views.py

import os
from rest_framework import views, parsers, response
from django.conf import settings


class FileUploadView(views.APIView):
    parser_classes = (parsers.FileUploadParser,)
    def put(self, request, filename):
        file = request.data['file']
        handle_uploaded_file(file, filename)
        return response.Response(status=204)

def handle_uploaded_file(file, filename):
    dir_name = settings.MEDIA_ROOT + '/scene/' + filename + '/cam1'
    new_filename = 'orig.mp4'
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)
    file_path = os.path.join(dir_name, new_filename)
    with open(file_path, 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)

and

#test.py

import tempfile
import os
from django.test import TestCase
from django.conf import settings
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from rest_framework.test import APIRequestFactory
from myapp.views import FileUploadView


class UploadVideoTestCase(TestCase):
    def setUp(self):
        settings.MEDIA_ROOT = tempfile.mkdtemp(suffix=None, prefix=None, dir=None)

    def test_video_uploaded(self):
        """Video uploaded"""
        filename = 'vid'
        file = File(open('media/testfiles/vid.mp4', 'rb'))
        uploaded_file = SimpleUploadedFile(filename, file.read(), 'video')
        factory = APIRequestFactory()
        request = factory.put('file_upload/'+filename,
            {'file': uploaded_file}, format='multipart')
        view = FileUploadView.as_view()
        response = view(request, filename)
        print(response)

        dir_name = settings.MEDIA_ROOT + '/scene/' + filename + '/cam1'
        new_filename = 'orig.mp4'
        file_path = os.path.join(dir_name, new_filename)
        self.assertTrue(os.path.exists(file_path))

In this test, I need to use an existing video file ('media/testfiles/vid.mp4') and upload it since I need to test some processings on the video data after: that's why I reset the MEDIA_ROOT using mkdtemp.

The test fails since the file is not uploaded. In the def put of my views.py, when I print request I get <rest_framework.request.Request object at 0x10f25f048> and when I print request.data I get nothing. But if I remove the FileUploadParser in my view and use request = factory.put('file_upload/' + filename, {'filename': filename}, format="multipart") in my test, I get <QueryDict: {'filename': ['vid']}> when I print request.data.

So my conclusion is that the request I generate with APIRequestFactory is incorrect. The FileUploadParseris not able to retrieve the raw file from it.

Hence my question: How to generate a file upload (test) request with Django REST Framework's APIRequestFactory?

Several people have asked questions close to this one on SO but I had no success with the proposed answers.

Any help on that matter will be much appreciated!


回答1:


It's alright now! Switching from APIRequestFactory to APIClient, I managed to have my test running.

My new test.py:

import os
import tempfile
from django.conf import settings
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from rest_framework.test import APITestCase, APIClient
from django.contrib.auth.models import User

class UploadVideoTestCase(APITestCase):
    def setUp(self):
        settings.MEDIA_ROOT = tempfile.mkdtemp()
        User.objects.create_user('michel')

    def test_video_uploaded(self):
        """Video uploaded"""
        filename = 'vid'
        file = File(open('media/testfiles/vid.mp4', 'rb'))
        uploaded_file = SimpleUploadedFile(filename, file.read(),
            content_type='multipart/form-data')
        client = APIClient()
        user = User.objects.get(username='michel')
        client.force_authenticate(user=user)
        url = reverse('file_upload:upload_view', kwargs={'filename': filename})
        client.put(url, {'file': uploaded_file}, format='multipart')
        dir_name = settings.MEDIA_ROOT + '/scene/' + filename + '/cam1'
        new_filename = 'orig.mp4'
        file_path = os.path.join(dir_name, new_filename)
        self.assertTrue(os.path.exists(file_path))



回答2:


Below, testing file upload using APIRequestFactory as requested (and ModelViewSet).

from rest_framework.test import APIRequestFactory, APITestCase
from my_project.api.views import MyViewSet
from io import BytesIO

class MyTestCase(APITestCase):

    def setUp(self):
        fd = BytesIO(b'Test File content')   # in-memory file to upload
        fd.seek(0)                           # not needed here, but to remember after writing to fd
        reqfactory = APIRequestFactory()     # initialize in setUp if used by more tests
        view = MyViewSet({'post': 'create'}) # for ViewSet {action:method} needed, for View, not.
        request = factory.post('/api/new_file/',
            {
                "title": 'test file',
                "fits_file": self.fd,
            },
            format='multipart')              # multipart is default, but for clarification that not json
        response = view(request)
        response.render()
        self.assertEqual(response.status_code, 201)


Note that there is no authorization for clarity, as with: 'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.AllowAny'].



来源:https://stackoverflow.com/questions/40453947/how-to-generate-a-file-upload-test-request-with-django-rest-frameworks-apireq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!