1
0
Fork 0
forked from github/pelican

Merge pull request #1521 from iKevinY/update-tests

Added tests for generators, settings, and pelican_import
This commit is contained in:
Justin Mayer 2015-02-17 18:59:04 -08:00
commit 053d198f22
6 changed files with 91 additions and 13 deletions

View file

@ -7,7 +7,6 @@ try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
from operator import itemgetter
from shutil import rmtree
from tempfile import mkdtemp
@ -35,12 +34,17 @@ class TestGenerator(unittest.TestCase):
def test_include_path(self):
self.settings['IGNORE_FILES'] = {'ignored1.rst', 'ignored2.rst'}
filename = os.path.join(CUR_DIR, 'content', 'article.rst')
include_path = self.generator._include_path
self.assertTrue(include_path(filename))
self.assertTrue(include_path(filename, extensions=('rst',)))
self.assertFalse(include_path(filename, extensions=('md',)))
ignored_file = os.path.join(CUR_DIR, 'content', 'ignored1.rst')
self.assertFalse(include_path(ignored_file))
def test_get_files_exclude(self):
"""Test that Generator.get_files() properly excludes directories.
"""
@ -56,6 +60,13 @@ class TestGenerator(unittest.TestCase):
self.assertFalse(expected_files - found_files,
"get_files() failed to find one or more files")
# Test string as `paths` argument rather than list
filepaths = generator.get_files(paths='maindir')
found_files = {os.path.basename(f) for f in filepaths}
expected_files = {'maindir.md', 'subdir.md'}
self.assertFalse(expected_files - found_files,
"get_files() failed to find one or more files")
filepaths = generator.get_files(paths=[''], exclude=['maindir'])
found_files = {os.path.basename(f) for f in filepaths}
self.assertNotIn('maindir.md', found_files,
@ -318,6 +329,14 @@ class TestArticlesGenerator(unittest.TestCase):
settings,
blog=True, dates=dates)
def test_nonexistent_template(self):
"""Attempt to load a non-existent template"""
settings = get_settings(filenames={})
generator = ArticlesGenerator(
context=settings, settings=settings,
path=None, theme=settings['THEME'], output_path=None)
self.assertRaises(Exception, generator.get_template, "not_a_template")
def test_generate_authors(self):
"""Check authors generation."""
authors = [author.name for author, _ in self.generator.authors]

View file

@ -245,6 +245,41 @@ class TestBuildHeader(unittest.TestCase):
header = build_header('test', None, None, None, None, None)
self.assertEqual(header, 'test\n####\n\n')
def test_build_header_with_fields(self):
header_data = [
'Test Post',
'2014-11-04',
'Alexis Métaireau',
['Programming'],
['Pelican', 'Python'],
'test-post',
]
expected_docutils = '\n'.join([
'Test Post',
'#########',
':date: 2014-11-04',
':author: Alexis Métaireau',
':category: Programming',
':tags: Pelican, Python',
':slug: test-post',
'\n',
])
expected_md = '\n'.join([
'Title: Test Post',
'Date: 2014-11-04',
'Author: Alexis Métaireau',
'Category: Programming',
'Tags: Pelican, Python',
'Slug: test-post',
'\n',
])
self.assertEqual(build_header(*header_data), expected_docutils)
self.assertEqual(build_markdown_header(*header_data), expected_md)
def test_build_header_with_east_asian_characters(self):
header = build_header('これは広い幅の文字だけで構成されたタイトルです',
None, None, None, None, None)
@ -265,6 +300,7 @@ class TestBuildHeader(unittest.TestCase):
self.assertEqual(header, 'Title: test\n' + 'Attachments: output/test1, '
+ 'output/test2\n\n')
@unittest.skipUnless(BeautifulSoup, 'Needs BeautifulSoup module')
class TestWordpressXMLAttachements(unittest.TestCase):
def setUp(self):

View file

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
import six
import locale
from pelican.tests.support import unittest, get_settings

View file

@ -123,8 +123,6 @@ class TestPelican(LoggedTestCase):
locale_available('French'), 'French locale needed')
def test_custom_locale_generation_works(self):
'''Test that generation with fr_FR.UTF-8 locale works'''
old_locale = locale.setlocale(locale.LC_TIME)
if sys.platform == 'win32':
our_locale = str('French')
else:

View file

@ -41,7 +41,7 @@ class TestSettingsConfiguration(unittest.TestCase):
self.assertNotIn('foobar', self.settings)
def test_read_empty_settings(self):
# Providing no file should return the default values.
# Ensure an empty settings file results in default settings.
settings = read_settings(None)
expected = copy.deepcopy(DEFAULT_CONFIG)
# Added by configure settings
@ -67,8 +67,8 @@ class TestSettingsConfiguration(unittest.TestCase):
settings['SITENAME'] = 'Not a Pelican Blog'
self.assertNotEqual(settings['SITENAME'], DEFAULT_CONFIG['SITENAME'])
def test_path_settings_safety(self):
"""Don't let people setting the static path listings to strs"""
def test_static_path_settings_safety(self):
# Disallow static paths from being strings
settings = {'STATIC_PATHS': 'foo/bar',
'THEME_STATIC_PATHS': 'bar/baz',
# These 4 settings are required to run configure_settings
@ -84,8 +84,7 @@ class TestSettingsConfiguration(unittest.TestCase):
DEFAULT_CONFIG['THEME_STATIC_PATHS'])
def test_configure_settings(self):
#Manipulations to settings should be applied correctly.
# Manipulations to settings should be applied correctly.
settings = {
'SITEURL': 'http://blog.notmyidea.org/',
'LOCALE': '',
@ -93,6 +92,7 @@ class TestSettingsConfiguration(unittest.TestCase):
'THEME': DEFAULT_THEME,
}
configure_settings(settings)
# SITEURL should not have a trailing slash
self.assertEqual(settings['SITEURL'], 'http://blog.notmyidea.org')
@ -103,12 +103,38 @@ class TestSettingsConfiguration(unittest.TestCase):
configure_settings(settings)
self.assertEqual(settings['FEED_DOMAIN'], 'http://feeds.example.com')
def test_theme_settings_exceptions(self):
settings = self.settings
# Check that theme lookup in "pelican/themes" functions as expected
settings['THEME'] = os.path.split(settings['THEME'])[1]
configure_settings(settings)
self.assertEqual(settings['THEME'], DEFAULT_THEME)
# Check that non-existent theme raises exception
settings['THEME'] = 'foo'
self.assertRaises(Exception, configure_settings, settings)
def test_deprecated_dir_setting(self):
settings = self.settings
settings['ARTICLE_DIR'] = 'foo'
settings['PAGE_DIR'] = 'bar'
configure_settings(settings)
self.assertEqual(settings['ARTICLE_PATHS'], ['foo'])
self.assertEqual(settings['PAGE_PATHS'], ['bar'])
with self.assertRaises(KeyError):
settings['ARTICLE_DIR']
settings['PAGE_DIR']
@unittest.skipIf(platform == 'win32', "Doesn't work on Windows")
def test_default_encoding(self):
# test that the default locale is set if
# locale is not specified in the settings
# Test that the default locale is set if not specified in settings
#reset locale to python default
# Reset locale to Python's default locale
locale.setlocale(locale.LC_ALL, str('C'))
self.assertEqual(self.settings['LOCALE'], DEFAULT_CONFIG['LOCALE'])

View file

@ -5,7 +5,7 @@ import shutil
import os
import time
import locale
from sys import platform, version_info
from sys import platform
from tempfile import mkdtemp
import pytz