view tests/test_data_linker.py @ 365:a9929e0b8f66

tests: Changes to output report and hack for comparing outputs. * Still some problems with newlines and YAML so might as well strip both the actual and expected outputs. * More fancy output for comparing strings.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 03 May 2015 18:40:05 -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]