gpt4 book ai didi

python - DRF APITestCase 不将 `multipart` 与其他参数一起使用

转载 作者:行者123 更新时间:2023-11-28 22:24:31 25 4
gpt4 key购买 nike

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

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

导入临时文件

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/

最佳答案

在发布到 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)

关于python - DRF APITestCase 不将 `multipart` 与其他参数一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com