diff tests/test_data_linker.py @ 974:72f17534d58e

tests: First pass on making unit tests work again. - Fix all imports - Add more helper functions to work with mock file-systems - Simplify some code by running chef directly on the mock FS - Fix a couple tests
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 17 Oct 2017 01:07:30 -0700
parents f987b29d6fab
children 45ad976712ec
line wrap: on
line diff
--- a/tests/test_data_linker.py	Tue Oct 17 01:04:10 2017 -0700
+++ b/tests/test_data_linker.py	Tue Oct 17 01:07:30 2017 -0700
@@ -1,101 +1,73 @@
-import os.path
 import pytest
 from piecrust.data.linker import Linker
-from .mockutil import mock_fs, mock_fs_scope
+from .mockutil import mock_fs, mock_fs_scope, get_simple_content_item
 
 
 @pytest.mark.parametrize(
     'fs_fac, page_path, expected',
     [
-        (lambda: mock_fs().withPage('pages/foo'), 'foo.md',
-            # is_dir, name, is_self, data
-            [(False, 'foo', True, '/foo')]),
+        (lambda: mock_fs().withPage('pages/foo'), 'foo',
+         []),
         ((lambda: mock_fs()
-                .withPage('pages/foo')
-                .withPage('pages/bar')),
-            'foo.md',
-            [(False, 'bar', False, '/bar'), (False, 'foo', True, '/foo')]),
+          .withPage('pages/foo')
+          .withPage('pages/bar')),
+         'foo',
+         ['/bar']),
         ((lambda: 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')]),
+          .withPage('pages/baz')
+          .withPage('pages/something')
+          .withPage('pages/something/else')
+          .withPage('pages/foo')
+          .withPage('pages/bar')),
+         'foo',
+         ['/bar', '/baz', '/something']),
         ((lambda: 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')])
+          .withPage('pages/something/else')
+          .withPage('pages/foo')
+          .withPage('pages/something/good')
+          .withPage('pages/bar')),
+         'something/else',
+         ['/something/good'])
     ])
-def test_linker_iteration(fs_fac, page_path, expected):
+def test_linker_siblings(fs_fac, page_path, expected):
     fs = fs_fac()
     fs.withConfig()
     with mock_fs_scope(fs):
         app = fs.getApp()
         app.config.set('site/pretty_urls', True)
         src = app.getSource('pages')
-        linker = Linker(src, os.path.dirname(page_path),
-                        root_page_path=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
+        item = get_simple_content_item(app, page_path)
+        linker = Linker(src, item)
+        actual = list(linker.siblings)
+        assert sorted(map(lambda i: i.url, actual)) == sorted(expected)
 
 
 @pytest.mark.parametrize(
-        'fs_fac, page_path, expected',
-        [
-            (lambda: mock_fs().withPage('pages/foo'), 'foo.md',
-                [('/foo', True)]),
-            ((lambda: mock_fs()
-                    .withPage('pages/foo')
-                    .withPage('pages/bar')),
-                'foo.md',
-                [('/bar', False), ('/foo', True)]),
-            ((lambda: 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)]),
-            ((lambda: 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_fac, page_path, expected):
+    'fs_fac, page_path, expected',
+    [
+        (lambda: mock_fs().withPage('pages/foo'), 'foo.md',
+         []),
+        ((lambda: mock_fs()
+          .withPage('pages/foo')
+          .withPage('pages/bar')),
+         'foo',
+         []),
+        ((lambda: mock_fs()
+          .withPage('pages/baz')
+          .withPage('pages/foo')
+          .withPage('pages/foo/more')
+          .withPage('pages/foo/even_more')),
+         'foo',
+         ['/foo/more', '/foo/even_more'])
+    ])
+def test_linker_children(fs_fac, page_path, expected):
     fs = fs_fac()
     fs.withConfig()
     with mock_fs_scope(fs):
         app = fs.getApp()
         app.config.set('site/pretty_urls', True)
         src = app.getSource('pages')
-        linker = Linker(src, os.path.dirname(page_path),
-                        root_page_path=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]
-
+        item = get_simple_content_item(app, page_path)
+        linker = Linker(src, item)
+        actual = list(linker.children)
+        assert sorted(map(lambda i: i.url, actual)) == sorted(expected)