import pytest from django.core.files.base import ContentFile from apps.gallery.models import Gallery from apps.gallery.tasks import process_uploaded_image pytestmark = [pytest.mark.integration, pytest.mark.django_db] def test_gallery_processing_updates_metadata(user_factory, tmp_path, settings, mocker): settings.MEDIA_ROOT = str(tmp_path) gallery = Gallery(title="Photo", uploaded_by=user_factory()) gallery._defer_image_processing = True gallery.image.save("gallery/photo.jpg", ContentFile(b"image data")) result = mocker.Mock(file_size=10, width=20, height=30) process = mocker.patch("apps.gallery.tasks.safe_process_public_image", return_value=result) assert process_uploaded_image.run(gallery.pk) == "Processed image: Photo" gallery.refresh_from_db() assert (gallery.file_size, gallery.width, gallery.height) == (10, 20, 30) process.assert_called_once() def test_gallery_processing_missing_image_and_id(user_factory): empty = Gallery.objects.create(title="Empty", uploaded_by=user_factory()) assert process_uploaded_image.run(empty.pk) is None with pytest.raises(Gallery.DoesNotExist): process_uploaded_image.run(999999)