More refactoring for test_generators

- list comprehensions for distill_articles/pages
- distill articles only once
- pep8
This commit is contained in:
Simon Conseil 2013-06-16 00:14:55 +02:00
commit 4ffa34544e

View file

@ -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__)
@ -49,18 +48,8 @@ class TestArticlesGenerator(unittest.TestCase):
path=CONTENT_DIR, theme=settings['THEME'], path=CONTENT_DIR, theme=settings['THEME'],
output_path=None, markup=settings['MARKUP']) output_path=None, markup=settings['MARKUP'])
cls.generator.generate_context() cls.generator.generate_context()
cls.articles = [[page.title, page.status, page.category.name,
def distill_articles(self, articles): page.template] for page in cls.generator.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()
@ -83,7 +72,6 @@ class TestArticlesGenerator(unittest.TestCase):
def test_generate_context(self): def test_generate_context(self):
articles = self.distill_articles(self.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',
@ -107,7 +95,7 @@ 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):
@ -129,15 +117,15 @@ class TestArticlesGenerator(unittest.TestCase):
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
@ -180,7 +168,8 @@ class TestArticlesGenerator(unittest.TestCase):
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):
@ -199,13 +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
""" """
articles = self.distill_articles(self.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):
@ -215,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={})
@ -290,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))