mirror of
https://github.com/ae-utbm/sith.git
synced 2026-05-03 03:46:08 +00:00
test: Picture.generate_thumbnails
This commit is contained in:
+13
-2
@@ -1,13 +1,24 @@
|
||||
from django.conf import settings
|
||||
from model_bakery import seq
|
||||
from model_bakery.recipe import Recipe
|
||||
from model_bakery.recipe import Recipe, foreign_key
|
||||
|
||||
from sas.models import Picture
|
||||
from sas.models import Album, Picture
|
||||
|
||||
album_recipe = Recipe(
|
||||
Album,
|
||||
is_in_sas=True,
|
||||
is_folder=True,
|
||||
is_moderated=True,
|
||||
parent_id=settings.SITH_SAS_ROOT_DIR_ID,
|
||||
name=seq("Album "),
|
||||
)
|
||||
|
||||
picture_recipe = Recipe(
|
||||
Picture,
|
||||
is_in_sas=True,
|
||||
is_folder=False,
|
||||
is_moderated=True,
|
||||
parent=foreign_key(album_recipe),
|
||||
name=seq("Picture "),
|
||||
)
|
||||
"""A SAS Picture fixture.
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from typing import ClassVar, Self
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from django.core.files.base import ContentFile
|
||||
from django.test import TestCase
|
||||
from model_bakery import baker
|
||||
from PIL import Image
|
||||
|
||||
from core.baker_recipes import old_subscriber_user, subscriber_user
|
||||
from core.models import User
|
||||
@@ -67,3 +72,36 @@ def test_identifications_viewable_by_user():
|
||||
assert list(picture.people.viewable_by(identifications[1].user)) == [
|
||||
identifications[1]
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
@pytest.mark.parametrize("save", [True, False])
|
||||
@pytest.mark.parametrize("initially_saved", [True, False])
|
||||
@pytest.mark.parametrize("pass_img_kwarg", [True, False])
|
||||
def test_generate_thumbnail(save, initially_saved, pass_img_kwarg):
|
||||
"""Test that Picture.generate_thumbnails works properly"""
|
||||
image = Image.new("RGB", (2, 1))
|
||||
image.putdata([(255, 0, 0), (0, 255, 0)])
|
||||
buffer = BytesIO()
|
||||
image.save(buffer, format="PNG")
|
||||
file = ContentFile(buffer.getvalue(), "img.png")
|
||||
picture: Picture = picture_recipe.prepare(
|
||||
file=file,
|
||||
name=file.name,
|
||||
mime_type="image/png",
|
||||
_save_related=True,
|
||||
)
|
||||
if initially_saved:
|
||||
picture.save()
|
||||
picture.generate_thumbnails(img=image if pass_img_kwarg else None, save=save)
|
||||
storage = picture.file.storage
|
||||
for f in picture.file, picture.compressed, picture.thumbnail:
|
||||
# the tested picture is alone in its album,
|
||||
# so there should be a single file in each folder
|
||||
assert storage.exists(f.name)
|
||||
_dirs, files = storage.listdir(str(Path(f.path).parent))
|
||||
assert files == [Path(f.name).name]
|
||||
new_img = Image.open(picture.file)
|
||||
assert new_img.get_flattened_data() == image.get_flattened_data()
|
||||
assert Image.open(picture.thumbnail).size == (200, 100)
|
||||
assert Image.open(picture.compressed).size == (1200, 600)
|
||||
|
||||
Reference in New Issue
Block a user