PageRenderTime 42ms CodeModel.GetById 16ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/tests/ext/test_sorter.py

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