def test_search_for(): with app.app_context(): assert storage.search_for('') == [] assert isinstance(storage.search_for('Hello'), Iterable) assert len(list(storage.search_for('Hello'))) == 1 assert len(list(storage.search_for('Hello', include_draft=True))) == 2 app.config['ALLOW_SEARCH_PAGES'] = False with app.app_context(): assert len(list(storage.search_for('Hello'))) == 0 app.config['ALLOW_SEARCH_PAGES'] = True
def test_read_file(): with app.app_context(): file_path = os.path.join(current_app.instance_path, 'posts', '2017-03-09-my-post.md') meta = { 'created': date(year=2017, month=3, day=9), 'updated': datetime.strptime('2017-03-10 10:24:00', '%Y-%m-%d %H:%M:%S'), 'categories': ['Default'], 'tags': 'Hello World' } raw_content = '## Title\n\nLorem ipsum dolor sit amet.' assert (meta, raw_content) == FileStorage.read_file(file_path) file_path = os.path.join(current_app.instance_path, 'posts', '2017-03-09-my-post-no-yaml.txt') meta = {} raw_content = 'Lorem ipsum dolor sit amet.' assert (meta, raw_content) == FileStorage.read_file(file_path) file_path = os.path.join(current_app.instance_path, 'posts', '2017-03-09-my-post-no-yaml2.txt') raw_content = '---\n\nLorem ipsum dolor sit amet.' assert (meta, raw_content) == FileStorage.read_file(file_path)
def test_fix_page_relative_url(): with app.app_context(): assert FileStorage.fix_page_relative_url('my-page') == ('my-page/', False) assert FileStorage.fix_page_relative_url('my-page/') == ('my-page/', False) assert FileStorage.fix_page_relative_url('style.css') \ == (os.path.join(current_app.instance_path, 'pages', 'style.css'), True) print(FileStorage.fix_page_relative_url('my-page/index.md')) assert FileStorage.fix_page_relative_url('my-page/index.md') \ == (os.path.join(current_app.instance_path, 'pages', 'my-page', 'index.md'), True) app.config['PAGE_SOURCE_ACCESSIBLE'] = False assert FileStorage.fix_page_relative_url('my-page/index.md') == ( 'my-page/index.md.html', False) app.config['PAGE_SOURCE_ACCESSIBLE'] = True assert FileStorage.fix_page_relative_url('my-page/index') == ( 'my-page/index.html', False) assert FileStorage.fix_page_relative_url('my-page/index.htm') == ( 'my-page/index.html', False) assert FileStorage.fix_page_relative_url('my-page/index.html') == ( 'my-page/index.html', False) assert FileStorage.fix_page_relative_url('//') == (None, False) assert storage.fix_relative_url( 'post', '2017/1/1/my-post/index') == ('2017/01/01/my-post/index.html', False) assert storage.fix_relative_url( 'page', '/my-page/index.htm') == ('my-page/index.html', False)
def test_get_page(): with app.app_context(): page = storage.get_page('my-page/') assert isinstance(page, Page) assert page.rel_url == 'my-page/' assert page.unique_key == '/my-page/' assert page.format == 'markdown' assert (page.meta, page.raw_content) == FileStorage.read_file( os.path.join(current_app.instance_path, 'pages', 'my-page', 'index.md')) page = storage.get_page('my-page/index.html') assert page.rel_url == 'my-page/index.html' assert page.unique_key == '/my-page/' assert page.format == 'markdown' page = storage.get_page('index.html') assert page.rel_url == 'index.html' assert page.unique_key == '/index.html' assert page.format == 'markdown' page = storage.get_page('test-page.html') assert page.rel_url == 'test-page.html' assert page.unique_key == '/test-page.html' assert page.format == 'txt' page = storage.get_page('test-page-draft.html') assert page is None page = storage.get_page('test-page-draft.html', include_draft=True) assert page is not None page = storage.get_page('test-non-exists.html') assert page is None page = storage.get_page('non-exists/non-exists.html') assert page is None
def generate_pages_by_file(): """Generates custom pages of 'file' storage type.""" from veripress import app from veripress.model import storage from veripress.model.parsers import get_standard_format_name from veripress.helpers import traverse_directory deploy_dir = get_deploy_dir() def copy_file(src, dst): makedirs(os.path.dirname(dst), mode=0o755, exist_ok=True) shutil.copyfile(src, dst) with app.app_context(), app.test_client() as client: root_path = os.path.join(app.instance_path, 'pages') for path in traverse_directory(root_path): rel_path = os.path.relpath(path, root_path) # e.g. 'a/b/c/index.md' filename, ext = os.path.splitext(rel_path) # e.g. ('a/b/c/index', '.md') if get_standard_format_name(ext[1:]) is not None: # is source of custom page rel_url = filename.replace(os.path.sep, '/') + '.html' # e.g. 'a/b/c/index.html' page = storage.get_page(rel_url, include_draft=False) if page is not None: # it's not a draft, so generate the html page makedirs(os.path.join(deploy_dir, os.path.dirname(rel_path)), mode=0o755, exist_ok=True) with open(os.path.join(deploy_dir, filename + '.html'), 'wb') as f: f.write(client.get('/' + rel_url).data) if app.config['PAGE_SOURCE_ACCESSIBLE']: copy_file(path, os.path.join(deploy_dir, rel_path)) else: # is other direct files copy_file(path, os.path.join(deploy_dir, rel_path))
def test_fix_rel_url(): with app.app_context(): correct = '2017/01/01/my-post/' assert Storage.fix_post_relative_url('2017/01/01/my-post/') == correct assert Storage.fix_post_relative_url('2017/1/1/my-post') == correct assert Storage.fix_post_relative_url('2017/1/1/my-post.html') == correct assert Storage.fix_post_relative_url('2017/1/1/my-post/index') == correct + 'index.html' assert Storage.fix_post_relative_url('2017/1/1/my-post/index.html') == correct + 'index.html' assert Storage.fix_post_relative_url('2017/1/1/my-post/test') is None assert Storage.fix_post_relative_url('2017/13/32/my-post/') is None # assert Storage.fix_page_relative_url('my-page') == ('my-page/', False) # assert Storage.fix_page_relative_url('my-page/') == ('my-page/', False) # assert Storage.fix_page_relative_url('test-page.txt') == ('test-page.txt', True) # assert Storage.fix_page_relative_url('my-page/index.md') == ('my-page/index.md', True) # assert Storage.fix_page_relative_url('my-page/index') == ('my-page/index.html', False) # assert Storage.fix_page_relative_url('my-page/index.htm') == ('my-page/index.html', False) # assert Storage.fix_page_relative_url('my-page/index.html') == ('my-page/index.html', False) # assert Storage.fix_page_relative_url('//') == (None, False) storage_ = Storage() assert storage_.fix_relative_url('post', '2017/1/1/my-post/index') == ('2017/01/01/my-post/index.html', False) # assert Storage.fix_relative_url('page', '/my-page/index.htm') == ('my-page/index.html', False) with raises(ValueError, message='Publish type "wrong" is not supported'): storage_.fix_relative_url('wrong', 'wrong-publish-type/')
def test_get_tags_categories(): with app.app_context(): tag_items = storage.get_tags() assert len(tag_items) == 2 assert ('Hello World', Pair(2, 1)) in tag_items category_items = storage.get_categories() assert len(category_items) == 1 assert ('Default', Pair(2, 1)) in category_items
def do_generate(): from veripress import app from veripress.model import storage deploy_dir = get_deploy_dir() # copy global static folder dst_static_folder = os.path.join(deploy_dir, 'static') if os.path.isdir(app.static_folder): shutil.copytree(app.static_folder, dst_static_folder) # copy theme static files makedirs(dst_static_folder, mode=0o755, exist_ok=True) copy_folder_content(app.theme_static_folder, dst_static_folder) # collect all possible urls (except custom pages) all_urls = {'/', '/feed.xml', '/atom.xml', '/archive/'} with app.app_context(): posts = list(storage.get_posts(include_draft=False)) index_page_count = int(math.ceil(len(posts) / app.config['ENTRIES_PER_PAGE'])) for i in range(2, index_page_count + 1): # ignore '/page/1/', this will be generated separately later all_urls.add('/page/{}/'.format(i)) for post in posts: all_urls.add(post.unique_key) all_urls.add('/archive/{}/'.format(post.created.strftime('%Y'))) all_urls.add('/archive/{}/{}/'.format(post.created.strftime('%Y'), post.created.strftime('%m'))) tags = storage.get_tags() for tag_item in tags: all_urls.add('/tag/{}/'.format(tag_item[0])) categories = storage.get_categories() for category_item in categories: all_urls.add('/category/{}/'.format(category_item[0])) with app.test_client() as client: # generate all possible urls for url in all_urls: resp = client.get(url) file_path = os.path.join(get_deploy_dir(), url.lstrip('/').replace('/', os.path.sep)) if url.endswith('/'): file_path += 'index.html' makedirs(os.path.dirname(file_path), mode=0o755, exist_ok=True) with open(file_path, 'wb') as f: f.write(resp.data) # generate 404 page resp = client.get('/post/this-is-a-page-that-never-gonna-exist' '-because-it-is-a-post-with-wrong-url-format/') with open(os.path.join(deploy_dir, '404.html'), 'wb') as f: f.write(resp.data) if app.config['STORAGE_TYPE'] == 'file': generate_pages_by_file()
def test_get_posts(): with app.app_context(): posts = storage.get_posts() assert isinstance(posts, Iterable) posts = list(posts) assert len(posts) == 3 posts = list(storage.get_posts(include_draft=True)) assert len(posts) == 4 assert posts[-1].title == 'Hello, world!' assert posts[-1].is_draft == True
def test_search_file(): with app.app_context(): search_root = os.path.join(current_app.instance_path, 'posts') path, ext = FileStorage.search_file(search_root, '2017-03-09-my-post') assert path == os.path.join(search_root, '2017-03-09-my-post.md') assert ext == 'md' search_root = 'posts' path, ext = FileStorage.search_file(search_root, '2017-03-09-my-post', instance_relative_root=True) assert (path, ext) == FileStorage.search_instance_file( search_root, '2017-03-09-my-post') assert path == os.path.join(current_app.instance_path, search_root, '2017-03-09-my-post.md') assert ext == 'md'
def test_get_pages(): with app.app_context(): pages = storage.get_pages() assert isinstance(pages, Iterable) pages = list(pages) assert len(pages) == 4 fail = False for p in pages: if p.rel_url == 'my-page/': break else: fail = True assert not fail assert len(list(storage.get_pages(include_draft=True))) == 5
def test_get_widgets(): with app.app_context(): widgets = storage.get_widgets() assert isinstance(widgets, Iterable) widgets = list(widgets) assert len(widgets) == 2 assert isinstance(widgets[0], Widget) assert widgets[0].position == widgets[1].position assert widgets[0].order < widgets[1].order widgets = list( storage.get_widgets(position='header', include_draft=True)) assert len(list(widgets)) == 1 widgets = list( storage.get_widgets(position='non-exists', include_draft=True)) assert len(list(widgets)) == 0
def test_get_posts_with_limits(): with app.app_context(): posts = storage.get_posts_with_limits(include_draft=True) assert posts == storage.get_posts(include_draft=True) posts = storage.get_posts_with_limits(include_draft=True, tags='Hello World', categories=['Default']) assert len(posts) == 2 posts = storage.get_posts_with_limits(include_draft=True, created=(datetime.strptime('2016-02-02', '%Y-%m-%d'), date(year=2016, month=3, day=3))) assert len(posts) == 1 posts = storage.get_posts_with_limits(include_draft=True, created=(date(year=2011, month=2, day=2), date(year=2014, month=2, day=2))) assert len(posts) == 0
def test_get_storage(): wrong_app = create_app('config2.py') assert wrong_app.config['STORAGE_TYPE'] == 'fake_type' with raises(ConfigurationError, message='Storage type "fake_type" is not supported.'): with wrong_app.app_context(): get_storage() # this will raise a ConfigurationError, because the storage type is not supported with app.app_context(): s = get_storage() assert storage == s assert id(storage) != id(s) # noinspection PyProtectedMember assert storage._get_current_object() == s assert isinstance(s, Storage) assert isinstance(s, FileStorage) assert s.closed # the storage object should be marked as 'closed' after the app context being torn down with raises(AttributeError): setattr(s, 'closed', True)
def test_get_post(): with app.app_context(): post = storage.get_post('2017/03/09/my-post/index.html') assert isinstance(post, Post) assert post.rel_url == '2017/03/09/my-post/index.html' assert post.unique_key == '/post/2017/03/09/my-post/' # unique_key has no trailing 'index.html' assert post.format == 'markdown' assert (post.meta, post.raw_content) == FileStorage.read_file( os.path.join(current_app.instance_path, 'posts', '2017-03-09-my-post.md')) post = storage.get_post('2017/03/09/my-post-no-yaml/') assert post.rel_url == '2017/03/09/my-post-no-yaml/' assert post.unique_key == '/post/2017/03/09/my-post-no-yaml/' assert post.format == 'txt' post = storage.get_post('2016/03/03/hello-world/') assert post is None post = storage.get_post('2016/03/03/hello-world/', include_draft=True) assert post is not None post = storage.get_post('2016/03/03/non-exists/') assert post is None