test_summary.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. from jinja2.utils import generate_lorem_ipsum
  4. # generate one paragraph, enclosed with <p>
  5. TEST_CONTENT = str(generate_lorem_ipsum(n=1))
  6. TEST_SUMMARY = generate_lorem_ipsum(n=1, html=False)
  7. from pelican.contents import Page
  8. import pelican.settings
  9. import summary
  10. class TestSummary(unittest.TestCase):
  11. def setUp(self):
  12. super(TestSummary, self).setUp()
  13. pelican.settings.DEFAULT_CONFIG['SUMMARY_MAX_LENGTH'] = None
  14. pelican.settings.DEFAULT_CONFIG['SUMMARY_USE_FIRST_PARAGRAPH'] = False
  15. summary.register()
  16. summary.initialized(None)
  17. self.page_kwargs = {
  18. 'content': TEST_CONTENT,
  19. 'context': {
  20. 'localsiteurl': '',
  21. },
  22. 'metadata': {
  23. 'summary': TEST_SUMMARY,
  24. 'title': 'foo bar',
  25. 'author': 'Blogger',
  26. },
  27. }
  28. def _copy_page_kwargs(self):
  29. # make a deep copy of page_kwargs
  30. page_kwargs = dict([(key, self.page_kwargs[key]) for key in
  31. self.page_kwargs])
  32. for key in page_kwargs:
  33. if not isinstance(page_kwargs[key], dict):
  34. break
  35. page_kwargs[key] = dict([(subkey, page_kwargs[key][subkey])
  36. for subkey in page_kwargs[key]])
  37. return page_kwargs
  38. def test_end_summary(self):
  39. page_kwargs = self._copy_page_kwargs()
  40. del page_kwargs['metadata']['summary']
  41. page_kwargs['content'] = (
  42. TEST_SUMMARY + '<!-- PELICAN_END_SUMMARY -->' + TEST_CONTENT)
  43. page = Page(**page_kwargs)
  44. summary.extract_summary(page)
  45. # test both the summary and the marker removal
  46. self.assertEqual(page.summary, TEST_SUMMARY)
  47. self.assertEqual(page.content, TEST_SUMMARY + TEST_CONTENT)
  48. def test_begin_summary(self):
  49. page_kwargs = self._copy_page_kwargs()
  50. del page_kwargs['metadata']['summary']
  51. page_kwargs['content'] = (
  52. 'FOOBAR<!-- PELICAN_BEGIN_SUMMARY -->' + TEST_CONTENT)
  53. page = Page(**page_kwargs)
  54. summary.extract_summary(page)
  55. # test both the summary and the marker removal
  56. self.assertEqual(page.summary, TEST_CONTENT)
  57. self.assertEqual(page.content, 'FOOBAR' + TEST_CONTENT)
  58. def test_begin_end_summary(self):
  59. page_kwargs = self._copy_page_kwargs()
  60. del page_kwargs['metadata']['summary']
  61. page_kwargs['content'] = (
  62. 'FOOBAR<!-- PELICAN_BEGIN_SUMMARY -->' + TEST_SUMMARY +
  63. '<!-- PELICAN_END_SUMMARY -->' + TEST_CONTENT)
  64. page = Page(**page_kwargs)
  65. summary.extract_summary(page)
  66. # test both the summary and the marker removal
  67. self.assertEqual(page.summary, TEST_SUMMARY)
  68. self.assertEqual(page.content, 'FOOBAR' + TEST_SUMMARY + TEST_CONTENT)
  69. def test_use_first_paragraph(self):
  70. page_kwargs = self._copy_page_kwargs()
  71. del page_kwargs['metadata']['summary']
  72. pelican.settings.DEFAULT_CONFIG['SUMMARY_USE_FIRST_PARAGRAPH'] = True
  73. page_kwargs['content'] = '<p>' + TEST_SUMMARY + '</p>' + TEST_CONTENT
  74. page = Page(**page_kwargs)
  75. summary.extract_summary(page)
  76. # test both the summary and the marker removal
  77. self.assertEqual(page.summary, TEST_SUMMARY)
  78. self.assertEqual(page.content, '<p>' + TEST_SUMMARY + '</p>' + TEST_CONTENT)
  79. if __name__ == '__main__':
  80. unittest.main()