如何在 Django Rest Framework 上测试图片上传
How to test image upload on Django Rest Framework
我在挣扎。问题出在单元测试 ("test.py") 上,我想出了如何使用 tempfile 和 PIL 上传图像,但这些临时图像永远不会被删除。我考虑制作一个临时目录,然后使用 os.remove 删除 temp_dir,但图像会根据型号上传到不同的媒体目录,所以我真的不知道怎么posttemp_images然后删除。
这是我的models.py
class Noticia(models.Model):
...
img = models.ImageField(upload_to="noticias", storage=OverwriteStorage(), default="noticias/tanque_arma3.jpg")
...
test.py
def temporary_image():
import tempfile
from PIL import Image
image = Image.new('RGB', (100, 100))
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg', prefix="test_img_")
image.save(tmp_file, 'jpeg')
tmp_file.seek(0)
return tmp_file
class NoticiaTest(APITestCase):
def setUp(self):
...
url = reverse('api:noticia-create')
data = {'usuario': usuario.pk, "titulo":"test", "subtitulo":"test", "descripcion":"test", "img": temporary_image()}
response = client.post(url, data,format="multipart")
...
因此,总而言之,问题是,考虑到这些文件必须严格上传到这些目录,我如何从不同的目录中删除临时文件?
为了测试,您可以使用包 dj-inmemorystorage 并且 Django 不会保存到磁盘。序列化程序和模型仍将按预期工作,如果需要,您可以读回数据。
在您的设置中,当您处于测试模式时,覆盖默认文件存储。您还可以在此处放置任何其他“测试模式”设置,只要确保它在您的其他设置之后最后运行即可。
if 'test' in sys.argv :
# store files in memory, no cleanup after tests are finished
DEFAULT_FILE_STORAGE = 'inmemorystorage.InMemoryStorage'
# much faster password hashing, default one is super slow (on purpose)
PASSWORD_HASHERS = ['django.contrib.auth.hashers.MD5PasswordHasher']
上传文件时,您可以使用 SimpleUploadFile
,它完全在内存中。这负责“客户端”,而 dj-inmemorystorage
包负责 Django 的存储。
def temporary_image():
bts = BytesIO()
img = Image.new("RGB", (100, 100))
img.save(bts, 'jpeg')
return SimpleUploadedFile("test.jpg", bts.getvalue())
我在挣扎。问题出在单元测试 ("test.py") 上,我想出了如何使用 tempfile 和 PIL 上传图像,但这些临时图像永远不会被删除。我考虑制作一个临时目录,然后使用 os.remove 删除 temp_dir,但图像会根据型号上传到不同的媒体目录,所以我真的不知道怎么posttemp_images然后删除。
这是我的models.py
class Noticia(models.Model):
...
img = models.ImageField(upload_to="noticias", storage=OverwriteStorage(), default="noticias/tanque_arma3.jpg")
...
test.py
def temporary_image():
import tempfile
from PIL import Image
image = Image.new('RGB', (100, 100))
tmp_file = tempfile.NamedTemporaryFile(suffix='.jpg', prefix="test_img_")
image.save(tmp_file, 'jpeg')
tmp_file.seek(0)
return tmp_file
class NoticiaTest(APITestCase):
def setUp(self):
...
url = reverse('api:noticia-create')
data = {'usuario': usuario.pk, "titulo":"test", "subtitulo":"test", "descripcion":"test", "img": temporary_image()}
response = client.post(url, data,format="multipart")
...
因此,总而言之,问题是,考虑到这些文件必须严格上传到这些目录,我如何从不同的目录中删除临时文件?
为了测试,您可以使用包 dj-inmemorystorage 并且 Django 不会保存到磁盘。序列化程序和模型仍将按预期工作,如果需要,您可以读回数据。
在您的设置中,当您处于测试模式时,覆盖默认文件存储。您还可以在此处放置任何其他“测试模式”设置,只要确保它在您的其他设置之后最后运行即可。
if 'test' in sys.argv :
# store files in memory, no cleanup after tests are finished
DEFAULT_FILE_STORAGE = 'inmemorystorage.InMemoryStorage'
# much faster password hashing, default one is super slow (on purpose)
PASSWORD_HASHERS = ['django.contrib.auth.hashers.MD5PasswordHasher']
上传文件时,您可以使用 SimpleUploadFile
,它完全在内存中。这负责“客户端”,而 dj-inmemorystorage
包负责 Django 的存储。
def temporary_image():
bts = BytesIO()
img = Image.new("RGB", (100, 100))
img.save(bts, 'jpeg')
return SimpleUploadedFile("test.jpg", bts.getvalue())