【问题标题】:DRF APITestCase not use `multipart` with other paramDRF APITestCase 不使用 `multipart` 与其他参数
【发布时间】:2018-02-28 23:42:00
【问题描述】:

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

django==1.11.5
djangorestframework==3.6.4
python3.6.2
PostgreSQL 9.6.5 on x86_64-apple-darwin14.5.0, compiled by Apple LLVM version 7.0.0 (clang-700.1.76), 64-bit

这是我的附加源代码。
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=mulipartcontent_type/multipart。 DRF 不允许这样做

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

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)

参考资料:
How can I test binary file uploading with django-rest-framework's test client?
http://www.django-rest-framework.org/api-guide/testing/

【问题讨论】:

标签: python django rest


【解决方案1】:

您可能需要将文件转换为上传的文件格式,然后才能发布到网址,

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)

【讨论】:

  • 同样的错误。
  • 你设置你的settings.MEDIA_ROOT了吗?
  • 您打印出像print(response.json()) 这样的响应了吗,在那里您可以看到实际引发的错误...另外,您可以为HouseHouseImage 发布models.py 吗?
  • 在您的HouseImage 模型中,field_name 是houses(这是一个必填字段,即NOT NULL),但在您的测试中,它是house。也许这就是错误所在。将house => houses 更改为data
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 2019-05-23
  • 2014-03-23
  • 2019-07-30
  • 2021-11-22
  • 1970-01-01
  • 2015-03-29
相关资源
最近更新 更多