У меня странная проблема с тестами, касающимися тестирования временных изображений и сжатия изображений в models.py. Кажется, проблема с разрешениями:
ERROR: test_has_light_images (realestate.tests.test_view_listing.RealestateListingViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\test\utils.py", line 373, in inner
return func(*args, **kwargs)
File "C:\Users\Storm\Dev\btre_project\realestate\tests\test_view_listing.py", line 72, in test_has_light_images
create_listing(title='listing_sample', address='sample', realtor_num=1, city='sample', state='sample', zipcode='1234', price='555555', bedrooms='1', bathrooms='1', garage='1', sqft='123', lot_size='123', image_sample=image_sample.name)
File "C:\Users\Storm\Dev\btre_project\realestate\tests\test_view_listing.py", line 37, in create_listing
return Listing.objects.create(title=title, address=address, realtor=realtor, city=city, state=state, zipcode=zipcode, price=price, bedrooms=bedrooms, bathrooms=bathrooms, garage=garage, sqft=sqft, lot_size=lot_size, photo_main=image_sample, photo_1=image_sample, photo_2=image_sample, photo_3=image_sample, photo_4=image_sample, photo_5=image_sample, photo_6=image_sample)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "C:\Users\Storm\Dev\btre_project\realestate\models.py", line 69, in save
new_image = compress(self.photo_main)
File "C:\Users\Storm\Dev\btre_project\realestate\models.py", line 11, in compress
im = Image.open(image)
File "C:\Users\Storm\Envs\btre\lib\site-packages\PIL\Image.py", line 2774, in open
fp.seek(0)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\utils.py", line 20, in <lambda>
seek = property(lambda self: self.file.seek)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\db\models\fields\files.py", line 43, in _get_file
self._file = self.storage.open(self.name, 'rb')
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\storage.py", line 36, in open
return self._open(name, mode)
File "C:\Users\Storm\Envs\btre\lib\site-packages\django\core\files\storage.py", line 224, in _open
return File(open(self.path(name), mode))
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Storm\\AppData\\Local\\Temp\\tmp24xoaa7g'
test.py
from django.test import TestCase
from django.urls import reverse, resolve
from django.utils import timezone
import datetime
from ..models import Listing
from django.test import override_settings
from PIL import Image
from io import BytesIO
import tempfile
def get_temporary_image(temp_file):
size = (200, 200)
color = (255, 0, 0, 0)
image = Image.new("RGB", size, color)
image.save(temp_file, 'jpeg')
return temp_file
@override_settings(MEDIA_ROOT=tempfile.gettempdir())
def test_has_light_images(self):
temp_file = tempfile.NamedTemporaryFile()
image_sample = get_temporary_image(temp_file)
Listing.objects.create(title='listing_sample', photo_main=image_sample.name)
models.py
def compress(image):
im = Image.open(image)
im_io = BytesIO()
im.save(im_io, 'JPEG', quality=20, optimize=True)
new_image = File(im_io, name=image.name)
return new_image
class Listing(models.Model):
title = models.CharField(max_length=200)
photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')
def save(self, *args, **kwargs):
new_image = compress(self.photo_main)
self.photo_main = new_image
super().save(*args, **kwargs)
Я пробовал другой способ сжатия изображений, который заставляет тесты работать, но безуспешно при сжатии изображения.
Кто-нибудь знает, что здесь происходит?