如何使用Django REST Framework的APIRequestFactory生成文件上载(测试)请求?

时间:2016-11-06 20:06:07

标签: django django-rest-framework django-testing

我开发了一个API(Python 3.5,Django 1.10,DRF 3.4.2),当我从我的UI请求时,它将视频文件上传到我的媒体路径。那部分工作正常。我尝试为此功能编写测试,但无法使其成功运行。

#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)

#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))

在这个测试中,我需要使用现有的视频文件('media / testfiles / vid.mp4')并上传它,因为我需要在视频数据之后测试一些处理:这就是我重置{{1使用MEDIA_ROOT

由于未上传文件,测试失败。在我mkdtemp的{​​{1}}中,当我打印def put时,我得到views.py,当我打印request时,我得到 nothing 。但是,如果我在视图中删除<rest_framework.request.Request object at 0x10f25f048>并在我的测试中使用request.data,那么当我打印FileUploadParser时,我会request = factory.put('file_upload/' + filename, {'filename': filename}, format="multipart")

所以我的结论是我用<QueryDict: {'filename': ['vid']}>生成的请求不正确。 request.data无法从中检索原始文件。

因此我的问题:如何使用Django REST Framework的APIRequestFactory生成文件上传(测试)请求?

有几个人在SO上提出了与此问题相近的问题,但我对提议的答案没有成功。

对此事的任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:5)

现在好了!从APIRequestFactory切换到APIClient,我设法让我的测试运行。

我的新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))

答案 1 :(得分:1)

下面,根据要求(和APIRequestFactory)使用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)


请注意,没有明确的授权,例如:'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.AllowAny']