test_photos.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. photos.initialized(cls)
  23. cls.generator = ArticlesGenerator(
  24. context=cls.settings.copy(), settings=cls.settings,
  25. path=cls.settings['PATH'], theme=cls.settings['THEME'],
  26. output_path=cls.settings['OUTPUT_PATH'])
  27. photos.register()
  28. cls.generator.generate_context()
  29. photos.detect_gallery(cls.generator)
  30. photos.detect_image(cls.generator)
  31. @classmethod
  32. def tearDownClass(cls):
  33. rmtree(cls.temp_path)
  34. def test_image(self):
  35. for a in self.generator.articles:
  36. if 'image' in a.metadata:
  37. self.assertTrue(
  38. hasattr(a, 'photo_image'),
  39. msg="{} not recognized.".format(a.metadata['image']))
  40. def test_gallery(self):
  41. for a in self.generator.articles:
  42. if 'gallety' in a.metadata:
  43. self.assertTrue(
  44. hasattr(a, 'photo_gallery'),
  45. msg="{} not recognized.".format(a.metadata['gallery']))
  46. def get_article(self, slug):
  47. for a in self.generator.articles:
  48. if slug == a.slug:
  49. return a
  50. return None
  51. def test_photo_article_image(self):
  52. self.assertEqual(self.get_article('photo').photo_image,
  53. ('best.jpg',
  54. 'photos/agallery/besta.jpg',
  55. 'photos/agallery/bestt.jpg'))
  56. def test_photo_article_gallery(self):
  57. self.assertEqual(self.get_article('photo').photo_gallery[0],
  58. ('best.jpg',
  59. 'photos/agallery/best.jpg',
  60. 'photos/agallery/bestt.jpg',
  61. 'EXIF-best', 'Caption-best'))
  62. self.assertEqual(self.get_article('photo').photo_gallery[1],
  63. ('night.png',
  64. 'photos/agallery/night.jpg',
  65. 'photos/agallery/nightt.jpg',
  66. 'EXIF-night', ''))
  67. def test_photo_article_body(self):
  68. expected = ('<p>Here is my best photo, again.</p>\n'
  69. '<p><img alt="" src="/photos/agallery/besta.jpg" />.</p>')
  70. self.assertEqual(expected, self.get_article('photo').content)
  71. def test_filename_article_image(self):
  72. self.assertEqual(
  73. ('best.jpg', 'agallery/best.jpg', 'photos/agallery/bestt.jpg'),
  74. self.get_article('filename').photo_image)
  75. def test_filename_article_gallery(self):
  76. self.assertEqual(self.get_article('filename').photo_gallery[0],
  77. ('best.jpg',
  78. 'agallery/best.jpg',
  79. 'photos/agallery/bestt.jpg',
  80. 'EXIF-best', 'Caption-best'))
  81. self.assertEqual(self.get_article('filename').photo_gallery[1],
  82. ('night.png',
  83. 'agallery/night.png',
  84. 'photos/agallery/nightt.jpg',
  85. 'EXIF-night', ''))
  86. def test_filename_article_body(self):
  87. expected = ('<p>Here is my best photo, again.</p>\n'
  88. '<p><img alt="" src="{filename}agallery/best.jpg" />.</p>')
  89. self.assertEqual(expected, self.get_article('filename').content)
  90. def test_queue_resize(self):
  91. expected = [
  92. ('photos/agallery/best.jpg',
  93. ('test_data/agallery/best.jpg', (1024, 768, 80))),
  94. ('photos/agallery/besta.jpg',
  95. ('test_data/agallery/best.jpg', (760, 506, 80))),
  96. ('photos/agallery/bestt.jpg',
  97. ('test_data/agallery/best.jpg', (192, 144, 60))),
  98. ('photos/agallery/night.jpg',
  99. ('test_data/agallery/night.png', (1024, 768, 80))),
  100. ('photos/agallery/nightt.jpg',
  101. ('test_data/agallery/night.png', (192, 144, 60)))]
  102. self.assertEqual(sorted(expected), sorted(photos.queue_resize.items()))
  103. if __name__ == '__main__':
  104. unittest.main()