diff --git a/tests/default_conf.py b/tests/default_conf.py index 09548fdd..bc3a7dff 100644 --- a/tests/default_conf.py +++ b/tests/default_conf.py @@ -30,7 +30,7 @@ SOCIAL = (('twitter', 'http://twitter.com/ametaireau'), DEFAULT_METADATA = (('yeah', 'it is'),) # static paths will be copied under the same name -STATIC_PATHS = ["pictures",] +STATIC_PATHS = ["pictures", ] # A list of files to copy from the source to the destination FILES_TO_COPY = (('extra/robots.txt', 'robots.txt'),) diff --git a/tests/support.py b/tests/support.py index 209cc665..b81e6eff 100644 --- a/tests/support.py +++ b/tests/support.py @@ -1,9 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function -__all__ = [ - 'get_article', - 'unittest', -] +__all__ = ['get_article', 'unittest', ] import os import re @@ -44,19 +41,19 @@ def temporary_folder(): def isplit(s, sep=None): - """ - Behave like str.split but returns a generator instead of a list. + """Behaves like str.split but returns a generator instead of a list. - >>> list(isplit('\tUse the force\n')) == '\tUse the force\n'.split() - True - >>> list(isplit('\tUse the force\n')) == ['Use', 'the', 'force'] - True - >>> list(isplit('\tUse the force\n', "e")) == '\tUse the force\n'.split("e") - True - >>> list(isplit('Use the force', "e")) == 'Use the force'.split("e") - True - >>> list(isplit('Use the force', "e")) == ['Us', ' th', ' forc', ''] - True + >>> list(isplit('\tUse the force\n')) == '\tUse the force\n'.split() + True + >>> list(isplit('\tUse the force\n')) == ['Use', 'the', 'force'] + True + >>> (list(isplit('\tUse the force\n', "e")) + == '\tUse the force\n'.split("e")) + True + >>> list(isplit('Use the force', "e")) == 'Use the force'.split("e") + True + >>> list(isplit('Use the force', "e")) == ['Us', ' th', ' forc', ''] + True """ sep, hardsep = r'\s+' if sep is None else re.escape(sep), sep is not None @@ -76,24 +73,23 @@ def isplit(s, sep=None): def mute(returns_output=False): - """ - Decorate a function that prints to stdout, intercepting the output. - If "returns_output" is True, the function will return a generator - yielding the printed lines instead of the return values. + """Decorate a function that prints to stdout, intercepting the output. + If "returns_output" is True, the function will return a generator + yielding the printed lines instead of the return values. - The decorator litterally hijack sys.stdout during each function - execution, so be careful with what you apply it to. + The decorator litterally hijack sys.stdout during each function + execution, so be careful with what you apply it to. - >>> def numbers(): - print "42" - print "1984" - ... - >>> numbers() - 42 - 1984 - >>> mute()(numbers)() - >>> list(mute(True)(numbers)()) - ['42', '1984'] + >>> def numbers(): + print "42" + print "1984" + ... + >>> numbers() + 42 + 1984 + >>> mute()(numbers)() + >>> list(mute(True)(numbers)()) + ['42', '1984'] """ @@ -164,9 +160,7 @@ def get_settings(): class LogCountHandler(BufferingHandler): - """ - Capturing and counting logged messages. - """ + """Capturing and counting logged messages.""" def __init__(self, capacity=1000): logging.handlers.BufferingHandler.__init__(self, capacity) @@ -179,8 +173,7 @@ class LogCountHandler(BufferingHandler): class LoggedTestCase(unittest.TestCase): - """A test case that captures log messages - """ + """A test case that captures log messages.""" def setUp(self): super(LoggedTestCase, self).setUp() diff --git a/tests/test_contents.py b/tests/test_contents.py index 9bb77ec3..c837ea03 100644 --- a/tests/test_contents.py +++ b/tests/test_contents.py @@ -1,6 +1,9 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +from datetime import datetime +from sys import platform + from .support import unittest from pelican.contents import Page, Article, URLWrapper @@ -31,10 +34,8 @@ class TestPage(unittest.TestCase): } def test_use_args(self): - """Creating a page with arguments passed to the constructor should use - them to initialise object's attributes. - - """ + # Creating a page with arguments passed to the constructor should use + # them to initialise object's attributes. metadata = {'foo': 'bar', 'foobar': 'baz', 'title': 'foobar', } page = Page(TEST_CONTENT, metadata=metadata, context={'localsiteurl': ''}) @@ -44,22 +45,22 @@ class TestPage(unittest.TestCase): self.assertEqual(page.content, TEST_CONTENT) def test_mandatory_properties(self): - """If the title is not set, must throw an exception.""" + # If the title is not set, must throw an exception. page = Page('content') - with self.assertRaises(NameError) as cm: + with self.assertRaises(NameError): page.check_properties() page = Page('content', metadata={'title': 'foobar'}) page.check_properties() def test_summary_from_metadata(self): - """If a :summary: metadata is given, it should be used.""" + # If a :summary: metadata is given, it should be used page = Page(**self.page_kwargs) self.assertEqual(page.summary, TEST_SUMMARY) def test_summary_max_length(self): - """If a :SUMMARY_MAX_LENGTH: is set, and there is no other summary, generated summary - should not exceed the given length.""" + # If a :SUMMARY_MAX_LENGTH: is set, and there is no other summary, + # generated summary should not exceed the given length. page_kwargs = self._copy_page_kwargs() settings = _DEFAULT_CONFIG.copy() page_kwargs['settings'] = settings @@ -72,12 +73,12 @@ class TestPage(unittest.TestCase): self.assertEqual(page.summary, truncate_html_words(TEST_CONTENT, 10)) def test_slug(self): - """If a title is given, it should be used to generate the slug.""" + # If a title is given, it should be used to generate the slug. page = Page(**self.page_kwargs) self.assertEqual(page.slug, 'foo-bar') def test_defaultlang(self): - """If no lang is given, default to the default one.""" + # If no lang is given, default to the default one. page = Page(**self.page_kwargs) self.assertEqual(page.lang, _DEFAULT_CONFIG['DEFAULT_LANG']) @@ -87,10 +88,9 @@ class TestPage(unittest.TestCase): self.assertEqual(page.lang, 'fr') def test_save_as(self): - """If a lang is not the default lang, save_as should be set - accordingly. + # If a lang is not the default lang, save_as should be set + # accordingly. - """ # if a title is defined, save_as should be set page = Page(**self.page_kwargs) self.assertEqual(page.save_as, "pages/foo-bar.html") @@ -101,8 +101,7 @@ class TestPage(unittest.TestCase): self.assertEqual(page.save_as, "pages/foo-bar-fr.html") def test_metadata_url_format(self): - """Arbitrary metadata should be passed through url_format() - """ + # Arbitrary metadata should be passed through url_format() page = Page(**self.page_kwargs) self.assertIn('summary', page.url_format.keys()) page.metadata['directory'] = 'test-dir' @@ -111,10 +110,7 @@ class TestPage(unittest.TestCase): self.assertEqual(page.save_as, 'test-dir/foo-bar') def test_datetime(self): - """If DATETIME is set to a tuple, it should be used to override LOCALE - """ - from datetime import datetime - from sys import platform + # If DATETIME is set to a tuple, it should be used to override LOCALE dt = datetime(2015, 9, 13) page_kwargs = self._copy_page_kwargs() @@ -126,7 +122,6 @@ class TestPage(unittest.TestCase): self.assertEqual(page.locale_date, dt.strftime(_DEFAULT_CONFIG['DEFAULT_DATE_FORMAT'])) - page_kwargs['settings'] = dict([(x, _DEFAULT_CONFIG[x]) for x in _DEFAULT_CONFIG]) @@ -154,9 +149,7 @@ class TestPage(unittest.TestCase): unittest.skip("There is no locale %s in this system." % locale) def test_template(self): - """ - Pages default to page, metadata overwrites - """ + # Pages default to page, metadata overwrites default_page = Page(**self.page_kwargs) self.assertEqual('page', default_page.template) page_kwargs = self._copy_page_kwargs() @@ -177,21 +170,19 @@ class TestPage(unittest.TestCase): return page_kwargs def test_signal(self): - """If a title is given, it should be used to generate the slug.""" + # If a title is given, it should be used to generate the slug. - def receiver_test_function(sender,instance): + def receiver_test_function(sender, instance): pass - content_object_init.connect(receiver_test_function ,sender=Page) - page = Page(**self.page_kwargs) + content_object_init.connect(receiver_test_function, sender=Page) + Page(**self.page_kwargs) self.assertTrue(content_object_init.has_receivers_for(Page)) class TestArticle(TestPage): def test_template(self): - """ - Articles default to article, metadata overwrites - """ + # Articles default to article, metadata overwrites default_article = Article(**self.page_kwargs) self.assertEqual('article', default_article.template) article_kwargs = self._copy_page_kwargs() @@ -202,8 +193,7 @@ class TestArticle(TestPage): class TestURLWrapper(unittest.TestCase): def test_comparisons(self): - """URLWrappers are sorted by name - """ + # URLWrappers are sorted by name wrapper_a = URLWrapper(name='first', settings={}) wrapper_b = URLWrapper(name='last', settings={}) self.assertFalse(wrapper_a > wrapper_b) diff --git a/tests/test_generators.py b/tests/test_generators.py index 54fe7e61..7a6e67fb 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -26,7 +26,7 @@ class TestArticlesGenerator(unittest.TestCase): def get_populated_generator(self): """ We only need to pull all the test articles once, but read from it - for each test. + for each test. """ if self.generator is None: settings = get_settings() @@ -58,7 +58,8 @@ class TestArticlesGenerator(unittest.TestCase): settings['THEME'], None, settings['MARKUP']) writer = MagicMock() generator.generate_feeds(writer) - writer.write_feed.assert_called_with([], settings, 'feeds/all.atom.xml') + writer.write_feed.assert_called_with([], settings, + 'feeds/all.atom.xml') generator = ArticlesGenerator(settings, {'FEED_ALL_ATOM': None}, None, settings['THEME'], None, None) @@ -72,16 +73,21 @@ class TestArticlesGenerator(unittest.TestCase): articles = self.distill_articles(generator.articles) articles_expected = [ ['Article title', 'published', 'Default', 'article'], - ['Article with markdown and summary metadata single', 'published', 'Default', 'article'], - ['Article with markdown and summary metadata multi', 'published', 'Default', 'article'], + ['Article with markdown and summary metadata single', 'published', + 'Default', 'article'], + ['Article with markdown and summary metadata multi', 'published', + 'Default', 'article'], ['Article with template', 'published', 'Default', 'custom'], ['Test md File', 'published', 'test', 'article'], ['Rst with filename metadata', 'published', 'yeah', 'article'], ['Test Markdown extensions', 'published', 'Default', 'article'], ['This is a super article !', 'published', 'Yeah', 'article'], - ['This is an article with category !', 'published', 'yeah', 'article'], - ['This is an article without category !', 'published', 'Default', 'article'], - ['This is an article without category !', 'published', 'TestCategory', 'article'], + ['This is an article with category !', 'published', 'yeah', + 'article'], + ['This is an article without category !', 'published', 'Default', + 'article'], + ['This is an article without category !', 'published', + 'TestCategory', 'article'], ['This is a super article !', 'published', 'yeah', 'article'] ] self.assertItemsEqual(articles_expected, articles) @@ -90,7 +96,8 @@ class TestArticlesGenerator(unittest.TestCase): generator = self.get_populated_generator() categories = [cat.name for cat, _ in generator.categories] - categories_expected = ['Default', 'TestCategory', 'Yeah', 'test', 'yeah'] + categories_expected = ['Default', 'TestCategory', 'Yeah', 'test', + 'yeah'] self.assertEquals(categories, categories_expected) def test_do_not_use_folder_as_category(self): @@ -153,23 +160,24 @@ class TestArticlesGenerator(unittest.TestCase): """ generator = self.get_populated_generator() articles = self.distill_articles(generator.articles) - custom_template = ['Article with template', 'published', 'Default', 'custom'] - standard_template = ['This is a super article !', 'published', 'Yeah', 'article'] + custom_template = ['Article with template', 'published', 'Default', + 'custom'] + standard_template = ['This is a super article !', 'published', 'Yeah', + 'article'] self.assertIn(custom_template, articles) self.assertIn(standard_template, articles) + class TestPageGenerator(unittest.TestCase): - """ - Every time you want to test for a new field; - Make sure the test pages in "TestPages" have all the fields - Add it to distilled in distill_pages - Then update the assertItemsEqual in test_generate_context to match expected - """ + # Note: Every time you want to test for a new field; Make sure the test + # pages in "TestPages" have all the fields Add it to distilled in + # distill_pages Then update the assertItemsEqual in test_generate_context + # to match expected def distill_pages(self, pages): distilled = [] for page in pages: - distilled.append([ + distilled.append([ page.title, page.status, page.template @@ -192,16 +200,18 @@ class TestPageGenerator(unittest.TestCase): pages_expected = [ ['This is a test page', 'published', 'page'], ['This is a markdown test page', 'published', 'page'], - ['This is a test page with a preset template', 'published', 'custom'] + ['This is a test page with a preset template', 'published', + 'custom'] ] hidden_pages_expected = [ ['This is a test hidden page', 'hidden', 'page'], ['This is a markdown test hidden page', 'hidden', 'page'], - ['This is a test hidden page with a custom template', 'hidden', 'custom'] + ['This is a test hidden page with a custom template', 'hidden', + 'custom'] ] - self.assertItemsEqual(pages_expected,pages) - self.assertItemsEqual(hidden_pages_expected,hidden_pages) + self.assertItemsEqual(pages_expected, pages) + self.assertItemsEqual(hidden_pages_expected, hidden_pages) class TestTemplatePagesGenerator(unittest.TestCase): diff --git a/tests/test_importer.py b/tests/test_importer.py index a79cd493..38177918 100644 --- a/tests/test_importer.py +++ b/tests/test_importer.py @@ -53,8 +53,9 @@ class TestWordpressXmlImporter(unittest.TestCase): posts = list(self.posts) test_posts = [post for post in posts if post[2] == 'html-entity-test'] self.assertTrue(len(test_posts) == 1) - + post = test_posts[0] title = post[0] - self.assertTrue(title, "A normal post with some entities in the title. You can't miss them.") + self.assertTrue(title, "A normal post with some entities in the" + " title. You can't miss them.") self.assertTrue('&' not in title) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9d6d43e6..f5da4ab1 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -10,11 +10,11 @@ from pelican.plugins import gzip_cache from .test_contents import TEST_CONTENT, TEST_SUMMARY from .support import unittest, temporary_folder + class TestGzipCache(unittest.TestCase): - '''Unit tests for the gzip cache plugin''' def test_should_compress(self): - '''Test that some filetypes should compress and others shouldn't.''' + # Some filetypes should compress and others shouldn't. self.assertTrue(gzip_cache.should_compress('foo.html')) self.assertTrue(gzip_cache.should_compress('bar.css')) self.assertTrue(gzip_cache.should_compress('baz.js')) @@ -26,16 +26,17 @@ class TestGzipCache(unittest.TestCase): self.assertFalse(gzip_cache.should_compress('foo.mov')) def test_creates_gzip_file(self): - '''Test that a file matching the input filename with a .gz extension is - created.''' + # A file matching the input filename with a .gz extension is created. + # The plugin walks over the output content after the finalized signal # so it is safe to assume that the file exists (otherwise walk would # not report it). Therefore, create a dummy file to use. with temporary_folder() as tempdir: - (_, a_html_filename) = tempfile.mkstemp(suffix='.html', dir=tempdir) + _, a_html_filename = tempfile.mkstemp(suffix='.html', dir=tempdir) gzip_cache.create_gzip_file(a_html_filename) self.assertTrue(os.path.exists(a_html_filename + '.gz')) + class TestSummary(unittest.TestCase): def setUp(self): super(TestSummary, self).setUp() @@ -92,7 +93,7 @@ class TestSummary(unittest.TestCase): page_kwargs = self._copy_page_kwargs() del page_kwargs['metadata']['summary'] page_kwargs['content'] = ( - 'FOOBAR' + TEST_SUMMARY + + 'FOOBAR' + TEST_SUMMARY + '' + TEST_CONTENT) page = Page(**page_kwargs) # test both the summary and the marker removal diff --git a/tests/test_readers.py b/tests/test_readers.py index be544caa..5e9e82b7 100644 --- a/tests/test_readers.py +++ b/tests/test_readers.py @@ -24,8 +24,8 @@ class RstReaderTest(unittest.TestCase): 'category': 'yeah', 'author': 'Alexis Métaireau', 'title': 'This is a super article !', - 'summary': '
Multi-line metadata should be'\
- ' supported\nas well as inline'\
+ 'summary': ' Multi-line metadata should be'
+ ' supported\nas well as inline'
' markup. This is some content. With some stuff to "\
- ""typogrify". Now with added "\
- 'support for '\
- 'TLA. This is some content. With some stuff to '
+ '"typogrify". Now with added '
+ 'support for '
+ 'TLA. This is some content. With some stuff to "\
- "“typogrify”. Now with added "\
- 'support for '\
- 'TLA. This is some content. With some stuff to '
+ '“typogrify”. Now with added '
+ 'support for '
+ 'TLA. The quick brown fox jumped over the lazy dog’s back. The quick brown fox jumped over'
+ ' the lazy dog’s back. version 1.0.42 The quick brown fox jumped over the lazy dog’s back. version 1.0.42 The quick brown fox jumped over the lazy'
+ ' dog’s back.
\nUsed for pelican test
\n'\
- '
\n'
+ 'Used for pelican test
\n'
+ '
\nUsed for pelican test
\n'\
- '
\nUsed for'
+ ' pelican test
\n