mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #1007 from saimn/refactor-tests
Refactor test_generators and pelican_open
This commit is contained in:
commit
ea6d0cf5b5
2 changed files with 45 additions and 81 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals, print_function
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from codecs import open
|
from codecs import open
|
||||||
|
|
@ -10,7 +10,6 @@ from tempfile import mkdtemp
|
||||||
from pelican.generators import (Generator, ArticlesGenerator, PagesGenerator,
|
from pelican.generators import (Generator, ArticlesGenerator, PagesGenerator,
|
||||||
TemplatePagesGenerator)
|
TemplatePagesGenerator)
|
||||||
from pelican.writers import Writer
|
from pelican.writers import Writer
|
||||||
from pelican.settings import DEFAULT_CONFIG
|
|
||||||
from pelican.tests.support import unittest, get_settings
|
from pelican.tests.support import unittest, get_settings
|
||||||
|
|
||||||
CUR_DIR = os.path.dirname(__file__)
|
CUR_DIR = os.path.dirname(__file__)
|
||||||
|
|
@ -38,37 +37,19 @@ class TestGenerator(unittest.TestCase):
|
||||||
|
|
||||||
class TestArticlesGenerator(unittest.TestCase):
|
class TestArticlesGenerator(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
@classmethod
|
||||||
super(TestArticlesGenerator, self).setUp()
|
def setUpClass(cls):
|
||||||
self.generator = None
|
settings = get_settings(filenames={})
|
||||||
|
settings['DEFAULT_CATEGORY'] = 'Default'
|
||||||
|
settings['DEFAULT_DATE'] = (1970, 1, 1)
|
||||||
|
|
||||||
def get_populated_generator(self):
|
cls.generator = ArticlesGenerator(
|
||||||
"""
|
context=settings.copy(), settings=settings,
|
||||||
We only need to pull all the test articles once, but read from it
|
path=CONTENT_DIR, theme=settings['THEME'],
|
||||||
for each test.
|
output_path=None, markup=settings['MARKUP'])
|
||||||
"""
|
cls.generator.generate_context()
|
||||||
if self.generator is None:
|
cls.articles = [[page.title, page.status, page.category.name,
|
||||||
settings = get_settings(filenames={})
|
page.template] for page in cls.generator.articles]
|
||||||
settings['DEFAULT_CATEGORY'] = 'Default'
|
|
||||||
settings['DEFAULT_DATE'] = (1970, 1, 1)
|
|
||||||
self.generator = ArticlesGenerator(
|
|
||||||
context=settings.copy(), settings=settings,
|
|
||||||
path=CONTENT_DIR, theme=settings['THEME'],
|
|
||||||
output_path=None, markup=settings['MARKUP'])
|
|
||||||
self.generator.generate_context()
|
|
||||||
return self.generator
|
|
||||||
|
|
||||||
def distill_articles(self, articles):
|
|
||||||
distilled = []
|
|
||||||
for page in articles:
|
|
||||||
distilled.append([
|
|
||||||
page.title,
|
|
||||||
page.status,
|
|
||||||
page.category.name,
|
|
||||||
page.template
|
|
||||||
]
|
|
||||||
)
|
|
||||||
return distilled
|
|
||||||
|
|
||||||
def test_generate_feeds(self):
|
def test_generate_feeds(self):
|
||||||
settings = get_settings()
|
settings = get_settings()
|
||||||
|
|
@ -91,8 +72,6 @@ class TestArticlesGenerator(unittest.TestCase):
|
||||||
|
|
||||||
def test_generate_context(self):
|
def test_generate_context(self):
|
||||||
|
|
||||||
generator = self.get_populated_generator()
|
|
||||||
articles = self.distill_articles(generator.articles)
|
|
||||||
articles_expected = [
|
articles_expected = [
|
||||||
['Article title', 'published', 'Default', 'article'],
|
['Article title', 'published', 'Default', 'article'],
|
||||||
['Article with markdown and summary metadata single', 'published',
|
['Article with markdown and summary metadata single', 'published',
|
||||||
|
|
@ -116,38 +95,37 @@ class TestArticlesGenerator(unittest.TestCase):
|
||||||
['Article with markdown containing footnotes', 'published',
|
['Article with markdown containing footnotes', 'published',
|
||||||
'Default', 'article']
|
'Default', 'article']
|
||||||
]
|
]
|
||||||
self.assertEqual(sorted(articles_expected), sorted(articles))
|
self.assertEqual(sorted(articles_expected), sorted(self.articles))
|
||||||
|
|
||||||
def test_generate_categories(self):
|
def test_generate_categories(self):
|
||||||
|
|
||||||
generator = self.get_populated_generator()
|
|
||||||
# test for name
|
# test for name
|
||||||
# categories are grouped by slug; if two categories have the same slug
|
# categories are grouped by slug; if two categories have the same slug
|
||||||
# but different names they will be grouped together, the first one in
|
# but different names they will be grouped together, the first one in
|
||||||
# terms of process order will define the name for that category
|
# terms of process order will define the name for that category
|
||||||
categories = [cat.name for cat, _ in generator.categories]
|
categories = [cat.name for cat, _ in self.generator.categories]
|
||||||
categories_alternatives = (
|
categories_alternatives = (
|
||||||
sorted(['Default', 'TestCategory', 'Yeah', 'test', '指導書']),
|
sorted(['Default', 'TestCategory', 'Yeah', 'test', '指導書']),
|
||||||
sorted(['Default', 'TestCategory', 'yeah', 'test', '指導書']),
|
sorted(['Default', 'TestCategory', 'yeah', 'test', '指導書']),
|
||||||
)
|
)
|
||||||
self.assertIn(sorted(categories), categories_alternatives)
|
self.assertIn(sorted(categories), categories_alternatives)
|
||||||
# test for slug
|
# test for slug
|
||||||
categories = [cat.slug for cat, _ in generator.categories]
|
categories = [cat.slug for cat, _ in self.generator.categories]
|
||||||
categories_expected = ['default', 'testcategory', 'yeah', 'test',
|
categories_expected = ['default', 'testcategory', 'yeah', 'test',
|
||||||
'zhi-dao-shu']
|
'zhi-dao-shu']
|
||||||
self.assertEqual(sorted(categories), sorted(categories_expected))
|
self.assertEqual(sorted(categories), sorted(categories_expected))
|
||||||
|
|
||||||
def test_do_not_use_folder_as_category(self):
|
def test_do_not_use_folder_as_category(self):
|
||||||
|
|
||||||
settings = DEFAULT_CONFIG.copy()
|
settings = get_settings(filenames={})
|
||||||
settings['DEFAULT_CATEGORY'] = 'Default'
|
settings['DEFAULT_CATEGORY'] = 'Default'
|
||||||
settings['DEFAULT_DATE'] = (1970, 1, 1)
|
settings['DEFAULT_DATE'] = (1970, 1, 1)
|
||||||
settings['USE_FOLDER_AS_CATEGORY'] = False
|
settings['USE_FOLDER_AS_CATEGORY'] = False
|
||||||
settings['filenames'] = {}
|
settings['filenames'] = {}
|
||||||
generator = ArticlesGenerator(
|
generator = ArticlesGenerator(
|
||||||
context=settings.copy(), settings=settings,
|
context=settings.copy(), settings=settings,
|
||||||
path=CONTENT_DIR, theme=DEFAULT_CONFIG['THEME'],
|
path=CONTENT_DIR, theme=settings['THEME'],
|
||||||
output_path=None, markup=DEFAULT_CONFIG['MARKUP'])
|
output_path=None, markup=settings['MARKUP'])
|
||||||
generator.generate_context()
|
generator.generate_context()
|
||||||
# test for name
|
# test for name
|
||||||
# categories are grouped by slug; if two categories have the same slug
|
# categories are grouped by slug; if two categories have the same slug
|
||||||
|
|
@ -174,8 +152,8 @@ class TestArticlesGenerator(unittest.TestCase):
|
||||||
write = MagicMock()
|
write = MagicMock()
|
||||||
generator.generate_direct_templates(write)
|
generator.generate_direct_templates(write)
|
||||||
write.assert_called_with("archives.html",
|
write.assert_called_with("archives.html",
|
||||||
generator.get_template("archives"), settings,
|
generator.get_template("archives"), settings,
|
||||||
blog=True, paginated={}, page_name='archives')
|
blog=True, paginated={}, page_name='archives')
|
||||||
|
|
||||||
def test_direct_templates_save_as_modified(self):
|
def test_direct_templates_save_as_modified(self):
|
||||||
|
|
||||||
|
|
@ -189,8 +167,9 @@ class TestArticlesGenerator(unittest.TestCase):
|
||||||
write = MagicMock()
|
write = MagicMock()
|
||||||
generator.generate_direct_templates(write)
|
generator.generate_direct_templates(write)
|
||||||
write.assert_called_with("archives/index.html",
|
write.assert_called_with("archives/index.html",
|
||||||
generator.get_template("archives"), settings,
|
generator.get_template("archives"), settings,
|
||||||
blog=True, paginated={}, page_name='archives/index')
|
blog=True, paginated={},
|
||||||
|
page_name='archives/index')
|
||||||
|
|
||||||
def test_direct_templates_save_as_false(self):
|
def test_direct_templates_save_as_false(self):
|
||||||
|
|
||||||
|
|
@ -209,14 +188,12 @@ class TestArticlesGenerator(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
Custom template articles get the field but standard/unset are None
|
Custom template articles get the field but standard/unset are None
|
||||||
"""
|
"""
|
||||||
generator = self.get_populated_generator()
|
|
||||||
articles = self.distill_articles(generator.articles)
|
|
||||||
custom_template = ['Article with template', 'published', 'Default',
|
custom_template = ['Article with template', 'published', 'Default',
|
||||||
'custom']
|
'custom']
|
||||||
standard_template = ['This is a super article !', 'published', 'Yeah',
|
standard_template = ['This is a super article !', 'published', 'Yeah',
|
||||||
'article']
|
'article']
|
||||||
self.assertIn(custom_template, articles)
|
self.assertIn(custom_template, self.articles)
|
||||||
self.assertIn(standard_template, articles)
|
self.assertIn(standard_template, self.articles)
|
||||||
|
|
||||||
|
|
||||||
class TestPageGenerator(unittest.TestCase):
|
class TestPageGenerator(unittest.TestCase):
|
||||||
|
|
@ -226,15 +203,7 @@ class TestPageGenerator(unittest.TestCase):
|
||||||
# to match expected
|
# to match expected
|
||||||
|
|
||||||
def distill_pages(self, pages):
|
def distill_pages(self, pages):
|
||||||
distilled = []
|
return [[page.title, page.status, page.template] for page in pages]
|
||||||
for page in pages:
|
|
||||||
distilled.append([
|
|
||||||
page.title,
|
|
||||||
page.status,
|
|
||||||
page.template
|
|
||||||
]
|
|
||||||
)
|
|
||||||
return distilled
|
|
||||||
|
|
||||||
def test_generate_context(self):
|
def test_generate_context(self):
|
||||||
settings = get_settings(filenames={})
|
settings = get_settings(filenames={})
|
||||||
|
|
@ -301,8 +270,7 @@ class TestTemplatePagesGenerator(unittest.TestCase):
|
||||||
writer = Writer(self.temp_output, settings=settings)
|
writer = Writer(self.temp_output, settings=settings)
|
||||||
generator.generate_output(writer)
|
generator.generate_output(writer)
|
||||||
|
|
||||||
output_path = os.path.join(
|
output_path = os.path.join(self.temp_output, 'generated', 'file.html')
|
||||||
self.temp_output, 'generated', 'file.html')
|
|
||||||
|
|
||||||
# output file has been generated
|
# output file has been generated
|
||||||
self.assertTrue(os.path.exists(output_path))
|
self.assertTrue(os.path.exists(output_path))
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,21 @@
|
||||||
from __future__ import unicode_literals, print_function
|
from __future__ import unicode_literals, print_function
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
import codecs
|
||||||
|
import errno
|
||||||
|
import fnmatch
|
||||||
|
import locale
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import pytz
|
import pytz
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import traceback
|
import traceback
|
||||||
import logging
|
|
||||||
import errno
|
|
||||||
import locale
|
|
||||||
import fnmatch
|
|
||||||
from collections import Hashable
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
from codecs import open, BOM_UTF8
|
from collections import Hashable
|
||||||
|
from contextlib import contextmanager
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import partial
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
from jinja2 import Markup
|
from jinja2 import Markup
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
|
|
@ -215,20 +216,15 @@ def get_date(string):
|
||||||
raise ValueError('{0!r} is not a valid date'.format(string))
|
raise ValueError('{0!r} is not a valid date'.format(string))
|
||||||
|
|
||||||
|
|
||||||
class pelican_open(object):
|
@contextmanager
|
||||||
|
def pelican_open(filename):
|
||||||
"""Open a file and return its content"""
|
"""Open a file and return its content"""
|
||||||
def __init__(self, filename):
|
|
||||||
self.filename = filename
|
|
||||||
|
|
||||||
def __enter__(self):
|
with codecs.open(filename, encoding='utf-8') as infile:
|
||||||
with open(self.filename, encoding='utf-8') as infile:
|
content = infile.read()
|
||||||
content = infile.read()
|
if content[0] == codecs.BOM_UTF8.decode('utf8'):
|
||||||
if content[0] == BOM_UTF8.decode('utf8'):
|
content = content[1:]
|
||||||
content = content[1:]
|
yield content
|
||||||
return content
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, traceback):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def slugify(value, substitutions=()):
|
def slugify(value, substitutions=()):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue