test_sub_parts.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import sub_parts
  7. CUR_DIR = os.path.dirname(__file__)
  8. class TestSubParts(unittest.TestCase):
  9. @classmethod
  10. def setUpClass(cls):
  11. settings = get_settings(filenames={})
  12. settings['PATH'] = os.path.join(CUR_DIR, 'test_data')
  13. settings['AUTHOR'] = 'Me'
  14. settings['DEFAULT_DATE'] = (1970, 1, 1)
  15. settings['DEFAULT_CATEGORY'] = 'Default'
  16. settings['FILENAME_METADATA'] = '(?P<slug>[^.]+)'
  17. settings['PLUGINS'] = [sub_parts]
  18. settings['CACHE_CONTENT'] = False
  19. cls.generator = ArticlesGenerator(
  20. context=settings.copy(), settings=settings,
  21. path=settings['PATH'], theme=settings['THEME'], output_path=None)
  22. cls.generator.generate_context()
  23. cls.all_articles = list(cls.generator.articles)
  24. sub_parts.patch_subparts(cls.generator)
  25. def test_all_articles(self):
  26. self.assertEqual(
  27. sorted(['noparent', 'parent',
  28. 'parent--explicit', 'parent--implicit']),
  29. sorted([a.slug for a in self.all_articles]))
  30. def test_articles(self):
  31. self.assertEqual(
  32. sorted(['noparent', 'parent']),
  33. sorted([a.slug for a in self.generator.articles]))
  34. def test_dates(self):
  35. self.assertEqual(
  36. sorted(['noparent', 'parent']),
  37. sorted([a.slug for a in self.generator.dates]))
  38. def test_categories(self):
  39. self.assertEqual(
  40. sorted(['noparent', 'parent']),
  41. sorted([a.slug for a in self.generator.categories[0][1]]))
  42. def test_tags(self):
  43. self.assertEqual(
  44. sorted([a.slug for a in self.all_articles]),
  45. sorted([a.slug for a in self.generator.tags['atag']]))
  46. def test_authors(self):
  47. self.assertEqual(
  48. sorted([a.slug for a in self.all_articles]),
  49. sorted([a.slug for a in self.generator.authors[0][1]]))
  50. def test_subparts(self):
  51. for a in self.all_articles:
  52. if a.slug == 'parent':
  53. self.assertTrue(hasattr(a, 'subparts'))
  54. self.assertEqual(
  55. sorted(['parent--explicit', 'parent--implicit']),
  56. sorted([a.slug for a in a.subparts]))
  57. else:
  58. self.assertFalse(hasattr(a, 'subparts'))
  59. def test_subpart_of(self):
  60. for a in self.all_articles:
  61. if '--' in a.slug:
  62. self.assertTrue(hasattr(a, 'subpart_of'))
  63. self.assertEqual('parent', a.subpart_of.slug)
  64. else:
  65. self.assertFalse(hasattr(a, 'subpart_of'))
  66. def test_subtitle(self):
  67. for a in self.all_articles:
  68. if '--' in a.slug:
  69. self.assertTrue(hasattr(a, 'subtitle'))
  70. self.assertEqual(a.title,
  71. a.subtitle + ', ' + a.subpart_of.title)
  72. class TestSubPartsPhotos(unittest.TestCase):
  73. @classmethod
  74. def setUpClass(cls):
  75. settings = get_settings(filenames={})
  76. settings['PATH'] = os.path.join(CUR_DIR, 'test_data')
  77. settings['AUTHOR'] = 'Me'
  78. settings['DEFAULT_DATE'] = (1970, 1, 1)
  79. settings['DEFAULT_CATEGORY'] = 'Default'
  80. settings['FILENAME_METADATA'] = '(?P<slug>[^.]+)'
  81. settings['PLUGINS'] = [sub_parts]
  82. settings['CACHE_CONTENT'] = False
  83. cls.generator = ArticlesGenerator(
  84. context=settings.copy(), settings=settings,
  85. path=settings['PATH'], theme=settings['THEME'], output_path=None)
  86. cls.generator.generate_context()
  87. cls.all_articles = list(cls.generator.articles)
  88. for a in cls.all_articles:
  89. a.photo_gallery = [('i.jpg', 'i.jpg', 'it.jpg', '', '')]
  90. sub_parts.patch_subparts(cls.generator)
  91. def test_subphotos(self):
  92. for a in self.all_articles:
  93. if a.slug == 'parent':
  94. self.assertTrue(hasattr(a, 'subphotos'))
  95. self.assertEqual(3, a.subphotos)
  96. else:
  97. self.assertFalse(hasattr(a, 'subphotos'))
  98. if __name__ == '__main__':
  99. unittest.main()