【问题标题】:Testing image uploads in Django REST Framework在 Django REST Framework 中测试图像上传
【发布时间】:2022-03-28 01:17:07
【问题描述】:

我正在尝试向 Django 项目中的一个应用程序添加一些测试。此应用包含Image 模型:

from django.db import models
from django.utils.translation import gettext_lazy as _

from cecommerce.image_mappings import ImageAgnosticMapping
from cecoresizer.fields import ResizableImageField


class Image(models.Model):
    file = ResizableImageField(
        _("Image"),
        max_length=300,
        image_config=ImageAgnosticMapping,
    )

    def __str__(self):
        return str(self.file)

然后使用以下序列化程序对其进行序列化:

from django.http import Http404

from rest_framework import serializers

from cecotec_apps.landings.models import ProductLanding
from cecotec_apps.partner.models import Home

from images.models import Image


class ImageSerializer(serializers.ModelSerializer):
    file = serializers.ImageField()

    def to_representation(self, instance):
        return {"file": str(instance)}

    class Meta:
        model = Image
        exclude = ("id",)

我正在尝试通过此测试来测试 Image 实例的创建(在使用 Imsonia 请求 API 时有效):

import tempfile

from PIL import Image as ImageFile

from django.test import tag

from model_bakery import baker

from rest_framework.test import APITestCase
from rest_framework.authtoken.models import Token
from rest_framework.reverse import reverse


from cecotec_apps.landings.models import ProductLanding
from cecotec_apps.partner.models import Home

from images.api.serializers import ImageSerializer
from images.models import Image

from tests.oscar.decorator.decorator_all_methods import global_test_decorator

from user.models import User


@tag("e2e", "image")
@global_test_decorator()
class ImageTestCase(APITestCase):
    API_VERSION = "v1"
    IMAGES_QUANTITY = 20
    HTTP_HOST = "localhost:8000"

    @classmethod
    def _populate(cls):
        cls.token = baker.make(Token)
        baker.make_recipe("images.image_recipe", _quantity=cls.IMAGES_QUANTITY)

    @classmethod
    def _generate_image_file(cls):
        with tempfile.NamedTemporaryFile(suffix="jpg") as tmp_file:
            image = ImageFile.new("RGB", size=(100, 100))
            image.save(tmp_file, "jpeg")
            tmp_file.seek(0)

            return tmp_file

    @classmethod
    def _depopulate(cls):
        Token.objects.all().delete()
        User.objects.all().delete()
        ProductLanding.objects.all().delete()
        Home.objects.all().delete()
        Image.objects.all().delete()

    def setUp(self, *args):
        self._depopulate()
        self._populate()

    def tearDown(self, *args):
        self._depopulate()

    @tag("create", "authenticated")
    def test_create_authenticated(self, *args):
        self.client.force_authenticate(user=self.token.user, token=self.token)

        data = self._prepare_create_data(**{"file": self._generate_image_file()})

        response = self._call_create(data)

        self._assert_create(response)
        self.assertRegex(response.data.get("file"), "^images\/.+$")

    def _call_create(self, data):
        return self.client.post(
            path=reverse(f"image-list"),
            data=data,
            # content_type=MULTIPART_CONTENT,
            format="multipart",
            HTTP_HOST=self.HTTP_HOST,
        )

    def _assert_create(self, response, expected_status_code=201):
        self.assertEqual(response.status_code, expected_status_code)

        if expected_status_code == 201:
            response_data = response.data

            self.assertEqual(Image.objects.count(), self.IMAGES_QUANTITY + 1)
            self._assert_response_body(response_data)
        else:
            self.assertEqual(Image.objects.count(), self.IMAGES_QUANTITY)

    def _assert_response_body(self, response_body):
        self.assertIn("file", response_body)

但是,运行它时返回的响应代码是 400 Bad Request 而不是 201 Created。响应正文是这样的:

{
    "file": [
        ErrorDetail(string="The submitted data was not a file. Check the encoding type on the form.",
        code="invalid")
    ]
}

我也尝试使用已经创建的文件进行测试,但错误是一样的。我在网上看到了很多帖子,但没有一个可以帮助我解决这个问题。

我将不胜感激。提前致谢。

【问题讨论】:

    标签: python django testing django-rest-framework


    【解决方案1】:

    原因

    我认为问题来自_generate_image_file 方法。

    @classmethod
    def _generate_image_file(cls):
        with tempfile.NamedTemporaryFile(suffix="jpg") as tmp_file:
            image = ImageFile.new("RGB", size=(100, 100))
            image.save(tmp_file, "jpeg")
            tmp_file.seek(0)
    
            return tmp_file
    

    由于tempfile.NamedTemporaryFile 是使用with 语句创建的,所以当您返回它并在其他方法中使用它时,该文件已经关闭并删除。

    解决方案

    您应该通过使用contextlib.contextmanager 修饰方法将_generate_image_file 更改为上下文管理器,并使用yied 语句而不是return。

    from contextlib import contextmanager
    
    @contextmanager
    @classmethod
    def _generate_image_file(cls):
        with tempfile.NamedTemporaryFile(suffix="jpg") as tmp_file:
            image = ImageFile.new("RGB", size=(100, 100))
            image.save(tmp_file, "jpeg")
            tmp_file.seek(0)
    
            yied tmp_file
    

    test_create_authenticated方法上,将其更改为以下

    @tag("create", "authenticated")
    def test_create_authenticated(self, *args):
        ...
        with self._generate_image_file() as image_file:
            data = self._prepare_create_data(**{"file": image_file})
            response = self._call_create(data)
        ...
    

    现在图像文件将在with 块退出后被删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-31
      • 2016-03-23
      • 2014-12-07
      • 2018-01-17
      • 2014-10-16
      • 2018-01-15
      • 2017-10-23
      • 1970-01-01
      相关资源
      最近更新 更多