test_summary.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 summary
  9. class TestSummary(unittest.TestCase):
  10. def setUp(self):
  11. super(TestSummary, self).setUp()
  12. summary.register()
  13. summary.initialized(None)
  14. self.page_kwargs = {
  15. 'content': TEST_CONTENT,
  16. 'context': {
  17. 'localsiteurl': '',
  18. },
  19. 'metadata': {
  20. 'summary': TEST_SUMMARY,
  21. 'title': 'foo bar',
  22. 'author': 'Blogger',
  23. },
  24. }
  25. def _copy_page_kwargs(self):
  26. # make a deep copy of page_kwargs
  27. page_kwargs = dict([(key, self.page_kwargs[key]) for key in
  28. self.page_kwargs])
  29. for key in page_kwargs:
  30. if not isinstance(page_kwargs[key], dict):
  31. break
  32. page_kwargs[key] = dict([(subkey, page_kwargs[key][subkey])
  33. for subkey in page_kwargs[key]])
  34. return page_kwargs
  35. def test_end_summary(self):
  36. page_kwargs = self._copy_page_kwargs()
  37. del page_kwargs['metadata']['summary']
  38. page_kwargs['content'] = (
  39. TEST_SUMMARY + '<!-- PELICAN_END_SUMMARY -->' + TEST_CONTENT)
  40. page = Page(**page_kwargs)
  41. # test both the summary and the marker removal
  42. self.assertEqual(page.summary, TEST_SUMMARY)
  43. self.assertEqual(page.content, TEST_SUMMARY + TEST_CONTENT)
  44. def test_begin_summary(self):
  45. page_kwargs = self._copy_page_kwargs()
  46. del page_kwargs['metadata']['summary']
  47. page_kwargs['content'] = (
  48. 'FOOBAR<!-- PELICAN_BEGIN_SUMMARY -->' + TEST_CONTENT)
  49. page = Page(**page_kwargs)
  50. # test both the summary and the marker removal
  51. self.assertEqual(page.summary, TEST_CONTENT)
  52. self.assertEqual(page.content, 'FOOBAR' + TEST_CONTENT)
  53. def test_begin_end_summary(self):
  54. page_kwargs = self._copy_page_kwargs()
  55. del page_kwargs['metadata']['summary']
  56. page_kwargs['content'] = (
  57. 'FOOBAR<!-- PELICAN_BEGIN_SUMMARY -->' + TEST_SUMMARY +
  58. '<!-- PELICAN_END_SUMMARY -->' + TEST_CONTENT)
  59. page = Page(**page_kwargs)
  60. # test both the summary and the marker removal
  61. self.assertEqual(page.summary, TEST_SUMMARY)
  62. self.assertEqual(page.content, 'FOOBAR' + TEST_SUMMARY + TEST_CONTENT)