diff options
| author | 2013-02-13 18:35:39 -0300 | |
|---|---|---|
| committer | 2013-02-13 18:35:39 -0300 | |
| commit | a40930043121a4b60de8526d58417761a54ab718 (patch) | |
| tree | 383c5cf8e320761ee942619282fe51be625179a7 /tests | |
| parent | 9c5708cc92af894e414bc76ee35ec2230de5d288 (diff) | |
Imported Upstream version 5.2upstream/5.2
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/import_wordpress_and_build_workflow.py | 42 | ||||
| -rw-r--r-- | tests/test_command_import_wordpress.py | 228 | ||||
| -rw-r--r-- | tests/test_command_init.py | 63 | ||||
| -rw-r--r-- | tests/test_plugin_importing.py | 16 | ||||
| -rw-r--r-- | tests/test_rss_feeds.py | 30 | ||||
| -rw-r--r-- | tests/test_utils.py | 121 | ||||
| -rw-r--r-- | tests/wordpress_export_example.xml | 14 |
7 files changed, 478 insertions, 36 deletions
diff --git a/tests/import_wordpress_and_build_workflow.py b/tests/import_wordpress_and_build_workflow.py new file mode 100644 index 0000000..90cb6a8 --- /dev/null +++ b/tests/import_wordpress_and_build_workflow.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +""" +Script to test the import workflow. + +It will remove an existing Nikola installation and then install from the +package directory. +After that it will do create a new site with the import_wordpress +command and use that newly created site to make a build. +""" +from __future__ import unicode_literals, print_function + +import os +import shutil + +TEST_SITE_DIRECTORY = 'import_test_site' + + +def main(import_directory=None): + if import_directory is None: + import_directory = TEST_SITE_DIRECTORY + + if os.path.exists(import_directory): + print('deleting %s' % import_directory) + shutil.rmtree(import_directory) + + test_directory = os.path.dirname(__file__) + package_directory = os.path.abspath(os.path.join(test_directory, '..')) + + os.system('echo "y" | pip uninstall Nikola') + os.system('pip install %s' % package_directory) + os.system('nikola') + import_file = os.path.join(test_directory, 'wordpress_export_example.xml') + os.system( + 'nikola import_wordpress -f %s -o %s' % (import_file, import_directory)) + + assert os.path.exists( + import_directory), "The directory %s should be existing." + os.chdir(import_directory) + os.system('nikola build') + +if __name__ == '__main__': + main() diff --git a/tests/test_command_import_wordpress.py b/tests/test_command_import_wordpress.py index 4a30dba..bda9b49 100644 --- a/tests/test_command_import_wordpress.py +++ b/tests/test_command_import_wordpress.py @@ -7,9 +7,10 @@ import unittest import mock -class CommandImportWordpressTest(unittest.TestCase): +class BasicCommandImportWordpress(unittest.TestCase): def setUp(self): - self.import_command = nikola.plugins.command_import_wordpress.CommandImportWordpress() + self.import_command = nikola.plugins.command_import_wordpress.CommandImportWordpress( + ) self.import_filename = os.path.abspath( os.path.join(os.path.dirname(__file__), 'wordpress_export_example.xml')) @@ -18,25 +19,83 @@ class CommandImportWordpressTest(unittest.TestCase): del self.import_command del self.import_filename - def test_create_import_work_without_argument(self): - # Running this without an argument must not fail. - # It should show the proper usage of the command. - self.import_command.run() + +class CommandImportWordpressRunTest(BasicCommandImportWordpress): + def setUp(self): + super(self.__class__, self).setUp() + self.data_import = mock.MagicMock() + self.site_generation = mock.MagicMock() + self.write_urlmap = mock.MagicMock() + self.write_configuration = mock.MagicMock() + + site_generation_patch = mock.patch( + 'nikola.plugins.command_import_wordpress.CommandImportWordpress.generate_base_site', self.site_generation) + data_import_patch = mock.patch( + 'nikola.plugins.command_import_wordpress.CommandImportWordpress.import_posts', self.data_import) + write_urlmap_patch = mock.patch( + 'nikola.plugins.command_import_wordpress.CommandImportWordpress.write_urlmap_csv', self.write_urlmap) + write_configuration_patch = mock.patch( + 'nikola.plugins.command_import_wordpress.CommandImportWordpress.write_configuration', self.write_configuration) + + self.patches = [site_generation_patch, data_import_patch, + write_urlmap_patch, write_configuration_patch] + for patch in self.patches: + patch.start() + + def tearDown(self): + del self.data_import + del self.site_generation + del self.write_urlmap + del self.write_configuration + + for patch in self.patches: + patch.stop() + del self.patches + + super(self.__class__, self).tearDown() def test_create_import(self): - data_import = mock.MagicMock() - site_generation = mock.MagicMock() - write_urlmap = mock.MagicMock() - write_configuration = mock.MagicMock() + valid_import_arguments = ( + ['--filename', self.import_filename], + ['-f', self.import_filename, '-o', 'some_folder'], + [self.import_filename], + [self.import_filename, 'folder_argument'], + ) - with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.generate_base_site', site_generation): - with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.import_posts', data_import): - with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.write_urlmap_csv', write_urlmap): - with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.write_configuration', write_configuration): - self.import_command.run(self.import_filename) + for arguments in valid_import_arguments: + self.import_command.run(*arguments) - self.assertTrue(site_generation.called) - self.assertTrue(data_import.called) + self.assertTrue(self.site_generation.called) + self.assertTrue(self.data_import.called) + self.assertTrue(self.write_urlmap.called) + self.assertTrue(self.write_configuration.called) + self.assertFalse(self.import_command.exclude_drafts) + + def test_ignoring_drafts(self): + valid_import_arguments = ( + ['--filename', self.import_filename, '--no-drafts'], + ['-f', self.import_filename, '-o', 'some_folder', '-d'], + ) + + for arguments in valid_import_arguments: + self.import_command.run(*arguments) + self.assertTrue(self.import_command.exclude_drafts) + + def test_getting_help(self): + for arguments in (['-h'], ['--help']): + self.assertRaises(SystemExit, self.import_command.run, *arguments) + + self.assertFalse(self.site_generation.called) + self.assertFalse(self.data_import.called) + self.assertFalse(self.write_urlmap.called) + self.assertFalse(self.write_configuration.called) + + +class CommandImportWordpressTest(BasicCommandImportWordpress): + def test_create_import_work_without_argument(self): + # Running this without an argument must not fail. + # It should show the proper usage of the command. + self.import_command.run() def test_populate_context(self): channel = self.import_command.get_channel_from_file( @@ -48,7 +107,8 @@ class CommandImportWordpressTest(unittest.TestCase): self.assertEqual('de', context['DEFAULT_LANG']) self.assertEqual('Wordpress blog title', context['BLOG_TITLE']) - self.assertEqual('Nikola test blog ;) - with moré Ümläüts', context['BLOG_DESCRIPTION']) + self.assertEqual('Nikola test blog ;) - with moré Ümläüts', + context['BLOG_DESCRIPTION']) self.assertEqual('http://some.blog', context['BLOG_URL']) self.assertEqual('mail@some.blog', context['BLOG_EMAIL']) self.assertEqual('Niko', context['BLOG_AUTHOR']) @@ -59,6 +119,7 @@ class CommandImportWordpressTest(unittest.TestCase): self.import_command.context = self.import_command.populate_context( channel) self.import_command.url_map = {} # For testing we use an empty one. + self.import_command.output_folder = 'new_site' write_metadata = mock.MagicMock() write_content = mock.MagicMock() @@ -71,22 +132,137 @@ class CommandImportWordpressTest(unittest.TestCase): self.import_command.import_posts(channel) self.assertTrue(download_mock.called) - download_mock.assert_any_call(u'http://some.blog/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png', u'new_site/files/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png') + download_mock.assert_any_call( + 'http://some.blog/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png', + 'new_site/files/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png') self.assertTrue(write_metadata.called) - write_metadata.assert_any_call(u'new_site/stories/kontakt.meta', 'Kontakt', u'kontakt', '2009-07-16 20:20:32', None, []) + write_metadata.assert_any_call( + 'new_site/stories/kontakt.meta', 'Kontakt', + 'kontakt', '2009-07-16 20:20:32', None, []) self.assertTrue(write_content.called) - write_content.assert_any_call(u'new_site/posts/200704hoert.wp', '...!\n\n\n\n[caption id="attachment_16" align="alignnone" width="739" caption="caption test"]<img class="size-full wp-image-16" title="caption test" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="caption test" width="739" height="517" />[/caption]\n\n\n\nNicht, dass daran jemals Zweifel bestanden.') - write_content.assert_any_call(u'new_site/posts/200807arzt-und-pfusch-s-i-c-k.wp', u'<img class="size-full wp-image-10 alignright" title="Arzt+Pfusch - S.I.C.K." src="http://some.blog/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png" alt="Arzt+Pfusch - S.I.C.K." width="210" height="209" />Arzt+Pfusch - S.I.C.K.Gerade bin ich \xfcber das Album <em>S.I.C.K</em> von <a title="Arzt+Pfusch" href="http://www.arztpfusch.com/" target="_blank">Arzt+Pfusch</a> gestolpert, welches Arzt+Pfusch zum Download f\xfcr lau anbieten. Das Album steht unter einer Creative Commons <a href="http://creativecommons.org/licenses/by-nc-nd/3.0/de/">BY-NC-ND</a>-Lizenz.\n\nDie Ladung <em>noisebmstupidevildustrial</em> gibts als MP3s mit <a href="http://www.archive.org/download/dmp005/dmp005_64kb_mp3.zip">64kbps</a> und <a href="http://www.archive.org/download/dmp005/dmp005_vbr_mp3.zip">VBR</a>, als Ogg Vorbis und als FLAC (letztere <a href="http://www.archive.org/details/dmp005">hier</a>). <a href="http://www.archive.org/download/dmp005/dmp005-artwork.zip">Artwork</a> und <a href="http://www.archive.org/download/dmp005/dmp005-lyrics.txt">Lyrics</a> gibts nochmal einzeln zum Download.') - write_content.assert_any_call(u'new_site/stories/kontakt.wp', u'<h1>Datenschutz</h1>\n\nIch erhebe und speichere automatisch in meine Server Log Files Informationen, die dein Browser an mich \xfcbermittelt. Dies sind:\n\n<ul>\n\n <li>Browsertyp und -version</li>\n\n <li>verwendetes Betriebssystem</li>\n\n <li>Referrer URL (die zuvor besuchte Seite)</li>\n\n <li>IP Adresse des zugreifenden Rechners</li>\n\n <li>Uhrzeit der Serveranfrage.</li>\n\n</ul>\n\nDiese Daten sind f\xfcr mich nicht bestimmten Personen zuordenbar. Eine Zusammenf\xfchrung dieser Daten mit anderen Datenquellen wird nicht vorgenommen, die Daten werden einzig zu statistischen Zwecken erhoben.') + write_content.assert_any_call('new_site/posts/200704hoert.wp', 'An image.\n\n\n\n<img class="size-full wp-image-16" title="caption test" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="caption test" width="739" height="517" />\n\n\n\nSome source code.\n\n\n\n\n~~~~~~~~~~~~{.Python}\n\n\nimport sys\n\nprint sys.version\n\n\n~~~~~~~~~~~~\n\n\n\n\nThe end.\n\n') + write_content.assert_any_call( + 'new_site/posts/200807arzt-und-pfusch-s-i-c-k.wp', '<img class="size-full wp-image-10 alignright" title="Arzt+Pfusch - S.I.C.K." src="http://some.blog/wp-content/uploads/2008/07/arzt_und_pfusch-sick-cover.png" alt="Arzt+Pfusch - S.I.C.K." width="210" height="209" />Arzt+Pfusch - S.I.C.K.Gerade bin ich \xfcber das Album <em>S.I.C.K</em> von <a title="Arzt+Pfusch" href="http://www.arztpfusch.com/" target="_blank">Arzt+Pfusch</a> gestolpert, welches Arzt+Pfusch zum Download f\xfcr lau anbieten. Das Album steht unter einer Creative Commons <a href="http://creativecommons.org/licenses/by-nc-nd/3.0/de/">BY-NC-ND</a>-Lizenz.\n\nDie Ladung <em>noisebmstupidevildustrial</em> gibts als MP3s mit <a href="http://www.archive.org/download/dmp005/dmp005_64kb_mp3.zip">64kbps</a> und <a href="http://www.archive.org/download/dmp005/dmp005_vbr_mp3.zip">VBR</a>, als Ogg Vorbis und als FLAC (letztere <a href="http://www.archive.org/details/dmp005">hier</a>). <a href="http://www.archive.org/download/dmp005/dmp005-artwork.zip">Artwork</a> und <a href="http://www.archive.org/download/dmp005/dmp005-lyrics.txt">Lyrics</a> gibts nochmal einzeln zum Download.') + write_content.assert_any_call( + 'new_site/stories/kontakt.wp', '<h1>Datenschutz</h1>\n\nIch erhebe und speichere automatisch in meine Server Log Files Informationen, die dein Browser an mich \xfcbermittelt. Dies sind:\n\n<ul>\n\n <li>Browsertyp und -version</li>\n\n <li>verwendetes Betriebssystem</li>\n\n <li>Referrer URL (die zuvor besuchte Seite)</li>\n\n <li>IP Adresse des zugreifenden Rechners</li>\n\n <li>Uhrzeit der Serveranfrage.</li>\n\n</ul>\n\nDiese Daten sind f\xfcr mich nicht bestimmten Personen zuordenbar. Eine Zusammenf\xfchrung dieser Daten mit anderen Datenquellen wird nicht vorgenommen, die Daten werden einzig zu statistischen Zwecken erhoben.') self.assertTrue(len(self.import_command.url_map) > 0) - self.assertEqual(self.import_command.url_map['http://some.blog/2007/04/hoert/'], u'http://some.blog/posts/200704hoert.html') - self.assertEqual(self.import_command.url_map['http://some.blog/2008/07/arzt-und-pfusch-s-i-c-k/'], u'http://some.blog/posts/200807arzt-und-pfusch-s-i-c-k.html') - self.assertEqual(self.import_command.url_map['http://some.blog/kontakt/'], u'http://some.blog/stories/kontakt.html') + self.assertEqual( + self.import_command.url_map['http://some.blog/2007/04/hoert/'], + 'http://some.blog/posts/200704hoert.html') + self.assertEqual( + self.import_command.url_map[ + 'http://some.blog/2008/07/arzt-und-pfusch-s-i-c-k/'], + 'http://some.blog/posts/200807arzt-und-pfusch-s-i-c-k.html') + self.assertEqual( + self.import_command.url_map['http://some.blog/kontakt/'], + 'http://some.blog/stories/kontakt.html') + + def test_transforming_content(self): + """Applying markup conversions to content.""" + transform_sourcecode = mock.MagicMock() + transform_caption = mock.MagicMock() + + with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.transform_sourcecode', transform_sourcecode): + with mock.patch('nikola.plugins.command_import_wordpress.CommandImportWordpress.transform_caption', transform_caption): + self.import_command.transform_content("random content") + + self.assertTrue(transform_sourcecode.called) + self.assertTrue(transform_caption.called) + + def test_transforming_source_code(self): + """ + Tests the handling of sourcecode tags. + """ + content = """Hello World. +[sourcecode language="Python"] +import sys +print sys.version +[/sourcecode]""" + + content = self.import_command.transform_sourcecode(content) + + self.assertFalse('[/sourcecode]' in content) + self.assertFalse('[sourcecode language=' in content) + + replaced_content = """Hello World. + +~~~~~~~~~~~~{.Python} + +import sys +print sys.version + +~~~~~~~~~~~~ +""" + + self.assertEqual(content, replaced_content) + + def test_transform_caption(self): + caption = '[caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]<img class="size-full wp-image-16" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="beautiful picture" width="739" height="517" />[/caption]' + transformed_content = self.import_command.transform_caption(caption) + + expected_content = '<img class="size-full wp-image-16" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="beautiful picture" width="739" height="517" />' + + self.assertEqual(transformed_content, expected_content) + + def test_transform_multiple_captions_in_a_post(self): + content = """asdasdas +[caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]<img class="size-full wp-image-16" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="beautiful picture" width="739" height="517" />[/caption] +asdasdas +asdasdas +[caption id="attachment_16" align="alignnone" width="739" caption="beautiful picture"]<img class="size-full wp-image-16" title="pretty" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="beautiful picture" width="739" height="517" />[/caption] +asdasdas""" + + expected_content = """asdasdas +<img class="size-full wp-image-16" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="beautiful picture" width="739" height="517" /> +asdasdas +asdasdas +<img class="size-full wp-image-16" title="pretty" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="beautiful picture" width="739" height="517" /> +asdasdas""" + + self.assertEqual( + expected_content, self.import_command.transform_caption(content)) + + def test_transform_caption_with_link_inside(self): + content = """[caption caption="Fehlermeldung"]<a href="http://some.blog/openttd-missing_sound.png"><img class="size-thumbnail wp-image-551" title="openttd-missing_sound" src="http://some.blog/openttd-missing_sound-150x150.png" alt="Fehlermeldung" /></a>[/caption]""" + transformed_content = self.import_command.transform_caption(content) + + expected_content = """<a href="http://some.blog/openttd-missing_sound.png"><img class="size-thumbnail wp-image-551" title="openttd-missing_sound" src="http://some.blog/openttd-missing_sound-150x150.png" alt="Fehlermeldung" /></a>""" + self.assertEqual(expected_content, transformed_content) + + def test_get_configuration_output_path(self): + self.import_command.output_folder = 'new_site' + default_config_path = os.path.join('new_site', 'conf.py') + + self.import_command.import_into_existing_site = False + self.assertEqual(default_config_path, + self.import_command.get_configuration_output_path()) + + self.import_command.import_into_existing_site = True + config_path_with_timestamp = self.import_command.get_configuration_output_path( + ) + self.assertNotEqual(default_config_path, config_path_with_timestamp) + self.assertTrue('wordpress_import' in config_path_with_timestamp) + + def test_write_content_does_not_detroy_text(self): + content = b"""<h1>Installation</h1> +Follow the instructions <a title="Installing Jenkins" href="https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins">described here</a>. + +<h1>Plugins</h1> +There are many plugins. +<h2>Violations</h2> +You can use the <a title="Jenkins Plugin: Violations" href="https://wiki.jenkins-ci.org/display/JENKINS/Violations">Violations</a> plugin.""" + open_mock = mock.mock_open() + with mock.patch('nikola.plugins.command_import_wordpress.open', open_mock, create=True): + self.import_command.write_content('some_file', content) + open_mock.assert_called_once_with('some_file', 'wb+') + call_context = open_mock() + call_context.write.assert_called_once_with( + content.join([b'<html><body>', b'</body></html>'])) if __name__ == '__main__': unittest.main() diff --git a/tests/test_command_init.py b/tests/test_command_init.py new file mode 100644 index 0000000..3176c1f --- /dev/null +++ b/tests/test_command_init.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from context import nikola +import os +import unittest +import mock + + +class CommandInitCallTest(unittest.TestCase): + def setUp(self): + self.copy_sample_site = mock.MagicMock() + self.create_configuration = mock.MagicMock() + self.create_empty_site = mock.MagicMock() + copy_sample_site_patch = mock.patch( + 'nikola.plugins.command_init.CommandInit.copy_sample_site', self.copy_sample_site) + create_configuration_patch = mock.patch( + 'nikola.plugins.command_init.CommandInit.create_configuration', self.create_configuration) + create_empty_site_patch = mock.patch( + 'nikola.plugins.command_init.CommandInit.create_empty_site', self.create_empty_site) + + self.patches = [copy_sample_site_patch, + create_configuration_patch, create_empty_site_patch] + for patch in self.patches: + patch.start() + + self.init_commad = nikola.plugins.command_init.CommandInit() + + def tearDown(self): + for patch in self.patches: + patch.stop() + del self.patches + + del self.copy_sample_site + del self.create_configuration + del self.create_empty_site + + def test_init_default(self): + for arguments in (('destination', '--demo'),): + self.init_commad.run(*arguments) + + self.assertTrue(self.create_configuration.called) + self.assertTrue(self.copy_sample_site.called) + self.assertFalse(self.create_empty_site.called) + + def test_init_called_without_target(self): + self.init_commad.run() + + self.assertFalse(self.create_configuration.called) + self.assertFalse(self.copy_sample_site.called) + self.assertFalse(self.create_empty_site.called) + + def test_init_empty_dir(self): + for arguments in (('destination', ), ('destination', '--empty')): + self.init_commad.run(*arguments) + + self.assertTrue(self.create_configuration.called) + self.assertFalse(self.copy_sample_site.called) + self.assertTrue(self.create_empty_site.called) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_plugin_importing.py b/tests/test_plugin_importing.py new file mode 100644 index 0000000..7e1e013 --- /dev/null +++ b/tests/test_plugin_importing.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals, absolute_import + +from context import nikola +import unittest + + +class ImportPluginsTest(unittest.TestCase): + def test_importing_command_import_wordpress(self): + import nikola.plugins.command_import_wordpress + + def test_importing_task_sitemap(self): + import nikola.plugins.task_sitemap.sitemap_gen + + def test_importing_compile_rest(self): + import nikola.plugins.compile_rest
\ No newline at end of file diff --git a/tests/test_rss_feeds.py b/tests/test_rss_feeds.py index 2b48f36..ae1cd41 100644 --- a/tests/test_rss_feeds.py +++ b/tests/test_rss_feeds.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals import unittest import os import re -from StringIO import StringIO +from io import StringIO import mock @@ -32,21 +33,36 @@ class RSSFeedTest(unittest.TestCase): {'en': ''}, 'en', self.blog_url, - 'unused message.') + 'unused message.', + 'post.tmpl') opener_mock = mock.mock_open() - with mock.patch('nikola.nikola.utils.open', opener_mock, create=True): + with mock.patch('nikola.nikola.utils.codecs.open', opener_mock, create=True): nikola.nikola.utils.generic_rss_renderer('en', "blog_title", self.blog_url, "blog_description", [example_post, ], - 'testfeed.rss') - - self.file_content = ''.join( - [call[1][0] for call in opener_mock.mock_calls[2:-1]]) + 'testfeed.rss', + True) + + opener_mock.assert_called_once_with( + 'testfeed.rss', 'wb+', 'utf-8') + + # Python 3 / unicode strings workaround + # lxml will complain if the encoding is specified in the + # xml when running with unicode strings. + # We do not include this in our content. + open_handle = opener_mock() + file_content = [call[1][0] + for call in open_handle.mock_calls[1:-1]][0] + splitted_content = file_content.split('\n') + self.encoding_declaration = splitted_content[0] + content_without_encoding_declaration = splitted_content[1:] + self.file_content = '\n'.join( + content_without_encoding_declaration) def tearDown(self): pass diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..8ec016f --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,121 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from context import nikola +import os +import unittest +import mock + + +class GetMetaTest(unittest.TestCase): + def test_getting_metadata_from_content(self): + file_metadata = [".. title: Nikola needs more tests!\n", + ".. slug: write-tests-now\n", + ".. date: 2012/09/15 19:52:05\n", + ".. tags:\n", + ".. link:\n", + ".. description:\n", + "Post content\n"] + + opener_mock = mock.mock_open(read_data=file_metadata) + opener_mock.return_value.readlines.return_value = file_metadata + + with mock.patch('nikola.utils.codecs.open', opener_mock, create=True): + (title, slug, date, tags, link, + description) = nikola.utils.get_meta('file_with_metadata') + + self.assertEqual('Nikola needs more tests!', title) + self.assertEqual('write-tests-now', slug) + self.assertEqual('2012/09/15 19:52:05', date) + self.assertEqual('', tags) + self.assertEqual('', link) + self.assertEqual('', description) + + def test_get_title_from_rest(self): + file_metadata = [".. slug: write-tests-now\n", + ".. date: 2012/09/15 19:52:05\n", + ".. tags:\n", + ".. link:\n", + ".. description:\n", + "Post Title\n", + "----------\n"] + + opener_mock = mock.mock_open(read_data=file_metadata) + opener_mock.return_value.readlines.return_value = file_metadata + + with mock.patch('nikola.utils.codecs.open', opener_mock, create=True): + (title, slug, date, tags, link, + description) = nikola.utils.get_meta('file_with_metadata') + + self.assertEqual('Post Title', title) + self.assertEqual('write-tests-now', slug) + self.assertEqual('2012/09/15 19:52:05', date) + self.assertEqual('', tags) + self.assertEqual('', link) + self.assertEqual('', description) + + def test_get_title_from_fname(self): + file_metadata = [".. slug: write-tests-now\n", + ".. date: 2012/09/15 19:52:05\n", + ".. tags:\n", + ".. link:\n", + ".. description:\n"] + + opener_mock = mock.mock_open(read_data=file_metadata) + opener_mock.return_value.readlines.return_value = file_metadata + + with mock.patch('nikola.utils.codecs.open', opener_mock, create=True): + (title, slug, date, tags, link, + description) = nikola.utils.get_meta('file_with_metadata') + + self.assertEqual('file_with_metadata', title) + self.assertEqual('write-tests-now', slug) + self.assertEqual('2012/09/15 19:52:05', date) + self.assertEqual('', tags) + self.assertEqual('', link) + self.assertEqual('', description) + + def test_use_filename_as_slug_fallback(self): + file_metadata = [".. title: Nikola needs more tests!\n", + ".. date: 2012/09/15 19:52:05\n", + ".. tags:\n", + ".. link:\n", + ".. description:\n", + "Post content\n"] + + opener_mock = mock.mock_open(read_data=file_metadata) + opener_mock.return_value.readlines.return_value = file_metadata + + with mock.patch('nikola.utils.codecs.open', opener_mock, create=True): + (title, slug, date, tags, link, + description) = nikola.utils.get_meta('Slugify this') + + self.assertEqual('Nikola needs more tests!', title) + self.assertEqual('slugify-this', slug) + self.assertEqual('2012/09/15 19:52:05', date) + self.assertEqual('', tags) + self.assertEqual('', link) + self.assertEqual('', description) + + def test_extracting_metadata_from_filename(self): + with mock.patch('nikola.utils.codecs.open', create=True): + ( + title, slug, date, tags, link, description) = nikola.utils.get_meta('2013-01-23-the_slug-dubdubtitle.md', + '(?P<date>\d{4}-\d{2}-\d{2})-(?P<slug>.*)-(?P<title>.*)\.md') + + self.assertEqual('dubdubtitle', title) + self.assertEqual('the_slug', slug) + self.assertEqual('2013-01-23', date) + self.assertEqual('', tags) + self.assertEqual('', link) + self.assertEqual('', description) + + def test_get_meta_slug_only_from_filename(self): + with mock.patch('nikola.utils.codecs.open', create=True): + (title, slug, date, tags, link, + description) = nikola.utils.get_meta('some/path/the_slug.md') + + self.assertEqual('the_slug', slug) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/wordpress_export_example.xml b/tests/wordpress_export_example.xml index 7517193..8ef1325 100644 --- a/tests/wordpress_export_example.xml +++ b/tests/wordpress_export_example.xml @@ -58,17 +58,25 @@ </item> <item> - <title>Caption test</title> + <title>Transformation test</title> <link>http://some.blog/2007/04/hoert/</link> <pubDate>Fri, 27 Apr 2007 13:02:35 +0000</pubDate> <dc:creator>Niko</dc:creator> <guid isPermaLink="false">http://some.blog/?p=17</guid> <description></description> - <content:encoded><![CDATA[...! + <content:encoded><![CDATA[An image. [caption id="attachment_16" align="alignnone" width="739" caption="caption test"]<img class="size-full wp-image-16" title="caption test" src="http://some.blog/wp-content/uploads/2009/07/caption_test.jpg" alt="caption test" width="739" height="517" />[/caption] -Nicht, dass daran jemals Zweifel bestanden.]]></content:encoded> +Some source code. + +[sourcecode language="Python"] +import sys +print sys.version +[/sourcecode] + +The end. +]]></content:encoded> <excerpt:encoded><![CDATA[]]></excerpt:encoded> <wp:post_id>17</wp:post_id> <wp:post_date>2007-04-27 15:02:35</wp:post_date> |
