From bb96e253c97ad540d99456a7353d5a542bda2fe7 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Wed, 5 Jan 2011 16:24:44 +0100 Subject: [PATCH 1/3] Start working on the testsuite. --- pelican/tests/test_contents.py | 54 ++++++++++++++++++++++++++++++++++ pelican/tests/test_settings.py | 16 ++++++++++ 2 files changed, 70 insertions(+) create mode 100644 pelican/tests/test_contents.py create mode 100644 pelican/tests/test_settings.py diff --git a/pelican/tests/test_contents.py b/pelican/tests/test_contents.py new file mode 100644 index 00000000..10d3741a --- /dev/null +++ b/pelican/tests/test_contents.py @@ -0,0 +1,54 @@ +from unittest2 import TestCase + +from pelican.contents import Page +from pelican.settings import DEFAULT_CONFIG + +class TestPage(TestCase): + + def test_use_args(self): + # creating a page with arguments passed to the connstructor should use + # them to initialise object's attributes + metadata = {'foo': 'bar', 'foobar': 'baz'} + page = Page('content', metadatas=metadata) + for key, value in metadata.items(): + self.assertTrue(hasattr(page, key)) + self.assertEqual(value, getattr(page, key)) + self.assertEqual(page.content, "content") + + def test_mandatory_properties(self): + # if the title is not set, must throw an exception + page = Page('content') + with self.assertRaises(NameError) as cm: + page.check_properties() + + page = Page('content', metadatas={'title': 'foobar'}) + page.check_properties() + + + def test_slug(self): + # if a title is given, it should be used to generate the slug + page = Page('content', {'title': 'foobar is foo'}) + self.assertEqual(page.slug, 'foobar-is-foo') + + + def test_defaultlang(self): + # if no lang is given, default to the default one + page = Page('content') + self.assertEqual(page.lang, DEFAULT_CONFIG['DEFAULT_LANG']) + + # it is possible to specify the lang in the metadata infos + page = Page('content', {'lang': 'fr'}) + self.assertEqual(page.lang, 'fr') + + def test_save_as(self): + # if a lang is not the default lang, save_as should be set accordingly + page = Page('content', {'title': 'foobar', 'lang': 'fr'}) #default lang is en + self.assertEqual(page.save_as, "foobar-fr.html") + + # otherwise, if a title is defined, save_as should be set + page = Page('content', {'title': 'foobar'}) + page.save_as = 'foobar.html' + + # if no title is given, there is no save_as + page = Page('content') + self.assertFalse(hasattr(page, 'save_as')) diff --git a/pelican/tests/test_settings.py b/pelican/tests/test_settings.py new file mode 100644 index 00000000..58e38378 --- /dev/null +++ b/pelican/tests/test_settings.py @@ -0,0 +1,16 @@ +from unittest2 import TestCase + +from pelican.settings import read_settings, DEFAULT_CONFIG + +class SettingsTest(TestCase): + + def test_read_settings(self): + # providing no file should return the default values + settings = read_settings(None) + self.assertDictEqual(settings, DEFAULT_CONFIG) + + # providing a file should read it, replace the default values and append + # new values to the settings, if any + settings = read_settings(mock) + self.assertIn('key', settings) + self.assertEqual(settings['KEY' From c13c707a625b11e1c5b0e0a180ce5b6bec177985 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Thu, 13 Jan 2011 00:32:37 +0100 Subject: [PATCH 2/3] Tests for contents.py --- pelican/contents.py | 10 ++++++---- {pelican/tests => tests}/test_contents.py | 2 -- 2 files changed, 6 insertions(+), 6 deletions(-) rename {pelican/tests => tests}/test_contents.py (99%) diff --git a/pelican/contents.py b/pelican/contents.py index b6e71b75..9a70da2c 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -1,4 +1,5 @@ from pelican.utils import slugify, truncate_html_words +from pelican.settings import DEFAULT_CONFIG class Page(object): @@ -22,16 +23,17 @@ class Page(object): if 'AUTHOR' in settings: self.author = settings['AUTHOR'] - default_lang = settings.get('DEFAULT_LANG').lower() + default_lang = settings.get('DEFAULT_LANG', + DEFAULT_CONFIG['DEFAULT_LANG']).lower() if not hasattr(self, 'lang'): self.lang = default_lang self.in_default_lang = (self.lang == default_lang) - if not hasattr(self, 'slug'): + if not hasattr(self, 'slug') and hasattr(self, 'title'): self.slug = slugify(self.title) - if not hasattr(self, 'save_as'): + if not hasattr(self, 'save_as') and hasattr(self, 'slug'): if self.in_default_lang: self.save_as = '%s.html' % self.slug clean_url = '%s/' % self.slug @@ -41,7 +43,7 @@ class Page(object): if settings.get('CLEAN_URLS', False): self.url = clean_url - else: + elif hasattr(self, 'save_as'): self.url = self.save_as if filename: diff --git a/pelican/tests/test_contents.py b/tests/test_contents.py similarity index 99% rename from pelican/tests/test_contents.py rename to tests/test_contents.py index 10d3741a..c1ef27e2 100644 --- a/pelican/tests/test_contents.py +++ b/tests/test_contents.py @@ -23,14 +23,12 @@ class TestPage(TestCase): page = Page('content', metadatas={'title': 'foobar'}) page.check_properties() - def test_slug(self): # if a title is given, it should be used to generate the slug page = Page('content', {'title': 'foobar is foo'}) self.assertEqual(page.slug, 'foobar-is-foo') - def test_defaultlang(self): # if no lang is given, default to the default one page = Page('content') From 8454b0d1b8f160227ebc15f5c94724edd682d628 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Thu, 13 Jan 2011 00:46:10 +0100 Subject: [PATCH 3/3] Tests for settings. --- pelican/settings.py | 48 +++++++++++++++++----------------- pelican/tests/test_settings.py | 16 ------------ samples/pelican.conf.py | 14 ++++++---- tests/__init__.py | 0 tests/test_settings.py | 34 ++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 45 deletions(-) delete mode 100644 pelican/tests/test_settings.py create mode 100644 tests/__init__.py create mode 100644 tests/test_settings.py diff --git a/pelican/settings.py b/pelican/settings.py index 2178c5ca..13270a4d 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -1,35 +1,35 @@ import os -_DEFAULT_THEME = os.sep.join([os.path.dirname(os.path.abspath(__file__)), +DEFAULT_THEME = os.sep.join([os.path.dirname(os.path.abspath(__file__)), "themes/notmyidea"]) -_DEFAULT_CONFIG = {'PATH': None, - 'THEME': _DEFAULT_THEME, - 'OUTPUT_PATH': 'output/', - 'MARKUP': ('rst', 'md'), - 'STATIC_PATHS': ['images',], - 'THEME_STATIC_PATHS': ['static',], - 'FEED': 'feeds/all.atom.xml', - 'CATEGORY_FEED': 'feeds/%s.atom.xml', - 'TRANSLATION_FEED': 'feeds/all-%s.atom.xml', - 'SITENAME': 'A Pelican Blog', - 'DISPLAY_PAGES_ON_MENU': True, - 'PDF_GENERATOR': False, - 'DEFAULT_CATEGORY': 'misc', - 'FALLBACK_ON_FS_DATE': True, - 'CSS_FILE': 'main.css', - 'REVERSE_ARCHIVE_ORDER': False, - 'KEEP_OUTPUT_DIRECTORY': False, - 'CLEAN_URLS': False, # use /blah/ instead /blah.html in urls - 'RELATIVE_URLS': True, - 'DEFAULT_LANG': 'en', - 'PELICAN_CLASS': 'pelican.Pelican', - 'JINJA_EXTENSIONS': [], +DEFAULT_CONFIG = {'PATH': None, + 'THEME': DEFAULT_THEME, + 'OUTPUT_PATH': 'output/', + 'MARKUP': ('rst', 'md'), + 'STATIC_PATHS': ['images',], + 'THEME_STATIC_PATHS': ['static',], + 'FEED': 'feeds/all.atom.xml', + 'CATEGORY_FEED': 'feeds/%s.atom.xml', + 'TRANSLATION_FEED': 'feeds/all-%s.atom.xml', + 'SITENAME': 'A Pelican Blog', + 'DISPLAY_PAGES_ON_MENU': True, + 'PDF_GENERATOR': False, + 'DEFAULT_CATEGORY': 'misc', + 'FALLBACK_ON_FS_DATE': True, + 'CSS_FILE': 'main.css', + 'REVERSE_ARCHIVE_ORDER': False, + 'KEEP_OUTPUT_DIRECTORY': False, + 'CLEAN_URLS': False, # use /blah/ instead /blah.html in urls + 'RELATIVE_URLS': True, + 'DEFAULT_LANG': 'en', + 'PELICAN_CLASS': 'pelican.Pelican', + 'JINJA_EXTENSIONS': [], } def read_settings(filename): """Load a Python file into a dictionary. """ - context = _DEFAULT_CONFIG.copy() + context = DEFAULT_CONFIG.copy() if filename: tempdict = {} execfile(filename, tempdict) diff --git a/pelican/tests/test_settings.py b/pelican/tests/test_settings.py deleted file mode 100644 index 58e38378..00000000 --- a/pelican/tests/test_settings.py +++ /dev/null @@ -1,16 +0,0 @@ -from unittest2 import TestCase - -from pelican.settings import read_settings, DEFAULT_CONFIG - -class SettingsTest(TestCase): - - def test_read_settings(self): - # providing no file should return the default values - settings = read_settings(None) - self.assertDictEqual(settings, DEFAULT_CONFIG) - - # providing a file should read it, replace the default values and append - # new values to the settings, if any - settings = read_settings(mock) - self.assertIn('key', settings) - self.assertEqual(settings['KEY' diff --git a/samples/pelican.conf.py b/samples/pelican.conf.py index daf1d20d..d796e2d5 100755 --- a/samples/pelican.conf.py +++ b/samples/pelican.conf.py @@ -8,14 +8,18 @@ DISQUS_SITENAME = "blog-notmyidea" PDF_GENERATOR = False LINKS = (('Biologeek', 'http://biologeek.org'), - ('Filyb', "http://filyb.info/"), - ('Libert-fr', "http://www.libert-fr.com"), - ('N1k0', "http://prendreuncafe.com/blog/"), - (u'Tarek Ziadé', "http://ziade.org/blog"), - ('Zubin Mithra', "http://zubin71.wordpress.com/"),) + ('Filyb', "http://filyb.info/"), + ('Libert-fr', "http://www.libert-fr.com"), + ('N1k0', "http://prendreuncafe.com/blog/"), + (u'Tarek Ziadé', "http://ziade.org/blog"), + ('Zubin Mithra', "http://zubin71.wordpress.com/"),) SOCIAL = (('twitter', 'http://twitter.com/ametaireau'), ('lastfm', 'http://lastfm.com/user/akounet'), ('github', 'http://github.com/ametaireau'),) STATIC_PATHS = ["pictures",] + +# foobar will not be used, because it's not in caps. All configuration keys +# have to be in caps +foobar = "barbaz" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 00000000..a22fcde0 --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,34 @@ +from unittest2 import TestCase +import os + +from pelican.settings import read_settings, DEFAULT_CONFIG + +SETTINGS = os.sep.join([os.path.dirname(os.path.abspath(__file__)), + "../samples/pelican.conf.py"]) + +class SettingsTest(TestCase): + + + def test_read_settings(self): + # providing a file, it should read it, replace the default values and append + # new values to the settings, if any + settings = read_settings(SETTINGS) + + # overwrite existing settings + self.assertEqual(settings.get('SITENAME'), u"Alexis' log") + + # add new settings + self.assertEqual(settings.get('SITEURL'), 'http://blog.notmyidea.org') + + # keep default settings if not defined + self.assertEqual(settings.get('DEFAULT_CATEGORY'), + DEFAULT_CONFIG['DEFAULT_CATEGORY']) + + # do not copy keys not in caps + self.assertNotIn('foobar', settings) + + + def test_empty_read_settings(self): + # providing no file should return the default values + settings = read_settings(None) + self.assertDictEqual(settings, DEFAULT_CONFIG)