test_photos.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. from pelican.generators import ArticlesGenerator
  5. from pelican.tests.support import unittest, get_settings
  6. from tempfile import mkdtemp
  7. from shutil import rmtree
  8. import photos
  9. CUR_DIR = os.path.dirname(__file__)
  10. class TestPhotos(unittest.TestCase):
  11. @classmethod
  12. def setUpClass(cls):
  13. cls.temp_path = mkdtemp(prefix='pelicantests.')
  14. cls.settings = get_settings(filenames={})
  15. cls.settings['PATH'] = os.path.join(CUR_DIR, 'test_data')
  16. cls.settings['PHOTO_LIBRARY'] = os.path.join(CUR_DIR, 'test_data')
  17. cls.settings['DEFAULT_DATE'] = (1970, 1, 1)
  18. cls.settings['FILENAME_METADATA'] = '(?P<slug>[^.]+)'
  19. cls.settings['PLUGINS'] = [photos]
  20. cls.settings['CACHE_CONTENT'] = False
  21. cls.settings['OUTPUT_PATH'] = cls.temp_path
  22. cls.settings['SITEURL'] = 'http://getpelican.com/sub'
  23. photos.initialized(cls)
  24. cls.generator = ArticlesGenerator(
  25. context=cls.settings.copy(), settings=cls.settings,
  26. path=cls.settings['PATH'], theme=cls.settings['THEME'],
  27. output_path=cls.settings['OUTPUT_PATH'])
  28. photos.register()
  29. cls.generator.generate_context()
  30. photos.detect_gallery(cls.generator)
  31. photos.detect_image(cls.generator)
  32. @classmethod
  33. def tearDownClass(cls):
  34. rmtree(cls.temp_path)
  35. def test_image(self):
  36. for a in self.generator.articles:
  37. if 'image' in a.metadata:
  38. self.assertTrue(
  39. hasattr(a, 'photo_image'),
  40. msg="{} not recognized.".format(a.metadata['image']))
  41. def test_gallery(self):
  42. for a in self.generator.articles:
  43. if 'gallety' in a.metadata:
  44. self.assertTrue(
  45. hasattr(a, 'photo_gallery'),
  46. msg="{} not recognized.".format(a.metadata['gallery']))
  47. def get_article(self, slug):
  48. for a in self.generator.articles:
  49. if slug == a.slug:
  50. return a
  51. return None
  52. def test_photo_article_image(self):
  53. self.assertEqual(self.get_article('photo').photo_image,
  54. ('best.jpg',
  55. 'photos/agallery/besta.jpg',
  56. 'photos/agallery/bestt.jpg'))
  57. def test_photo_article_gallery(self):
  58. self.assertEqual(self.get_article('photo').photo_gallery[0],
  59. ('best.jpg',
  60. 'photos/agallery/best.jpg',
  61. 'photos/agallery/bestt.jpg',
  62. 'EXIF-best', 'Caption-best'))
  63. self.assertEqual(self.get_article('photo').photo_gallery[1],
  64. ('night.png',
  65. 'photos/agallery/night.jpg',
  66. 'photos/agallery/nightt.jpg',
  67. 'EXIF-night', ''))
  68. def test_photo_article_body(self):
  69. expected = ('<p>Here is my best photo, again.</p>\n'
  70. '<p><img alt="" src="http://getpelican.com/sub/photos/agallery/besta.jpg" />.</p>')
  71. self.assertEqual(expected, self.get_article('photo').content)
  72. def test_filename_article_image(self):
  73. self.assertEqual(
  74. ('best.jpg', 'agallery/best.jpg', 'photos/agallery/bestt.jpg'),
  75. self.get_article('filename').photo_image)
  76. def test_filename_article_gallery(self):
  77. self.assertEqual(self.get_article('filename').photo_gallery[0],
  78. ('best.jpg',
  79. 'agallery/best.jpg',
  80. 'photos/agallery/bestt.jpg',
  81. 'EXIF-best', 'Caption-best'))
  82. self.assertEqual(self.get_article('filename').photo_gallery[1],
  83. ('night.png',
  84. 'agallery/night.png',
  85. 'photos/agallery/nightt.jpg',
  86. 'EXIF-night', ''))
  87. def test_filename_article_body(self):
  88. expected = ('<p>Here is my best photo, again.</p>\n'
  89. '<p><img alt="" src="{filename}agallery/best.jpg" />.</p>')
  90. self.assertEqual(expected, self.get_article('filename').content)
  91. def test_queue_resize(self):
  92. expected = [
  93. ('photos/agallery/best.jpg',
  94. ('./test_data/agallery/best.jpg', (1024, 768, 80))),
  95. ('photos/agallery/besta.jpg',
  96. ('./test_data/agallery/best.jpg', (760, 506, 80))),
  97. ('photos/agallery/bestt.jpg',
  98. ('./test_data/agallery/best.jpg', (192, 144, 60))),
  99. ('photos/agallery/night.jpg',
  100. ('./test_data/agallery/night.png', (1024, 768, 80))),
  101. ('photos/agallery/nightt.jpg',
  102. ('./test_data/agallery/night.png', (192, 144, 60)))]
  103. self.assertEqual(sorted(expected), sorted(photos.queue_resize.items()))
  104. if __name__ == '__main__':
  105. unittest.main()