test_representative_image.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/sh
  2. import unittest
  3. from jinja2.utils import generate_lorem_ipsum
  4. # Generate content with image
  5. TEST_CONTENT_IMAGE_URL = 'https://testimage.com/test.jpg'
  6. TEST_CONTENT = str(generate_lorem_ipsum(n=3, html=True)) + '<img src="' + TEST_CONTENT_IMAGE_URL + '"/>'+ str(generate_lorem_ipsum(n=2,html=True))
  7. TEST_SUMMARY_IMAGE_URL = 'https://testimage.com/summary.jpg'
  8. TEST_SUMMARY_WITHOUTIMAGE = str(generate_lorem_ipsum(n=1, html=True))
  9. TEST_SUMMARY_WITHIMAGE = TEST_SUMMARY_WITHOUTIMAGE + '<img src="' + TEST_SUMMARY_IMAGE_URL + '"/>'
  10. TEST_CUSTOM_IMAGE_URL = 'https://testimage.com/custom.jpg'
  11. from pelican.contents import Article
  12. import representative_image
  13. class TestRepresentativeImage(unittest.TestCase):
  14. def setUp(self):
  15. super(TestRepresentativeImage, self).setUp()
  16. representative_image.register()
  17. def test_extract_image_from_content(self):
  18. args = {
  19. 'content': TEST_CONTENT,
  20. 'metadata': {
  21. 'summary': TEST_SUMMARY_WITHOUTIMAGE,
  22. },
  23. }
  24. article = Article(**args)
  25. self.assertEqual(article.featured_image, TEST_CONTENT_IMAGE_URL)
  26. def test_extract_image_from_summary(self):
  27. args = {
  28. 'content': TEST_CONTENT,
  29. 'metadata': {
  30. 'summary': TEST_SUMMARY_WITHIMAGE,
  31. },
  32. }
  33. article = Article(**args)
  34. self.assertEqual(article.featured_image, TEST_SUMMARY_IMAGE_URL)
  35. self.assertEqual(article.summary, TEST_SUMMARY_WITHOUTIMAGE)
  36. def test_extract_image_from_summary_with_custom_image(self):
  37. args = {
  38. 'content': TEST_CONTENT,
  39. 'metadata': {
  40. 'summary': TEST_SUMMARY_WITHIMAGE,
  41. 'image': TEST_CUSTOM_IMAGE_URL,
  42. },
  43. }
  44. article = Article(**args)
  45. self.assertEqual(article.featured_image, TEST_CUSTOM_IMAGE_URL)
  46. self.assertEqual(article.summary, TEST_SUMMARY_WITHOUTIMAGE)
  47. if __name__ == '__main__':
  48. unittest.main()