Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tests.py 8.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import mock
  2. import qrcode
  3. from django.test import TestCase
  4. from django.core.files.images import ImageFile
  5. from django.conf import settings
  6. from django.utils.six import BytesIO
  7. from django.core.urlresolvers import reverse
  8. from django_images.models import Image, Thumbnail
  9. from django_images.templatetags.images import at_size
  10. from django_images.utils import scale_and_crop
  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. thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, 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_size(self.image.id, size)
  28. url = self.image.get_absolute_url(size)
  29. self.assertEqual(url, thumb.image.url)
  30. # Fallback on creation url
  31. size = list(settings.IMAGE_SIZES.keys())[1]
  32. url = self.image.get_absolute_url(size)
  33. fallback_url = reverse('image-thumbnail', args=(self.image.id, size))
  34. self.assertEqual(url, fallback_url)
  35. class ThumbnailManagerModelTest(TestCase):
  36. def setUp(self):
  37. image_obj = BytesIO()
  38. qrcode_obj = qrcode.make('https://mirumee.com/')
  39. qrcode_obj.save(image_obj)
  40. self.image = Image.objects.create(width=370, height=370,
  41. image=ImageFile(image_obj, '01.png'))
  42. self.size = list(settings.IMAGE_SIZES.keys())[0]
  43. def test_unknown_size(self):
  44. self.assertRaises(ValueError, Thumbnail.objects.get_or_create_at_size,
  45. self.image.id, 'foo')
  46. # TODO: Test the image object and data
  47. def test_create(self):
  48. thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size)
  49. self.assertEqual(self.image.thumbnail_set.count(), 1)
  50. def test_get(self):
  51. thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size)
  52. thumb2 = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size)
  53. self.assertEqual(thumb.id, thumb2.id)
  54. class ThumbnailModelTest(TestCase):
  55. def setUp(self):
  56. image_obj = BytesIO()
  57. qrcode_obj = qrcode.make('https://mirumee.com/')
  58. qrcode_obj.save(image_obj)
  59. self.image = Image.objects.create(width=370, height=370,
  60. image=ImageFile(image_obj, '01.png'))
  61. size = list(settings.IMAGE_SIZES.keys())[0]
  62. self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size)
  63. def test_get_absolute_url(self):
  64. url = self.thumb.get_absolute_url()
  65. self.assertEqual(url, self.thumb.image.url)
  66. class PostSaveSignalOriginalChangedTestCase(TestCase):
  67. def setUp(self):
  68. image_obj = BytesIO()
  69. qrcode_obj = qrcode.make('https://mirumee.com/')
  70. qrcode_obj.save(image_obj)
  71. self.image = Image.objects.create(width=370, height=370,
  72. image=ImageFile(image_obj, '01.png'))
  73. size = list(settings.IMAGE_SIZES.keys())[0]
  74. self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size)
  75. def test_post_save_signal_original_changed(self):
  76. size = list(settings.IMAGE_SIZES.keys())[0]
  77. thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size)
  78. self.image.delete()
  79. self.assertFalse(Thumbnail.objects.exists())
  80. class PostDeleteSignalDeleteImageFileTest(TestCase):
  81. def setUp(self):
  82. image_obj = BytesIO()
  83. qrcode_obj = qrcode.make('https://mirumee.com/')
  84. qrcode_obj.save(image_obj)
  85. self.image = Image.objects.create(width=370, height=370,
  86. image=ImageFile(image_obj, '01.png'))
  87. size = list(settings.IMAGE_SIZES.keys())[0]
  88. self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size)
  89. @mock.patch('django_images.models.IMAGE_AUTO_DELETE', True)
  90. def test_post_delete_signal_delete_image_files_enabled(self):
  91. storage = self.image.image.storage
  92. image_name = self.image.image.name
  93. thumb_name = self.thumb.image.name
  94. self.image.delete()
  95. self.assertFalse(storage.exists(image_name))
  96. self.assertFalse(storage.exists(thumb_name))
  97. @mock.patch('django_images.models.IMAGE_AUTO_DELETE', False)
  98. def test_post_delete_signal_delete_image_files_disabled(self):
  99. storage = self.image.image.storage
  100. image_name = self.image.image.name
  101. thumb_name = self.thumb.image.name
  102. # Delete thumb
  103. self.thumb.delete()
  104. self.assertTrue(storage.exists(image_name))
  105. self.assertTrue(storage.exists(thumb_name))
  106. # Delete image
  107. self.image.delete()
  108. self.assertTrue(storage.exists(image_name))
  109. self.assertTrue(storage.exists(thumb_name))
  110. class AtSizeTemplateTagTest(TestCase):
  111. def setUp(self):
  112. image_obj = BytesIO()
  113. qrcode_obj = qrcode.make('https://mirumee.com/')
  114. qrcode_obj.save(image_obj)
  115. self.image = Image.objects.create(width=370, height=370,
  116. image=ImageFile(image_obj, '01.png'))
  117. size = list(settings.IMAGE_SIZES.keys())[0]
  118. self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, size)
  119. def test_at_size(self):
  120. size = list(settings.IMAGE_SIZES.keys())[0]
  121. url = at_size(self.image, size)
  122. self.assertEqual(url, self.thumb.image.url)
  123. class ThumbnailViewTest(TestCase):
  124. def setUp(self):
  125. image_obj = BytesIO()
  126. qrcode_obj = qrcode.make('https://mirumee.com/')
  127. qrcode_obj.save(image_obj)
  128. self.image = Image.objects.create(width=370, height=370,
  129. image=ImageFile(image_obj, '01.png'))
  130. self.size = list(settings.IMAGE_SIZES.keys())[0]
  131. self.thumb = Thumbnail.objects.get_or_create_at_size(self.image.id, self.size)
  132. def test_redirect(self):
  133. url = reverse('image-thumbnail', args=[self.image.id, self.size])
  134. response = self.client.get(url)
  135. self.assertNotEqual(url, self.thumb.image.url)
  136. self.assertEqual(response.status_code, 302)
  137. self.assertEqual(response.url, self.thumb.image.url)
  138. def test_not_found(self):
  139. url = reverse('image-thumbnail', args=['42', self.size])
  140. response = self.client.get(url)
  141. self.assertEqual(response.status_code, 404)
  142. def test_size_not_found(self):
  143. url = reverse('image-thumbnail', args=[self.image.id, '42'])
  144. response = self.client.get(url)
  145. self.assertEqual(response.status_code, 404)
  146. class UtilsScaleAndDropTest(TestCase):
  147. def setUp(self):
  148. image_obj = BytesIO()
  149. qrcode_obj = qrcode.make('https://mirumee.com/')
  150. qrcode_obj.save(image_obj)
  151. self.imagefile = ImageFile(image_obj, '01.png')
  152. def test_change_size(self):
  153. new_size = (10, 10)
  154. image = scale_and_crop(self.imagefile, new_size)
  155. self.assertEqual(new_size, image.im.size)
  156. def test_crop(self):
  157. new_size = (10, 10)
  158. image = scale_and_crop(self.imagefile, new_size, crop=True)
  159. self.assertEqual(new_size, image.im.size)
  160. def test_disabled_upscale(self):
  161. image = scale_and_crop(self.imagefile, (740, 740), upscale=False)
  162. self.assertLess(image.im.size[0], 371)
  163. self.assertLess(image.im.size[1], 371)
  164. def test_enaabled_upscale(self):
  165. image = scale_and_crop(self.imagefile, (740, 740), upscale=True)
  166. self.assertGreater(image.im.size[0], 371)
  167. self.assertGreater(image.im.size[1], 371)
  168. def test_not_change_quality(self):
  169. image = scale_and_crop(self.imagefile, (10, 10), quality=None)
  170. self.assertEqual(image.info.get('quality'), None)
  171. def test_change_quality(self):
  172. image = scale_and_crop(self.imagefile, (10, 10), quality=50)
  173. self.assertEqual(image.info.get('quality'), 50)