You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tests.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from django.conf import settings
  2. from django.utils import unittest
  3. from oscar.apps.dynamic_images import ImageModifier
  4. from oscar.apps.dynamic_images.exceptions import ResizerFormatException
  5. from oscar.apps.dynamic_images.mods import AutotrimMod, CropMod, ResizeMod
  6. import os
  7. import Image
  8. TEST_IMAGE = os.path.join(os.path.dirname(__file__), 'test_fixtures/test.jpg')
  9. class ModifierTestCase(unittest.TestCase):
  10. def setUp(self):
  11. self.base_config = settings.DYNAMIC_MEDIA_CONFIG
  12. def tearDown(self):
  13. pass
  14. def test_process_path_nochange(self):
  15. path = '/path/to/image.jpg'
  16. modifier = ImageModifier(path,self.base_config)
  17. filename = modifier.source_filename
  18. params = modifier._params
  19. self.assertEquals(filename,'/path/to/image.jpg')
  20. self.assertEquals(params['type'],'jpg')
  21. def test_process_path_invalid_ext(self):
  22. path = '/path/to/image.ext'
  23. with self.assertRaises(ResizerFormatException):
  24. ImageModifier(path,self.base_config)
  25. def test_process_path_change_ext(self):
  26. path = '/path/to/image.jpg.to.gif'
  27. modifier = ImageModifier(path,self.base_config)
  28. filename = modifier.source_filename
  29. params = modifier._params
  30. self.assertEquals(filename,'/path/to/image.jpg')
  31. self.assertEquals(params['type'],'gif')
  32. def test_process_path_with_param(self):
  33. path = '/path/to/image.jpg.width-300.png'
  34. modifier = ImageModifier(path,self.base_config)
  35. filename = modifier.source_filename
  36. params = modifier._params
  37. self.assertEquals(filename, '/path/to/image.jpg')
  38. self.assertEquals(params['type'], 'png')
  39. self.assertEquals(params['width'], '300')
  40. self.assertEquals(len(params), 2)
  41. def test_process_path_with_params(self):
  42. path = '/path/to/image.jpg.width-300_height-200.png'
  43. modifier = ImageModifier(path,self.base_config)
  44. filename = modifier.source_filename
  45. params = modifier._params
  46. self.assertEquals(filename, '/path/to/image.jpg')
  47. self.assertEquals(params['type'], 'png')
  48. self.assertEquals(params['width'], '300')
  49. self.assertEquals(params['height'], '200')
  50. self.assertEquals(len(params), 3)
  51. def test_process_path_with_unusual_params(self):
  52. path = '/path/to/image.jpg.width-300_height-200_crop-400,40,1000,1000.png'
  53. modifier = ImageModifier(path,self.base_config)
  54. filename = modifier.source_filename
  55. params = modifier._params
  56. self.assertEquals(filename, '/path/to/image.jpg')
  57. self.assertEquals(params['type'], 'png')
  58. self.assertEquals(params['width'], '300')
  59. self.assertEquals(params['height'], '200')
  60. self.assertEquals(params['crop'], '400,40,1000,1000')
  61. self.assertEquals(len(params), 4)
  62. def test_process_path_unusual_nochange_filenames(self):
  63. test_filenames = (
  64. '/path/to/image.jpg..png',
  65. '/path/to/image.jpg.png',
  66. '/path/to/image.jpg._.png',
  67. )
  68. for t in test_filenames:
  69. modifier = ImageModifier(t, self.base_config)
  70. filename = modifier.source_filename
  71. self.assertEquals(filename, t)
  72. def test_process_path_dots_in_path(self):
  73. path = '/path.to/image.jpg.width-300_height-200.png'
  74. modifier = ImageModifier(path, self.base_config)
  75. filename = modifier.source_filename
  76. self.assertEquals(filename, '/path.to/image.jpg')
  77. path = '/path.to/another.nice/image.jpg.width-300_height-200.png'
  78. modifier = ImageModifier(path, self.base_config)
  79. filename = modifier.source_filename
  80. self.assertEquals(filename, '/path.to/another.nice/image.jpg')
  81. def test_process_path_dots_in_filename(self):
  82. path = '/path.to/image.ext.jpg.width-300_height-200.png'
  83. modifier = ImageModifier(path, self.base_config)
  84. filename = modifier.source_filename
  85. self.assertEquals(filename, '/path.to/image.ext.jpg')
  86. class ImageModTestCase(unittest.TestCase):
  87. def setUp(self):
  88. self.image = Image.open(TEST_IMAGE)
  89. def tearDown(self):
  90. pass
  91. def test_correct_image(self):
  92. """ Sanity check to make sure it's the right test image """
  93. self.assertEquals(self.image.size, (1535, 1800))
  94. def test_resize(self):
  95. mod = ResizeMod(self.image, dict(width=200, height=200))
  96. im = mod.apply()
  97. self.assertEquals(im.size, (200, 200))
  98. self.assertEquals(self.image.size, (1535, 1800)) # Mod shouldn't change original
  99. def test_resize_rounding(self):
  100. mod = ResizeMod(self.image, dict(width=200))
  101. im = mod.apply()
  102. self.assertEquals(im.size, (200, 235)) # Check scaling rounds up
  103. mod = ResizeMod(self.image, dict(height=200))
  104. im = mod.apply()
  105. self.assertEquals(im.size, (171, 200))
  106. def test_crop(self):
  107. mod = CropMod(self.image, dict(crop='50,25,200,200'))
  108. im = mod.apply()
  109. self.assertEquals(im.size, (150, 175))
  110. self.assertEquals(self.image.size, (1535, 1800)) # Mod shouldn't change original