test_shortcuts.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.test import TestCase
  2. from wagtail.images.models import Filter
  3. from wagtail.images.shortcuts import (
  4. get_rendition_or_not_found,
  5. get_renditions_or_not_found,
  6. )
  7. from .utils import Image, get_test_image_file
  8. class TestShortcuts(TestCase):
  9. fixtures = ["test.json"]
  10. def test_fallback_to_not_found(self):
  11. bad_image = Image.objects.get(id=1)
  12. good_image = Image.objects.create(
  13. title="Test image",
  14. file=get_test_image_file(),
  15. )
  16. rendition = get_rendition_or_not_found(good_image, "width-400")
  17. self.assertEqual(rendition.width, 400)
  18. rendition = get_rendition_or_not_found(bad_image, "width-400")
  19. self.assertEqual(rendition.file.name, "not-found")
  20. def test_multiple_fallback_to_not_found(self):
  21. bad_image = Image.objects.get(id=1)
  22. good_image = Image.objects.create(
  23. title="Test image",
  24. file=get_test_image_file(),
  25. )
  26. renditions = get_renditions_or_not_found(good_image, ("width-200", "width-400"))
  27. self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
  28. self.assertEqual(renditions["width-200"].width, 200)
  29. self.assertEqual(renditions["width-400"].width, 400)
  30. renditions = get_renditions_or_not_found(bad_image, ("width-200", "width-400"))
  31. self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
  32. self.assertEqual(renditions["width-200"].file.name, "not-found")
  33. self.assertEqual(renditions["width-400"].file.name, "not-found")
  34. def test_multiple_fallback_to_not_found_with_filters(self):
  35. bad_image = Image.objects.get(id=1)
  36. good_image = Image.objects.create(
  37. title="Test image",
  38. file=get_test_image_file(),
  39. )
  40. renditions = get_renditions_or_not_found(
  41. good_image, (Filter("width-200"), Filter("width-400"))
  42. )
  43. self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
  44. self.assertEqual(renditions["width-200"].width, 200)
  45. self.assertEqual(renditions["width-400"].width, 400)
  46. renditions = get_renditions_or_not_found(
  47. bad_image, (Filter("width-200"), Filter("width-400"))
  48. )
  49. self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
  50. self.assertEqual(renditions["width-200"].file.name, "not-found")
  51. self.assertEqual(renditions["width-400"].file.name, "not-found")