test_collate_content.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. """
  2. test_collate_content.py
  3. =======================
  4. (c) 2014 - Edward J. Stronge
  5. Tests for the collate_content module
  6. """
  7. from collections import defaultdict, namedtuple
  8. import os
  9. import random
  10. import tempfile
  11. import shutil
  12. import string
  13. import unittest
  14. from pelican import Pelican
  15. from pelican import ArticlesGenerator, PagesGenerator
  16. from pelican.settings import read_settings
  17. import collate_content as cc
  18. TEMP_PAGE_TEMPLATE = """Title: {title}
  19. Category: {category}
  20. """
  21. Content = namedtuple('Content', ['title', 'path', 'category'])
  22. # Characters likely to appear in blog titles/categories. Could eventually
  23. # extend support to more characters that can't appear in a Python identifier
  24. BLOG_CHARACTERS = string.letters + ' -:'
  25. def make_content(directory, categories, count=5, categories_per_content=1):
  26. """
  27. make_content --> {(processed_category, original_category): articles, ...}
  28. Writes random titles and categories into `count` temporary
  29. files in `directory`. If desired, specify `categories_per_content`
  30. to assign multiple categories to each written file.
  31. Returns a dictionary whose keys are in `categories` with values
  32. that are (title, path, category) tuples for the generated
  33. content files.
  34. """
  35. new_content = defaultdict(list)
  36. for _ in range(count):
  37. title = get_random_text_and_whitespace()
  38. category_choice = random.sample(categories, categories_per_content)
  39. categories_string = ', '.join(category_choice)
  40. output = TEMP_PAGE_TEMPLATE.format(
  41. title=title, category=categories_string)
  42. with tempfile.NamedTemporaryFile(
  43. dir=directory, mode='w', suffix='.md', delete=False) as tmp:
  44. tmp.write(output)
  45. path = os.path.join(directory, tmp.name)
  46. for each_cat in category_choice:
  47. new_content[(cc.substitute_category_name(each_cat), each_cat)]\
  48. .append(Content(title, path, categories_string))
  49. return new_content
  50. def get_random_text_and_whitespace(length=10):
  51. """
  52. Returns at most `length` randomly-generated letters and/or
  53. whitespace. The returned string will not begin or end in whitespace.
  54. """
  55. return "".join(random.sample(BLOG_CHARACTERS, length)).strip()
  56. def modified_pelican_run(self):
  57. """Runs the generators and returns the context object
  58. Modified from the Pelican object's run methods.
  59. """
  60. context = self.settings.copy()
  61. context['filenames'] = {} # share the dict between all the generators
  62. context['localsiteurl'] = self.settings['SITEURL'] # share
  63. generators = [
  64. cls(
  65. context=context,
  66. settings=self.settings,
  67. path=self.path,
  68. theme=self.theme,
  69. output_path=self.output_path,
  70. ) for cls in self.get_generator_classes()
  71. ]
  72. for p in generators:
  73. if hasattr(p, 'generate_context'):
  74. p.generate_context()
  75. writer = self.get_writer()
  76. for p in generators:
  77. if hasattr(p, 'generate_output'):
  78. p.generate_output(writer)
  79. next(g for g in generators if isinstance(g, ArticlesGenerator))
  80. next(g for g in generators if isinstance(g, PagesGenerator))
  81. return context
  82. class ContentCollationTester(unittest.TestCase):
  83. """Test generation of lists of content based on their Category metadata"""
  84. def setUp(self, settings_overrides=None, count=5,
  85. categories_per_content=1, categories=None):
  86. self.temp_input_dir = tempfile.mkdtemp(prefix="cc-input-")
  87. page_directory = os.path.join(self.temp_input_dir, 'pages')
  88. os.mkdir(page_directory)
  89. self.temp_output_dir = tempfile.mkdtemp(prefix="cc-output-")
  90. if categories is None:
  91. categories = [get_random_text_and_whitespace() for _ in range(5)]
  92. self.articles = make_content(
  93. self.temp_input_dir, categories, count=count,
  94. categories_per_content=categories_per_content)
  95. self.pages = make_content(
  96. page_directory, categories, count=count,
  97. categories_per_content=categories_per_content)
  98. settings = {
  99. 'PATH': self.temp_input_dir,
  100. 'PAGE_DIR': 'pages',
  101. 'OUTPUT_PATH': self.temp_output_dir,
  102. 'PLUGINS': [cc],
  103. 'DEFAULT_DATE': (2014, 6, 8),
  104. }
  105. if settings_overrides is not None:
  106. settings.update(settings_overrides)
  107. settings = read_settings(override=settings)
  108. pelican = Pelican(settings=settings)
  109. pelican.modified_run = modified_pelican_run
  110. self.collations = pelican.modified_run(pelican)['collations']
  111. def tearDown(self):
  112. shutil.rmtree(self.temp_input_dir)
  113. shutil.rmtree(self.temp_output_dir)
  114. class TestCollation(ContentCollationTester):
  115. """Test generation of lists of content based on their Category metadata"""
  116. def test_articles_with_one_category(self):
  117. for substituted_category, original_category in self.articles.keys():
  118. collation_key = '%s_articles' % substituted_category
  119. self.assertIn(collation_key, self.collations)
  120. collated_titles = [a.title for a in self.collations[collation_key]]
  121. for title, _, _ in self.articles[
  122. (substituted_category, original_category)]:
  123. self.assertIn(title, collated_titles)
  124. def test_pages_with_one_category(self):
  125. for substituted_category, original_category in self.pages.keys():
  126. collation_key = '%s_pages' % substituted_category
  127. self.assertIn(collation_key, self.collations)
  128. collated_titles = [a.title for a in self.collations[collation_key]]
  129. for title, _, _ in self.pages[
  130. (substituted_category, original_category)]:
  131. self.assertIn(title, collated_titles)
  132. class TestCollationAndMultipleCategories(TestCollation):
  133. """
  134. Test collate_content with multiple categories specified in each
  135. article and each page.
  136. """
  137. def setUp(self):
  138. categories = [get_random_text_and_whitespace() for _ in range(5)]
  139. ContentCollationTester.setUp(
  140. self, categories=categories, categories_per_content=3)
  141. class TestFilteredCategories(ContentCollationTester):
  142. """
  143. Test collate_content with the `CATEGORIES_TO_COLLATE` setting
  144. in effect
  145. """
  146. def setUp(self):
  147. categories = [get_random_text_and_whitespace() for _ in range(5)]
  148. self.retained_categories = categories[:2]
  149. override = {'CATEGORIES_TO_COLLATE': self.retained_categories}
  150. ContentCollationTester.setUp(
  151. self, settings_overrides=override, categories=categories)
  152. def test_articles_with_one_category_after_filtering(self):
  153. for substituted_category, original_category in self.articles.keys():
  154. collation_key = '%s_articles' % substituted_category
  155. if original_category not in self.retained_categories:
  156. self.assertNotIn(collation_key, self.collations)
  157. continue
  158. self.assertIn(collation_key, self.collations)
  159. collated_titles = [a.title for a in self.collations[collation_key]]
  160. for title, _, _ in self.articles[
  161. (substituted_category, original_category)]:
  162. self.assertIn(title, collated_titles)
  163. def test_pages_with_one_category_after_filtering(self):
  164. for substituted_category, original_category in self.pages.keys():
  165. collation_key = '%s_pages' % substituted_category
  166. if original_category not in self.retained_categories:
  167. self.assertNotIn(collation_key, self.collations)
  168. continue
  169. self.assertIn(collation_key, self.collations)
  170. collated_titles = [a.title for a in self.collations[collation_key]]
  171. for title, _, _ in self.pages[
  172. (substituted_category, original_category)]:
  173. self.assertIn(title, collated_titles)
  174. class TestFilteredAndMultipleCategories(TestFilteredCategories):
  175. """
  176. Test collate_content with the `CATEGORIES_TO_COLLATE` setting
  177. in effect as well as with multiple categories specified in each
  178. article and each page.
  179. """
  180. def setUp(self):
  181. categories = [get_random_text_and_whitespace() for _ in range(5)]
  182. self.retained_categories = categories[:2]
  183. override = {'CATEGORIES_TO_COLLATE': self.retained_categories}
  184. ContentCollationTester.setUp(
  185. self, settings_overrides=override, categories=categories,
  186. categories_per_content=3)
  187. if __name__ == '__main__':
  188. suite = unittest.TestLoader().loadTestsFromNames(['test_collate_content'])
  189. unittest.TextTestRunner(verbosity=1).run(suite)