diff options
Diffstat (limited to 'test/test_content.py')
-rw-r--r-- | test/test_content.py | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/test/test_content.py b/test/test_content.py new file mode 100644 index 0000000..06ec97d --- /dev/null +++ b/test/test_content.py @@ -0,0 +1,114 @@ +import unittest +import shutil +import os + +import makesite +from test import path + + +class ContentTest(unittest.TestCase): + def setUp(self): + self.blog_path = path.temppath('blog') + self.undated_path = os.path.join(self.blog_path, 'foo.txt') + self.dated_path = os.path.join(self.blog_path, '2018-01-01-foo.txt') + self.long_post_path = os.path.join(self.blog_path, 'bar.txt') + self.normal_post_path = os.path.join(self.blog_path, 'baz.txt') + self.md_post_path = os.path.join(self.blog_path, 'qux.md') + self.no_md_post_path = os.path.join(self.blog_path, 'qux.txt') + + os.makedirs(self.blog_path) + + with open(self.undated_path, 'w') as f: + f.write('hello world') + + with open(self.dated_path, 'w') as f: + f.write('hello world') + + with open(self.long_post_path, 'w') as f: + self.long_text = ' \n'.join('word' + str(i) for i in range(50)) + f.write(self.long_text) + + with open(self.normal_post_path, 'w') as f: + f.write('<!-- a: 1 -->\n<!-- b: 2 -->\nFoo') + + with open(self.md_post_path, 'w') as f: + f.write('*Foo*') + + with open(self.no_md_post_path, 'w') as f: + f.write('*Foo*') + + def tearDown(self): + shutil.rmtree(self.blog_path) + + # Rudimentary mock because unittest.mock is unavailable in Python 2.7. + def mock(self, *args): + self.mock_args = args + + def test_content_content(self): + content = makesite.read_content(self.long_post_path) + self.assertEqual(content['content'], self.long_text) + + def test_content_summary(self): + content = makesite.read_content(self.long_post_path) + expected_text = ' '.join('word' + str(i) for i in range(25)) + self.assertEqual(content['summary'], expected_text) + + def test_content_date(self): + content = makesite.read_content(self.dated_path) + self.assertEqual(content['date'], '2018-01-01') + + def test_content_date_missing(self): + content = makesite.read_content(self.undated_path) + self.assertEqual(content['date'], '1970-01-01') + + def test_content_slug_dated(self): + content = makesite.read_content(self.dated_path) + self.assertEqual(content['slug'], 'foo') + + def test_content_slug_undated(self): + content = makesite.read_content(self.undated_path) + self.assertEqual(content['slug'], 'foo') + + def test_content_headers(self): + content = makesite.read_content(self.normal_post_path) + self.assertEqual(content['a'], '1') + self.assertEqual(content['b'], '2') + self.assertEqual(content['content'], 'Foo') + + def test_markdown_rendering(self): + content = makesite.read_content(self.md_post_path) + self.assertEqual(content['content'], '<p><em>Foo</em></p>\n') + + def test_markdown_import_error(self): + makesite._test = 'ImportError' + original_log = makesite.log + + makesite.log = self.mock + self.mock_args = None + content = makesite.read_content(self.md_post_path) + + makesite._test = None + makesite.log = original_log + + self.assertEqual(content['content'], '*Foo*') + self.assertEqual(self.mock_args, + ('WARNING: Cannot render Markdown in {}: {}', + self.md_post_path, 'Error forced by test')) + + def test_no_markdown_rendering(self): + content = makesite.read_content(self.no_md_post_path) + self.assertEqual(content['content'], '*Foo*') + + def test_no_markdown_import_error(self): + makesite._test = 'ImportError' + original_log = makesite.log + + makesite.log = self.mock + self.mock_args = None + content = makesite.read_content(self.no_md_post_path) + + makesite._test = None + makesite.log = original_log + + self.assertEqual(content['content'], '*Foo*') + self.assertIsNone(self.mock_args) |