/hyde/tests/ext/test_tagger.py

http://github.com/hyde/hyde · Python · 226 lines · 177 code · 43 blank · 6 comment · 13 complexity · f656a249547c047401f19bdef030061c MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.generator import Generator
  8. from hyde.site import Site
  9. from fswrap import File
  10. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  11. class TestTagger(object):
  12. def setUp(self):
  13. TEST_SITE.make()
  14. TEST_SITE.parent.child_folder(
  15. 'sites/test_tagger').copy_contents_to(TEST_SITE)
  16. self.s = Site(TEST_SITE)
  17. self.deploy = TEST_SITE.child_folder('deploy')
  18. def tearDown(self):
  19. TEST_SITE.delete()
  20. def test_tagger_walker(self):
  21. gen = Generator(self.s)
  22. gen.load_site_if_needed()
  23. gen.generate_all()
  24. assert hasattr(self.s, 'tagger')
  25. assert hasattr(self.s.tagger, 'tags')
  26. assert self.s.tagger.tags
  27. tags = self.s.tagger.tags.to_dict()
  28. assert len(tags) == 6
  29. for tag in ['sad', 'happy', 'angry', 'thoughts', 'events']:
  30. assert tag in tags
  31. sad_posts = [post.name for post in tags['sad']['resources']]
  32. assert len(sad_posts) == 2
  33. assert "sad-post.html" in sad_posts
  34. assert "another-sad-post.html" in sad_posts
  35. sad_posts == [post.name for post in
  36. self.s.content.walk_resources_tagged_with('sad')]
  37. happy_posts = [post.name for post in
  38. self.s.content.walk_resources_tagged_with('happy')]
  39. assert len(happy_posts) == 1
  40. assert "happy-post.html" in happy_posts
  41. angry_posts = [post.name for post in
  42. self.s.content.walk_resources_tagged_with('angry')]
  43. assert len(angry_posts) == 1
  44. assert "angry-post.html" in angry_posts
  45. sad_thought_posts = [post.name for post in
  46. self.s.content.walk_resources_tagged_with('sad+thoughts')]
  47. assert len(sad_thought_posts) == 1
  48. assert "sad-post.html" in sad_thought_posts
  49. def test_tagger_archives_generated(self):
  50. gen = Generator(self.s)
  51. gen.load_site_if_needed()
  52. gen.load_template_if_needed()
  53. gen.generate_all()
  54. tags_folder = self.deploy.child_folder('blog/tags')
  55. assert tags_folder.exists
  56. tags = ['sad', 'happy', 'angry', 'thoughts', 'events']
  57. archives = (File(tags_folder.child("%s.html" % tag)) for tag in tags)
  58. for archive in archives:
  59. assert archive.exists
  60. from pyquery import PyQuery
  61. q = PyQuery(File(tags_folder.child('sad.html')).read_all())
  62. assert q
  63. assert q('li').length == 2
  64. assert q('li:nth-child(1) a').attr('href') == '/blog/another-sad-post.html'
  65. assert q('li:nth-child(2) a').attr('href') == '/blog/sad-post.html'
  66. q = PyQuery(File(tags_folder.child('happy.html')).read_all())
  67. assert q
  68. assert q('li').length == 1
  69. assert q('li a:first-child').attr('href') == '/blog/happy-post.html'
  70. q = PyQuery(File(tags_folder.child('angry.html')).read_all())
  71. assert q
  72. assert q('li').length == 1
  73. assert q('li a:first-child').attr('href') == '/blog/angry-post.html'
  74. q = PyQuery(File(tags_folder.child('thoughts.html')).read_all())
  75. assert q
  76. assert q('li').length == 3
  77. assert q('li:nth-child(1) a').attr('href') == '/blog/happy-post.html'
  78. assert q('li:nth-child(2) a').attr('href') == '/blog/angry-post.html'
  79. assert q('li:nth-child(3) a').attr('href') == '/blog/sad-post.html'
  80. q = PyQuery(File(tags_folder.child('events.html')).read_all())
  81. assert q
  82. assert q('li').length == 1
  83. assert q('li a:first-child').attr('href') == '/blog/another-sad-post.html'
  84. def test_tagger_metadata(self):
  85. conf = {
  86. "tagger":{
  87. "tags": {
  88. "sad" : {
  89. "emotions": ["Dissappointed", "Lost"]
  90. },
  91. "angry": {
  92. "emotions": ["Irritated", "Annoyed", "Disgusted"]
  93. }
  94. }
  95. }
  96. }
  97. s = Site(TEST_SITE)
  98. s.config.update(conf)
  99. gen = Generator(s)
  100. gen.load_site_if_needed()
  101. gen.generate_all()
  102. assert hasattr(s, 'tagger')
  103. assert hasattr(s.tagger, 'tags')
  104. assert s.tagger.tags
  105. tags = s.tagger.tags
  106. sad_tag = tags.sad
  107. assert hasattr(sad_tag, "emotions")
  108. assert sad_tag.emotions == s.config.tagger.tags.sad.emotions
  109. assert hasattr(tags, "angry")
  110. angry_tag = tags.angry
  111. assert angry_tag
  112. assert hasattr(angry_tag, "emotions")
  113. assert angry_tag.emotions == s.config.tagger.tags.angry.emotions
  114. for tagname in ['happy', 'thoughts', 'events']:
  115. tag = getattr(tags, tagname)
  116. assert tag
  117. assert not hasattr(tag, "emotions")
  118. def test_tagger_sorted(self):
  119. conf = {
  120. "tagger":{
  121. "sorter": "time",
  122. "archives": {
  123. "blog": {
  124. "template": "emotions.j2",
  125. "source": "blog",
  126. "target": "blog/tags",
  127. "extension": "html",
  128. "meta": {
  129. "author": "Tagger Plugin"
  130. }
  131. }
  132. },
  133. "tags": {
  134. "sad" : {
  135. "emotions": ["Dissappointed", "Lost"]
  136. },
  137. "angry": {
  138. "emotions": ["Irritated", "Annoyed", "Disgusted"]
  139. }
  140. }
  141. }
  142. }
  143. text = """
  144. <div id="author">{{ resource.meta.author }}</div>
  145. <h1>Posts tagged: {{ tag }} in {{ node.name|title }}</h1>
  146. Emotions:
  147. <ul>
  148. {% for emotion in tag.emotions %}
  149. <li class="emotion">
  150. {{ emotion }}
  151. </li>
  152. {% endfor %}
  153. <ul>
  154. {% for resource in walker() -%}
  155. <li>
  156. <a href="{{ content_url(resource.url) }}">{{ resource.meta.title }}</a>
  157. </li>
  158. {%- endfor %}
  159. </ul>
  160. """
  161. template = File(TEST_SITE.child('layout/emotions.j2'))
  162. template.write(text)
  163. s = Site(TEST_SITE)
  164. s.config.update(conf)
  165. gen = Generator(s)
  166. gen.load_site_if_needed()
  167. gen.generate_all()
  168. tags_folder = self.deploy.child_folder('blog/tags')
  169. assert tags_folder.exists
  170. tags = ['sad', 'happy', 'angry', 'thoughts', 'events']
  171. archives = dict((tag, File(tags_folder.child("%s.html" % tag))) for tag in tags)
  172. for tag, archive in archives.items():
  173. assert archive.exists
  174. from pyquery import PyQuery
  175. q = PyQuery(archives['sad'].read_all())
  176. assert len(q("li.emotion")) == 2
  177. assert q("#author").text() == "Tagger Plugin"
  178. q = PyQuery(archives['angry'].read_all())
  179. assert len(q("li.emotion")) == 3
  180. for tag, archive in archives.items():
  181. if tag not in ["sad", "angry"]:
  182. q = PyQuery(archives[tag].read_all())
  183. assert not len(q("li.emotion"))