Issue
i'm on a struggle. The problem is with the unit testing ("test.py"), and i figured out how to upload images with tempfile and PIL, but those temporary images never get deleted. I think about making a temporary dir and then, with os.remove, delete that temp_dir, but the images upload on different media directorys dependings on the model, so i really don't know how to post temp_images and then delete them.
This is my 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")
...
So, to summarize, the question is, ¿How can i delete a temporary file from different directories, taking into account that those files strictly have to be upload on those directorys?
Solution
For testing you can use the package dj-inmemorystorage and Django will not save to disk. Serializers and models will still work as expected, and you can read the data back out if needed.
In your settings, when you are in test mode, overwrite the default file storage. You can also put any other "test mode" settings in here, just make sure it runs last, after your other settings.
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']
When you are uploading a file you can use SimpleUploadFile
, which is purely in-memory. This takes care of the "client" side, while the dj-inmemorystorage
package takes care of Django's storage.
def temporary_image():
bts = BytesIO()
img = Image.new("RGB", (100, 100))
img.save(bts, 'jpeg')
return SimpleUploadedFile("test.jpg", bts.getvalue())
Answered By - Andrew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.