DRF APITestCase不能将`multipart`与其他参数一起使用 [英] DRF APITestCase not use `multipart` with other param

查看:51
本文介绍了DRF APITestCase不能将`multipart`与其他参数一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型.首先是 House .第二个是 HouseImage
因此,我必须使用 ForeigneKey
提交图像我可以正常使用REST进行上传,但是无法执行unittest.
之所以继续在这里进行单元测试,是因为我有更多的规格在等我,而且我不确定会进行手动测试.

I have 2 models. First is House. Second is HouseImage
Therefore I have to submit the image with ForeigneKey
I can upload by using the REST normally, but failed to do unittest.
The reason why I keep doing unittest in here because I have a more specifications waiting for me and I would not do hand-test for sure.

django == 1.11.5
djangorestframework == 3.6.4
python3.6.2
x86_64-apple-darwin14.5.0上的PostgreSQL 9.6.5,由Apple LLVM 7.0.0版本(clang-700.1.76)编译,64位

这是我的其他源代码.
https://gist.github.com/elcolie/a013be9c3b7ab5f0cc130e320b19da4b

Here is my additional source code.
https://gist.github.com/elcolie/a013be9c3b7ab5f0cc130e320b19da4b

导入临时文件

from PIL import Image
from django.contrib.auth.models import User
from model_mommy import mommy
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase, APIClient

from soken_web.apps.houses.models import House


class HouseImageTest(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = mommy.make(User, username='Pan')
        self.house = mommy.make(House, location="100.00, 100.00")

    def test_post_image(self):
        self.client.force_authenticate(user=self.user)

        image = Image.new('RGB', (100, 100))

        tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
        image.save(tmp_file)
        data = {
            'image': tmp_file,
            'house': self.house.id,
        }

        response = self.client.post(reverse('api:house_images-list'), data, format='multipart')

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

问题:
服务器向我提出 appliation/json 类型

尝试:
1.将 format = multipart 替换为 content_type/multipart .同样的错误1.使用 format = mulipart content_type/multipart .DRF不允许

Attempts:
1. Replace format=multipart with content_type/multipart. Same error 1. User both format=mulipart and content_type/multipart. It is not permitted by DRF

解决方案:
@zaidfazil非常感谢.你是对的.我必须使用实文件

Solution:
@zaidfazil Thank you very much. You are right. I have to use real-file

import tempfile

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

from soken_web.apps.houses.models import House


class HouseImageTest(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = mommy.make(User, username='Pan')
        self.house = mommy.make(House, location="100.00, 100.00")
        settings.MEDIA_ROOT = tempfile.mkdtemp()

    def test_post_image(self):
        file = File(open('static/rest_framework/img/grid.png', 'rb'))
        uploaded_file = SimpleUploadedFile('new_image.jpg', file.read(), content_type='multipart/form-data')
        data = {
            'image': uploaded_file,
            'houses': self.house.id,
        }

        self.client.force_authenticate(user=self.user)
        response = self.client.post(reverse('api:house_images-list'), data, format='multipart')
        response.render()

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

参考文献:
如何测试二进制文件用django-rest-framework的测试客户端上传?
http://www.django-rest-framework.org/api-guide/testing/

推荐答案

在发布到url之前,您可能需要将文件转换为上传的文件格式,

You may need to convert the file into uploaded file format before posting to the url,

from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.files import File

class HouseImageTest(APITestCase):

    def setUp(self):
        self.client = APIClient()
        self.user = mommy.make(User, username='Pan')
        self.house = mommy.make(House, location="100.00, 100.00")
        settings.MEDIA_ROOT = tempfile.mkdtemp()

    def test_post_image(self):

        image = Image.new('RGB', (100, 100))    
        tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg')
        image.save(tmp_file)

        file = File(tmp_file)
        uploaded_file = SimpleUploadedFile('new_image.jpg', file.read(), content_type='multipart/form-data')

        data = {
            'image': uploaded_file,
            'houses': self.house.id,
        }

        self.client.force_authenticate(user=self.user)
        response = self.client.post(reverse('api:house_images-list'), data, format='multipart')
        response.render()

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

这篇关于DRF APITestCase不能将`multipart`与其他参数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆