/hyde/tests/ext/test_sorter.py

http://github.com/hyde/hyde · Python · 366 lines · 301 code · 59 blank · 6 comment · 9 complexity · 041ac637954fce67621724a4b638a346 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Use nose
  4. `$ pip install nose`
  5. `$ nosetests`
  6. """
  7. from hyde.ext.plugins.meta import 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, Folder
  12. import yaml
  13. TEST_SITE = File(__file__).parent.parent.child_folder('_test')
  14. class TestSorter(object):
  15. def setUp(self):
  16. TEST_SITE.make()
  17. TEST_SITE.parent.child_folder(
  18. 'sites/test_jinja').copy_contents_to(TEST_SITE)
  19. def tearDown(self):
  20. TEST_SITE.delete()
  21. def test_walk_resources_sorted(self):
  22. s = Site(TEST_SITE)
  23. s.load()
  24. s.config.plugins = ['hyde.ext.meta.SorterPlugin']
  25. s.config.sorter = Expando(dict(kind=dict(attr=['source_file.kind', 'name'])))
  26. SorterPlugin(s).begin_site()
  27. assert hasattr(s.content, 'walk_resources_sorted_by_kind')
  28. expected = ["404.html",
  29. "about.html",
  30. "apple-touch-icon.png",
  31. "merry-christmas.html",
  32. "crossdomain.xml",
  33. "favicon.ico",
  34. "robots.txt",
  35. "site.css"
  36. ]
  37. pages = [page.name for page in
  38. s.content.walk_resources_sorted_by_kind()]
  39. assert pages == sorted(expected, key=lambda f: (File(f).kind, f))
  40. def test_walk_resources_sorted_reverse(self):
  41. s = Site(TEST_SITE)
  42. s.load()
  43. s.config.plugins = ['hyde.ext.meta.SorterPlugin']
  44. s.config.sorter = Expando(dict(kind=dict(attr=['source_file.kind', 'name'], reverse=True)))
  45. SorterPlugin(s).begin_site()
  46. assert hasattr(s.content, 'walk_resources_sorted_by_kind')
  47. expected = ["404.html",
  48. "about.html",
  49. "apple-touch-icon.png",
  50. "merry-christmas.html",
  51. "crossdomain.xml",
  52. "favicon.ico",
  53. "robots.txt",
  54. "site.css"
  55. ]
  56. pages = [page.name for page in
  57. s.content.walk_resources_sorted_by_kind()]
  58. assert pages == sorted(expected, key=lambda f: (File(f).kind, f), reverse=True)
  59. def test_walk_resources_sorted_with_filters(self):
  60. s = Site(TEST_SITE)
  61. cfg = """
  62. plugins:
  63. - hyde.ext.meta.SorterPlugin
  64. sorter:
  65. kind2:
  66. filters:
  67. source_file.kind: html
  68. """
  69. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  70. s.load()
  71. SorterPlugin(s).begin_site()
  72. assert hasattr(s.content, 'walk_resources_sorted_by_kind2')
  73. expected = ["404.html",
  74. "about.html",
  75. "merry-christmas.html"
  76. ]
  77. pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()]
  78. assert pages == sorted(expected)
  79. def test_walk_resources_sorted_with_multiple_attributes(self):
  80. s = Site(TEST_SITE)
  81. cfg = """
  82. plugins:
  83. - hyde.ext.meta.SorterPlugin
  84. sorter:
  85. multi:
  86. attr:
  87. - source_file.kind
  88. - node.name
  89. - name
  90. """
  91. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  92. s.load()
  93. SorterPlugin(s).begin_site()
  94. assert hasattr(s.content, 'walk_resources_sorted_by_multi')
  95. expected = ["content/404.html",
  96. "content/about.html",
  97. "content/apple-touch-icon.png",
  98. "content/blog/2010/december/merry-christmas.html",
  99. "content/crossdomain.xml",
  100. "content/favicon.ico",
  101. "content/robots.txt",
  102. "content/site.css"
  103. ]
  104. pages = [page.name for page in s.content.walk_resources_sorted_by_multi()]
  105. expected_sorted = [File(page).name
  106. for page in
  107. sorted(expected,
  108. key=lambda p: tuple(
  109. [File(p).kind,
  110. File(p).parent.name, p]))]
  111. assert pages == expected_sorted
  112. def test_walk_resources_sorted_no_default_is_processable(self):
  113. s = Site(TEST_SITE)
  114. cfg = """
  115. plugins:
  116. - hyde.ext.meta.SorterPlugin
  117. sorter:
  118. kind2:
  119. filters:
  120. source_file.kind: html
  121. attr:
  122. - name
  123. """
  124. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  125. s.load()
  126. p_404 = s.content.resource_from_relative_path('404.html')
  127. p_404.is_processable = False
  128. SorterPlugin(s).begin_site()
  129. assert hasattr(s.content, 'walk_resources_sorted_by_kind2')
  130. expected = ["404.html", "about.html", "merry-christmas.html"]
  131. pages = [page.name for page in s.content.walk_resources_sorted_by_kind2()]
  132. assert pages == sorted(expected)
  133. def test_prev_next(self):
  134. s = Site(TEST_SITE)
  135. cfg = """
  136. plugins:
  137. - hyde.ext.meta.SorterPlugin
  138. sorter:
  139. kind2:
  140. filters:
  141. source_file.kind: html
  142. attr:
  143. - name
  144. """
  145. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  146. s.load()
  147. SorterPlugin(s).begin_site()
  148. p_404 = s.content.resource_from_relative_path('404.html')
  149. p_about = s.content.resource_from_relative_path('about.html')
  150. p_mc = s.content.resource_from_relative_path(
  151. 'blog/2010/december/merry-christmas.html')
  152. assert hasattr(p_404, 'prev_by_kind2')
  153. assert not p_404.prev_by_kind2
  154. assert hasattr(p_404, 'next_by_kind2')
  155. assert p_404.next_by_kind2 == p_about
  156. assert hasattr(p_about, 'prev_by_kind2')
  157. assert p_about.prev_by_kind2 == p_404
  158. assert hasattr(p_about, 'next_by_kind2')
  159. assert p_about.next_by_kind2 == p_mc
  160. assert hasattr(p_mc, 'prev_by_kind2')
  161. assert p_mc.prev_by_kind2 == p_about
  162. assert hasattr(p_mc, 'next_by_kind2')
  163. assert not p_mc.next_by_kind2
  164. def test_prev_next_looped(self):
  165. s = Site(TEST_SITE)
  166. cfg = """
  167. plugins:
  168. - hyde.ext.meta.SorterPlugin
  169. sorter:
  170. kind2:
  171. circular: true
  172. filters:
  173. source_file.kind: html
  174. attr:
  175. - name
  176. """
  177. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  178. s.load()
  179. SorterPlugin(s).begin_site()
  180. p_404 = s.content.resource_from_relative_path('404.html')
  181. p_about = s.content.resource_from_relative_path('about.html')
  182. p_mc = s.content.resource_from_relative_path(
  183. 'blog/2010/december/merry-christmas.html')
  184. assert hasattr(p_404, 'prev_by_kind2')
  185. assert p_404.prev_by_kind2 == p_mc
  186. assert hasattr(p_404, 'next_by_kind2')
  187. assert p_404.next_by_kind2 == p_about
  188. assert hasattr(p_about, 'prev_by_kind2')
  189. assert p_about.prev_by_kind2 == p_404
  190. assert hasattr(p_about, 'next_by_kind2')
  191. assert p_about.next_by_kind2 == p_mc
  192. assert hasattr(p_mc, 'prev_by_kind2')
  193. assert p_mc.prev_by_kind2 == p_about
  194. assert hasattr(p_mc, 'next_by_kind2')
  195. assert p_mc.next_by_kind2 == p_404
  196. def test_prev_next_reversed(self):
  197. s = Site(TEST_SITE)
  198. cfg = """
  199. plugins:
  200. - hyde.ext.meta.SorterPlugin
  201. sorter:
  202. folder_name:
  203. attr:
  204. - node.name
  205. reverse: True
  206. filters:
  207. source_file.kind: html
  208. """
  209. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  210. s.load()
  211. SorterPlugin(s).begin_site()
  212. p_404 = s.content.resource_from_relative_path('404.html')
  213. p_about = s.content.resource_from_relative_path('about.html')
  214. p_mc = s.content.resource_from_relative_path(
  215. 'blog/2010/december/merry-christmas.html')
  216. assert hasattr(p_mc, 'prev_by_folder_name')
  217. assert not p_mc.prev_by_folder_name
  218. assert hasattr(p_mc, 'next_by_folder_name')
  219. assert p_mc.next_by_folder_name == p_404
  220. assert hasattr(p_404, 'prev_by_folder_name')
  221. assert p_404.prev_by_folder_name == p_mc
  222. assert hasattr(p_404, 'next_by_folder_name')
  223. assert p_404.next_by_folder_name == p_about
  224. assert hasattr(p_about, 'prev_by_folder_name')
  225. assert p_about.prev_by_folder_name == p_404
  226. assert hasattr(p_about, 'next_by_folder_name')
  227. assert not p_about.next_by_folder_name
  228. def test_walk_resources_sorted_using_generator(self):
  229. s = Site(TEST_SITE)
  230. cfg = """
  231. meta:
  232. time: !!timestamp 2010-10-23
  233. title: NahNahNah
  234. plugins:
  235. - hyde.ext.plugins.meta.MetaPlugin
  236. - hyde.ext.plugins.meta.SorterPlugin
  237. sorter:
  238. time:
  239. attr: meta.time
  240. filters:
  241. source_file.kind: html
  242. """
  243. s.config = Config(TEST_SITE, config_dict=yaml.load(cfg))
  244. text = """
  245. ---
  246. time: !!timestamp 2010-12-31
  247. title: YayYayYay
  248. ---
  249. {% extends "base.html" %}
  250. {% block main %}
  251. {% set latest = site.content.walk_resources_sorted_by_time()|reverse|first %}
  252. <span class="latest">{{ latest.meta.title }}</span>
  253. {% endblock %}
  254. """
  255. about2 = File(TEST_SITE.child('content/about2.html'))
  256. about2.write(text)
  257. gen = Generator(s)
  258. gen.generate_all()
  259. from pyquery import PyQuery
  260. target = File(Folder(s.config.deploy_root_path).child('about2.html'))
  261. text = target.read_all()
  262. q = PyQuery(text)
  263. assert q('span.latest').text() == 'YayYayYay'
  264. class TestSorterMeta(object):
  265. def setUp(self):
  266. TEST_SITE.make()
  267. TEST_SITE.parent.child_folder(
  268. 'sites/test_sorter').copy_contents_to(TEST_SITE)
  269. def tearDown(self):
  270. TEST_SITE.delete()
  271. def test_attribute_checker_no_meta(self):
  272. s = Site(TEST_SITE)
  273. s.load()
  274. from hyde.ext.plugins.meta import attributes_checker
  275. for r in s.content.walk_resources():
  276. assert not attributes_checker(r, ['meta.index'])
  277. def test_attribute_checker_with_meta(self):
  278. s = Site(TEST_SITE)
  279. s.load()
  280. MetaPlugin(s).begin_site()
  281. from hyde.ext.plugins.meta import attributes_checker
  282. have_index = ["angry-post.html",
  283. "another-sad-post.html",
  284. "happy-post.html"]
  285. for r in s.content.walk_resources():
  286. expected = r.name in have_index
  287. assert attributes_checker(r, ['meta.index']) == expected
  288. def test_walk_resources_sorted_by_index(self):
  289. s = Site(TEST_SITE)
  290. s.load()
  291. config = {
  292. "index": {
  293. "attr": ['meta.index', 'name']
  294. }
  295. }
  296. s.config.sorter = Expando(config)
  297. MetaPlugin(s).begin_site()
  298. SorterPlugin(s).begin_site()
  299. assert hasattr(s.content, 'walk_resources_sorted_by_index')
  300. expected = ["angry-post.html",
  301. "another-sad-post.html",
  302. "happy-post.html"]
  303. pages = [page.name for page in
  304. s.content.walk_resources_sorted_by_index()]
  305. assert pages == sorted(expected, key=lambda f: (File(f).kind, f))