PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/hyde/tests/ext/test_grouper.py

http://github.com/hyde/hyde
Python | 322 lines | 310 code | 6 blank | 6 comment | 0 complexity | af61a4688ebcc81223415132ddbd41f9 MD5 | raw file
Possible License(s): MIT
  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.ext.plugins.meta import GrouperPlugin, MetaPlugin, SorterPlugin
  8. from hyde.generator import Generator
  9. from hyde.site import Site
  10. from hyde.model import Config, Expando
  11. from fswrap import File
  12. from hyde.tests.util import assert_html_equals
  13. import yaml
  14. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  15. class TestGrouperSingleLevel(object):
  16. def setUp(self):
  17. TEST_SITE.make()
  18. TEST_SITE.parent.child_folder(
  19. 'sites/test_grouper').copy_contents_to(TEST_SITE)
  20. self.s = Site(TEST_SITE)
  21. cfg = """
  22. nodemeta: meta.yaml
  23. plugins:
  24. - hyde.ext.plugins.meta.MetaPlugin
  25. - hyde.ext.plugins.meta.SorterPlugin
  26. - hyde.ext.plugins.meta.GrouperPlugin
  27. sorter:
  28. kind:
  29. attr:
  30. - source_file.kind
  31. filters:
  32. is_processable: True
  33. grouper:
  34. section:
  35. description: Sections in the site
  36. sorter: kind
  37. groups:
  38. -
  39. name: start
  40. description: Getting Started
  41. -
  42. name: plugins
  43. description: Plugins
  44. """
  45. self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  46. self.s.load()
  47. MetaPlugin(self.s).begin_site()
  48. SorterPlugin(self.s).begin_site()
  49. GrouperPlugin(self.s).begin_site()
  50. self.all = ['installation.html', 'overview.html', 'templating.html', 'plugins.html', 'tags.html']
  51. self.start = ['installation.html', 'overview.html', 'templating.html']
  52. self.plugins = ['plugins.html', 'tags.html']
  53. self.section = self.all
  54. def tearDown(self):
  55. TEST_SITE.delete()
  56. def test_site_grouper_groups(self):
  57. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  58. assert len(groups) == 2
  59. assert 'start' in groups
  60. assert 'plugins' in groups
  61. def test_site_grouper_walk_groups(self):
  62. groups = dict([(g.name, g) for g in self.s.grouper['section'].walk_groups()])
  63. assert len(groups) == 3
  64. assert 'section' in groups
  65. assert 'start' in groups
  66. assert 'plugins' in groups
  67. def test_walk_section_groups(self):
  68. assert hasattr(self.s.content, 'walk_section_groups')
  69. groups = dict([(grouper.group.name, grouper) for grouper in self.s.content.walk_section_groups()])
  70. assert len(groups) == 3
  71. assert 'section' in groups
  72. assert 'start' in groups
  73. assert 'plugins' in groups
  74. for name in ['start', 'plugins']:
  75. res = [resource.name for resource in groups[name].resources]
  76. assert res == getattr(self, name)
  77. def test_walk_start_groups(self):
  78. assert hasattr(self.s.content, 'walk_start_groups')
  79. groups = dict([(g.name, g) for g, resources in self.s.content.walk_start_groups()])
  80. assert len(groups) == 1
  81. assert 'start' in groups
  82. def test_walk_plugins_groups(self):
  83. assert hasattr(self.s.content, 'walk_plugins_groups')
  84. groups = dict([(g.name, g) for g, resources in self.s.content.walk_plugins_groups()])
  85. assert len(groups) == 1
  86. assert 'plugins' in groups
  87. def test_walk_section_resources(self):
  88. assert hasattr(self.s.content, 'walk_resources_grouped_by_section')
  89. resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_section()]
  90. assert resources == self.all
  91. def test_walk_start_resources(self):
  92. assert hasattr(self.s.content, 'walk_resources_grouped_by_start')
  93. start_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_start()]
  94. assert start_resources == self.start
  95. def test_walk_plugins_resources(self):
  96. assert hasattr(self.s.content, 'walk_resources_grouped_by_plugins')
  97. plugin_resources = [resource.name for resource in self.s.content.walk_resources_grouped_by_plugins()]
  98. assert plugin_resources == self.plugins
  99. def test_resource_group(self):
  100. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  101. for name, group in groups.items():
  102. pages = getattr(self, name)
  103. for page in pages:
  104. res = self.s.content.resource_from_relative_path('blog/' + page)
  105. assert hasattr(res, 'section_group')
  106. res_group = getattr(res, 'section_group')
  107. assert res_group == group
  108. def test_resource_belongs_to(self):
  109. groups = dict([(g.name, g) for g in self.s.grouper['section'].groups])
  110. for name, group in groups.items():
  111. pages = getattr(self, name)
  112. for page in pages:
  113. res = self.s.content.resource_from_relative_path('blog/' + page)
  114. res_groups = getattr(res, 'walk_%s_groups' % name)()
  115. assert group in res_groups
  116. def test_prev_next(self):
  117. resources = []
  118. for page in self.all:
  119. resources.append(self.s.content.resource_from_relative_path('blog/' + page))
  120. index = 0
  121. for res in resources:
  122. if index < 4:
  123. assert res.next_in_section.name == self.all[index + 1]
  124. else:
  125. assert not res.next_in_section
  126. index += 1
  127. index = 0
  128. for res in resources:
  129. if index:
  130. assert res.prev_in_section.name == self.all[index - 1]
  131. else:
  132. assert not res.prev_in_section
  133. index += 1
  134. def test_nav_with_grouper(self):
  135. text ="""
  136. {% for group, resources in site.content.walk_section_groups() %}
  137. <ul>
  138. <li>
  139. <h2>{{ group.name|title }}</h2>
  140. <h3>{{ group.description }}</h3>
  141. <ul class="links">
  142. {% for resource in resources %}
  143. <li>{{resource.name}}</li>
  144. {% endfor %}
  145. </ul>
  146. </li>
  147. </ul>
  148. {% endfor %}
  149. """
  150. expected = """
  151. <ul>
  152. <li>
  153. <h2>Section</h2>
  154. <h3>Sections in the site</h3>
  155. <ul class="links"></ul>
  156. </li>
  157. </ul>
  158. <ul>
  159. <li>
  160. <h2>Start</h2>
  161. <h3>Getting Started</h3>
  162. <ul class="links">
  163. <li>installation.html</li>
  164. <li>overview.html</li>
  165. <li>templating.html</li>
  166. </ul>
  167. </li>
  168. </ul>
  169. <ul>
  170. <li>
  171. <h2>Plugins</h2>
  172. <h3>Plugins</h3>
  173. <ul class="links">
  174. <li>plugins.html</li>
  175. <li>tags.html</li>
  176. </ul>
  177. </li>
  178. </ul>
  179. """
  180. gen = Generator(self.s)
  181. gen.load_site_if_needed()
  182. gen.load_template_if_needed()
  183. out = gen.template.render(text, {'site':self.s})
  184. assert_html_equals(out, expected)
  185. def test_nav_with_grouper_sorted(self):
  186. cfg = """
  187. nodemeta: meta.yaml
  188. plugins:
  189. - hyde.ext.plugins.meta.MetaPlugin
  190. - hyde.ext.plugins.meta.SorterPlugin
  191. - hyde.ext.plugins.meta.GrouperPlugin
  192. sorter:
  193. kind:
  194. attr:
  195. - source_file.kind
  196. filters:
  197. is_processable: True
  198. grouper:
  199. section:
  200. description: Sections in the site
  201. sorter: kind
  202. groups:
  203. -
  204. name: start
  205. description: Getting Started
  206. -
  207. name: awesome
  208. description: Awesome
  209. -
  210. name: plugins
  211. description: Plugins
  212. """
  213. self.s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  214. self.s.load()
  215. MetaPlugin(self.s).begin_site()
  216. SorterPlugin(self.s).begin_site()
  217. GrouperPlugin(self.s).begin_site()
  218. text ="""
  219. {% set sorted = site.grouper['section'].groups|sort(attribute='name') %}
  220. {% for group in sorted %}
  221. <ul>
  222. <li>
  223. <h2>{{ group.name|title }}</h2>
  224. <h3>{{ group.description }}</h3>
  225. <ul class="links">
  226. {% for resource in group.walk_resources_in_node(site.content) %}
  227. <li>{{resource.name}}</li>
  228. {% endfor %}
  229. </ul>
  230. </li>
  231. </ul>
  232. {% endfor %}
  233. """
  234. expected = """
  235. <ul>
  236. <li>
  237. <h2>Awesome</h2>
  238. <h3>Awesome</h3>
  239. <ul class="links">
  240. </ul>
  241. </li>
  242. </ul>
  243. <ul>
  244. <li>
  245. <h2>Plugins</h2>
  246. <h3>Plugins</h3>
  247. <ul class="links">
  248. <li>plugins.html</li>
  249. <li>tags.html</li>
  250. </ul>
  251. </li>
  252. </ul>
  253. <ul>
  254. <li>
  255. <h2>Start</h2>
  256. <h3>Getting Started</h3>
  257. <ul class="links">
  258. <li>installation.html</li>
  259. <li>overview.html</li>
  260. <li>templating.html</li>
  261. </ul>
  262. </li>
  263. </ul>
  264. """
  265. self.s.config.grouper.section.groups.append(Expando({"name": "awesome", "description": "Aweesoome"}));
  266. gen = Generator(self.s)
  267. gen.load_site_if_needed()
  268. gen.load_template_if_needed()
  269. out = gen.template.render(text, {'site':self.s})
  270. assert_html_equals(out, expected)