easy_thumbnails.test: 63 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/easy_thumbnails/test.py

Stats: 0 executed, 53 missed, 10 excluded, 61 ignored

  1. import shutil
  2. import tempfile
  3. try:
  4. from cStringIO import StringIO
  5. except ImportError:
  6. from StringIO import StringIO
  7. from django.core.files.base import ContentFile
  8. from django.core.files.storage import FileSystemStorage
  9. from django.test import TestCase
  10. try:
  11. from PIL import Image
  12. except ImportError:
  13. import Image
  14. from easy_thumbnails.conf import settings
  15. class TemporaryStorage(FileSystemStorage):
  16. """
  17. A storage class useful for tests that uses a temporary location to store
  18. all files and provides a method to remove this location when it is finished
  19. with.
  20. """
  21. def __init__(self, location=None, *args, **kwargs):
  22. """
  23. Create the temporary location.
  24. """
  25. if location is None:
  26. location = tempfile.mkdtemp()
  27. self.temporary_location = location
  28. super(TemporaryStorage, self).__init__(location=location, *args,
  29. **kwargs)
  30. def delete_temporary_storage(self):
  31. """
  32. Delete the temporary directory created during initialisation.
  33. This storage class should not be used again after this method is
  34. called.
  35. """
  36. temporary_location = getattr(self, 'temporary_location', None)
  37. if temporary_location:
  38. shutil.rmtree(temporary_location)
  39. class FakeRemoteStorage(TemporaryStorage):
  40. """
  41. A temporary storage class that acts similar to remote storage.
  42. It's not thread safe.
  43. """
  44. remote_mode = False
  45. def path(self, *args, **kwargs):
  46. """
  47. Raise ``NotImplementedError``, since this is the way that
  48. easy-thumbnails determines if a storage is remote.
  49. """
  50. if self.remote_mode:
  51. raise NotImplementedError
  52. return super(FakeRemoteStorage, self).path(*args, **kwargs)
  53. def exists(self, *args, **kwargs):
  54. original_remote_mode = self.remote_mode
  55. self.remote_mode = False
  56. try:
  57. return super(FakeRemoteStorage, self).exists(*args, **kwargs)
  58. finally:
  59. self.remote_mode = original_remote_mode
  60. def save(self, *args, **kwargs):
  61. self.remote_mode = False
  62. try:
  63. return super(FakeRemoteStorage, self).save(*args, **kwargs)
  64. finally:
  65. self.remote_mode = True
  66. def open(self, *args, **kwargs):
  67. self.remote_mode = False
  68. try:
  69. return super(FakeRemoteStorage, self).open(*args, **kwargs)
  70. finally:
  71. self.remote_mode = True
  72. class BaseTest(TestCase):
  73. """
  74. Remove any customised THUMBNAIL_* settings in a project's ``settings``
  75. configuration module before running the tests to ensure there is a
  76. consistent test environment.
  77. """
  78. def setUp(self):
  79. """
  80. Isolate all settings.
  81. """
  82. output = super(BaseTest, self).setUp()
  83. settings.isolated = True
  84. return output
  85. def tearDown(self):
  86. """
  87. Restore settings to their original state.
  88. """
  89. settings.isolated = False
  90. settings.revert()
  91. return super(BaseTest, self).tearDown()
  92. def create_image(self, storage, filename, size=(800, 600),
  93. image_mode='RGB', image_format='JPEG'):
  94. """
  95. Generate a test image, returning the filename that it was saved as.
  96. If ``storage`` is ``None``, the StringIO containing the image data
  97. will be passed instead.
  98. """
  99. data = StringIO()
  100. Image.new(image_mode, size).save(data, image_format)
  101. data.seek(0)
  102. if not storage:
  103. return data
  104. image_file = ContentFile(data.read())
  105. return storage.save(filename, image_file)