選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

177 行
6.8 KiB

  1. from io import BytesIO
  2. import mock
  3. import qrcode
  4. from django.test import TestCase
  5. from django.core.files.images import ImageFile
  6. from django.conf import settings
  7. from django_images.models import Image, Thumbnail
  8. from django_images.templatetags.images import at_size
  9. from django_images.utils import scale_and_crop_single
  10. from PIL import Image as PILImage
  11. class ImageModelTest(TestCase):
  12. def setUp(self):
  13. image_obj = BytesIO()
  14. qrcode_obj = qrcode.make('https://mirumee.com/')
  15. qrcode_obj.save(image_obj)
  16. self.image = Image.objects.create(width=370, height=370,
  17. image=ImageFile(image_obj, '01.png'))
  18. def test_get_by_size(self):
  19. size = list(settings.IMAGE_SIZES.keys())[0]
  20. Thumbnail.objects.get_or_create_at_sizes(self.image, [size])
  21. self.image.get_by_size(size)
  22. def test_get_absolute_url(self):
  23. url = self.image.get_absolute_url()
  24. self.assertEqual(url, self.image.image.url)
  25. # For thumbnail
  26. size = list(settings.IMAGE_SIZES.keys())[0]
  27. thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0]
  28. url = self.image.get_absolute_url(size)
  29. self.assertEqual(url, thumb.image.url)
  30. class ThumbnailManagerModelTest(TestCase):
  31. def setUp(self):
  32. image_obj = BytesIO()
  33. qrcode_obj = qrcode.make('https://mirumee.com/')
  34. qrcode_obj.save(image_obj)
  35. self.image = Image.objects.create(width=370, height=370,
  36. image=ImageFile(image_obj, '01.png'))
  37. self.size = list(settings.IMAGE_SIZES.keys())[0]
  38. def test_unknown_size(self):
  39. self.assertRaises(ValueError, Thumbnail.objects.get_or_create_at_sizes,
  40. self.image, ['foo'])
  41. # TODO: Test the image object and data
  42. def test_create(self):
  43. Thumbnail.objects.get_or_create_at_sizes(self.image, [self.size])
  44. self.assertEqual(self.image.thumbnail_set.count(), 1)
  45. def test_get(self):
  46. thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [self.size])[0]
  47. thumb2 = Thumbnail.objects.get_or_create_at_sizes(self.image, [self.size])[0]
  48. self.assertEqual(thumb.id, thumb2.id)
  49. class ThumbnailModelTest(TestCase):
  50. def setUp(self):
  51. image_obj = BytesIO()
  52. qrcode_obj = qrcode.make('https://mirumee.com/')
  53. qrcode_obj.save(image_obj)
  54. self.image = Image.objects.create(width=370, height=370,
  55. image=ImageFile(image_obj, '01.png'))
  56. size = list(settings.IMAGE_SIZES.keys())[0]
  57. self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0]
  58. def test_get_absolute_url(self):
  59. url = self.thumb.get_absolute_url()
  60. self.assertEqual(url, self.thumb.image.url)
  61. class PostSaveSignalOriginalChangedTestCase(TestCase):
  62. def setUp(self):
  63. image_obj = BytesIO()
  64. qrcode_obj = qrcode.make('https://mirumee.com/')
  65. qrcode_obj.save(image_obj)
  66. self.image = Image.objects.create(width=370, height=370,
  67. image=ImageFile(image_obj, '01.png'))
  68. size = list(settings.IMAGE_SIZES.keys())[0]
  69. self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0]
  70. def test_post_save_signal_original_changed(self):
  71. size = list(settings.IMAGE_SIZES.keys())[0]
  72. Thumbnail.objects.get_or_create_at_sizes(self.image, [size])
  73. self.image.delete()
  74. self.assertFalse(Thumbnail.objects.exists())
  75. class PostDeleteSignalDeleteImageFileTest(TestCase):
  76. def setUp(self):
  77. image_obj = BytesIO()
  78. qrcode_obj = qrcode.make('https://mirumee.com/')
  79. qrcode_obj.save(image_obj)
  80. self.image = Image.objects.create(width=370, height=370,
  81. image=ImageFile(image_obj, '01.png'))
  82. size = list(settings.IMAGE_SIZES.keys())[0]
  83. self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0]
  84. @mock.patch('django_images.models.IMAGE_AUTO_DELETE', True)
  85. def test_post_delete_signal_delete_image_files_enabled(self):
  86. storage = self.image.image.storage
  87. image_name = self.image.image.name
  88. thumb_name = self.thumb.image.name
  89. self.image.delete()
  90. self.assertFalse(storage.exists(image_name))
  91. self.assertFalse(storage.exists(thumb_name))
  92. @mock.patch('django_images.models.IMAGE_AUTO_DELETE', False)
  93. def test_post_delete_signal_delete_image_files_disabled(self):
  94. storage = self.image.image.storage
  95. image_name = self.image.image.name
  96. thumb_name = self.thumb.image.name
  97. # Delete thumb
  98. self.thumb.delete()
  99. self.assertTrue(storage.exists(image_name))
  100. self.assertTrue(storage.exists(thumb_name))
  101. # Delete image
  102. self.image.delete()
  103. self.assertTrue(storage.exists(image_name))
  104. self.assertTrue(storage.exists(thumb_name))
  105. class AtSizeTemplateTagTest(TestCase):
  106. def setUp(self):
  107. image_obj = BytesIO()
  108. qrcode_obj = qrcode.make('https://mirumee.com/')
  109. qrcode_obj.save(image_obj)
  110. self.image = Image.objects.create(width=370, height=370,
  111. image=ImageFile(image_obj, '01.png'))
  112. size = list(settings.IMAGE_SIZES.keys())[0]
  113. self.thumb = Thumbnail.objects.get_or_create_at_sizes(self.image, [size])[0]
  114. def test_at_size(self):
  115. size = list(settings.IMAGE_SIZES.keys())[0]
  116. url = at_size(self.image, size)
  117. self.assertEqual(url, self.thumb.image.url)
  118. class UtilsScaleAndDropTest(TestCase):
  119. def setUp(self):
  120. image_obj = BytesIO()
  121. qrcode_obj = qrcode.make('https://mirumee.com/')
  122. qrcode_obj.save(image_obj)
  123. self.image = PILImage.open(image_obj)
  124. def test_change_size(self):
  125. new_size = (10, 10)
  126. image = scale_and_crop_single(self.image, new_size)
  127. self.assertEqual(new_size, image.size)
  128. def test_crop(self):
  129. new_size = (10, 10)
  130. image = scale_and_crop_single(self.image, new_size, crop=True)
  131. self.assertEqual(new_size, image.size)
  132. def test_disabled_upscale(self):
  133. image = scale_and_crop_single(self.image, (740, 740), upscale=False)
  134. self.assertLess(image.size[0], 371)
  135. self.assertLess(image.size[1], 371)
  136. def test_enaabled_upscale(self):
  137. image = scale_and_crop_single(self.image, (740, 740), upscale=True)
  138. self.assertGreater(image.size[0], 371)
  139. self.assertGreater(image.size[1], 371)
  140. def test_not_change_quality(self):
  141. image = scale_and_crop_single(self.image, (10, 10), quality=None)
  142. self.assertEqual(image.info.get('quality'), None)
  143. def test_change_quality(self):
  144. image = scale_and_crop_single(self.image, (10, 10), quality=50)
  145. self.assertEqual(image.info.get('quality'), 50)