view tests/test_data_linker.py @ 380:f33712c4cfab

routing: Fix bugs with matching URLs with correct route but missing metadata. When matching a route like `/foo/%slug%` against an URL like `/foo`, the route will (correctly) return a match, but it will be completely missing the `slug` metadata, resulting in problems elsewhere. This change makes it so that any missing route metadata will be filled in with an empty string. And because this means generated URLs may differ from the incoming URL when using trailing slashes (`/foo/` _vs._ `/foo`), we make the assert in the chef server handle those discrepancies.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 10 May 2015 00:34:21 -0700
parents b51ddb0c260b
children 27b10024f8d8
line wrap: on
line source

import pytest
from piecrust.data.linker import Linker
from .mockutil import mock_fs, mock_fs_scope


@pytest.mark.parametrize(
    'fs, page_path, expected',
    [
        (mock_fs().withPage('pages/foo'), 'foo.md',
            # is_dir, name, is_self, data
            [(False, 'foo', True, '/foo')]),
        ((mock_fs()
                .withPage('pages/foo')
                .withPage('pages/bar')),
            'foo.md',
            [(False, 'bar', False, '/bar'), (False, 'foo', True, '/foo')]),
        ((mock_fs()
                .withPage('pages/baz')
                .withPage('pages/something')
                .withPage('pages/something/else')
                .withPage('pages/foo')
                .withPage('pages/bar')),
            'foo.md',
            [(False, 'bar', False, '/bar'),
                (False, 'baz', False, '/baz'),
                (False, 'foo', True, '/foo'),
                (True, 'something', False, '/something')]),
        ((mock_fs()
                .withPage('pages/something/else')
                .withPage('pages/foo')
                .withPage('pages/something/good')
                .withPage('pages/bar')),
            'something/else.md',
            [(False, 'else', True, '/something/else'),
                (False, 'good', False, '/something/good')])
    ])
def test_linker_iteration(fs, page_path, expected):
    with mock_fs_scope(fs):
        app = fs.getApp()
        app.config.set('site/pretty_urls', True)
        src = app.getSource('pages')
        linker = Linker(src, page_path)
        actual = list(iter(linker))

        assert len(actual) == len(expected)
        for (a, e) in zip(actual, expected):
            is_dir, name, is_self, url = e
            assert a.is_dir == is_dir
            assert a.name == name
            assert a.is_self == is_self
            assert a.url == url


@pytest.mark.parametrize(
        'fs, page_path, expected',
        [
            (mock_fs().withPage('pages/foo'), 'foo.md',
                [('/foo', True)]),
            ((mock_fs()
                    .withPage('pages/foo')
                    .withPage('pages/bar')),
                'foo.md',
                [('/bar', False), ('/foo', True)]),
            ((mock_fs()
                    .withPage('pages/baz')
                    .withPage('pages/something/else')
                    .withPage('pages/foo')
                    .withPage('pages/bar')),
                'foo.md',
                [('/bar', False), ('/baz', False),
                    ('/foo', True), ('/something/else', False)]),
            ((mock_fs()
                    .withPage('pages/something/else')
                    .withPage('pages/foo')
                    .withPage('pages/something/good')
                    .withPage('pages/bar')),
                'something/else.md',
                [('/something/else', True),
                    ('/something/good', False)])
        ])
def test_recursive_linker_iteration(fs, page_path, expected):
    with mock_fs_scope(fs):
        app = fs.getApp()
        app.config.set('site/pretty_urls', True)
        src = app.getSource('pages')
        linker = Linker(src, page_path)
        actual = list(iter(linker.allpages))

        assert len(actual) == len(expected)
        for i, (a, e) in enumerate(zip(actual, expected)):
            assert a.is_dir is False
            assert a.url == e[0]
            assert a.is_self == e[1]