tests.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from __future__ import unicode_literals
  2. from future.builtins import str
  3. from future.utils import native
  4. import os
  5. from shutil import rmtree
  6. from uuid import uuid4
  7. from mezzanine.conf import settings
  8. from mezzanine.core.templatetags.mezzanine_tags import thumbnail
  9. from mezzanine.galleries.models import Gallery, GALLERIES_UPLOAD_DIR
  10. from mezzanine.utils.tests import TestCase, copy_test_to_media
  11. class GalleriesTests(TestCase):
  12. def test_gallery_import(self):
  13. """
  14. Test that a gallery creates images when given a zip file to
  15. import, and that descriptions are created.
  16. """
  17. zip_name = "gallery.zip"
  18. copy_test_to_media("mezzanine.core", zip_name)
  19. title = native(str(uuid4())) # i.e. Py3 str / Py2 unicode
  20. gallery = Gallery.objects.create(title=title, zip_import=zip_name)
  21. images = list(gallery.images.all())
  22. self.assertTrue(images)
  23. self.assertTrue(all([image.description for image in images]))
  24. # Clean up.
  25. rmtree(os.path.join(settings.MEDIA_ROOT,
  26. GALLERIES_UPLOAD_DIR, title))
  27. def test_thumbnail_generation(self):
  28. """
  29. Test that a thumbnail is created and resized.
  30. """
  31. try:
  32. from PIL import Image
  33. except ImportError:
  34. return
  35. image_name = "image.jpg"
  36. size = (24, 24)
  37. copy_test_to_media("mezzanine.core", image_name)
  38. thumb_name = os.path.join(settings.THUMBNAILS_DIR_NAME, image_name,
  39. image_name.replace(".", "-%sx%s." % size))
  40. thumb_path = os.path.join(settings.MEDIA_ROOT, thumb_name)
  41. thumb_image = thumbnail(image_name, *size)
  42. self.assertEqual(os.path.normpath(thumb_image.lstrip("/")), thumb_name)
  43. self.assertNotEqual(os.path.getsize(thumb_path), 0)
  44. thumb = Image.open(thumb_path)
  45. self.assertEqual(thumb.size, size)
  46. # Clean up.
  47. del thumb
  48. os.remove(os.path.join(settings.MEDIA_ROOT, image_name))
  49. os.remove(os.path.join(thumb_path))
  50. rmtree(os.path.join(os.path.dirname(thumb_path)))