diff tests/test_serving.py @ 324:65e6d72f3877

bake/serve: Fix how taxonomy index pages are setup and rendered. * Properly use the taxonomy's setting name where appropriate. * Delete duplicated (and sometimes incorrect) code in 2 places to setup filtering on an index page and consolidate it on the `PageRenderingContext`. * Add unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 29 Mar 2015 23:08:37 -0700
parents d47d9493bb0a
children d8c28e496bb3
line wrap: on
line diff
--- a/tests/test_serving.py	Sun Mar 29 23:05:03 2015 -0700
+++ b/tests/test_serving.py	Sun Mar 29 23:08:37 2015 -0700
@@ -1,8 +1,13 @@
 import re
 import pytest
 import mock
+from piecrust.data.filters import (
+        PaginationFilter, HasFilterClause, IsFilterClause,
+        page_value_accessor)
+from piecrust.rendering import PageRenderingContext, render_page
 from piecrust.serving import find_routes
 from piecrust.sources.base import REALM_USER, REALM_THEME
+from .mockutil import mock_fs, mock_fs_scope
 
 
 @pytest.mark.parametrize('uri, route_specs, expected',
@@ -33,3 +38,93 @@
         assert route.source_name == exp_source
         assert metadata == exp_md
 
+
+@pytest.mark.parametrize(
+        'tag, expected_indices',
+        [
+            ('foo', [1, 2, 4, 5, 6]),
+            ('bar', [2, 3, 4, 6, 8]),
+            ('whatever', [5, 8]),
+            ('unique', [7]),
+            ('missing', None)
+        ])
+def test_serve_tag_page(tag, expected_indices):
+    tags = [
+            ['foo'],
+            ['foo', 'bar'],
+            ['bar'],
+            ['bar', 'foo'],
+            ['foo', 'whatever'],
+            ['foo', 'bar'],
+            ['unique'],
+            ['whatever', 'bar']]
+
+    def config_factory(i):
+        c = {'title': 'Post %d' % (i + 1)}
+        c['tags'] = list(tags[i])
+        return c
+
+    fs = (mock_fs()
+          .withPages(8, 'posts/2015-03-{idx1:02}_post{idx1:02}.md',
+                     config_factory)
+          .withPage('pages/_tag.md', {'layout': 'none', 'format': 'none'},
+                    "Pages in {{tag}}\n"
+                    "{%for p in pagination.posts -%}\n"
+                    "{{p.title}}\n"
+                    "{%endfor%}"))
+    with mock_fs_scope(fs):
+        app = fs.getApp()
+        page = app.getSource('pages').getPage({'slug': '_tag'})
+        taxonomy = app.getTaxonomy('tags')
+
+        ctx = PageRenderingContext(page, '/tag/' + tag)
+        ctx.setTaxonomyFilter(taxonomy, tag)
+        rp = render_page(ctx)
+
+        expected = "Pages in %s\n" % tag
+        if expected_indices:
+            for i in reversed(expected_indices):
+                expected += "Post %d\n" % i
+        assert expected == rp.content
+
+
+@pytest.mark.parametrize(
+        'category, expected_indices',
+        [
+            ('foo', [1, 2, 4]),
+            ('bar', [3, 6]),
+            ('missing', None)
+        ])
+def test_serve_category_page(category, expected_indices):
+    categories = [
+            'foo', 'foo', 'bar', 'foo', None, 'bar']
+
+    def config_factory(i):
+        c = {'title': 'Post %d' % (i + 1)}
+        if categories[i]:
+            c['category'] = categories[i]
+        return c
+
+    fs = (mock_fs()
+          .withPages(6, 'posts/2015-03-{idx1:02}_post{idx1:02}.md',
+                     config_factory)
+          .withPage('pages/_category.md', {'layout': 'none', 'format': 'none'},
+                    "Pages in {{category}}\n"
+                    "{%for p in pagination.posts -%}\n"
+                    "{{p.title}}\n"
+                    "{%endfor%}"))
+    with mock_fs_scope(fs):
+        app = fs.getApp()
+        page = app.getSource('pages').getPage({'slug': '_category'})
+        taxonomy = app.getTaxonomy('categories')
+
+        ctx = PageRenderingContext(page, '/' + category)
+        ctx.setTaxonomyFilter(taxonomy, category)
+        rp = render_page(ctx)
+
+        expected = "Pages in %s\n" % category
+        if expected_indices:
+            for i in reversed(expected_indices):
+                expected += "Post %d\n" % i
+        assert expected == rp.content
+