From 538685e5b287b3e79a7bab04592b33ec7266a548 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 28 May 2017 16:58:58 +0100 Subject: [PATCH] Test config generator --- md_pdf/build/context.py | 7 ++++-- md_pdf/utils.py | 2 ++ tests/__init__.py | 12 +++++++++ tests/test_config.py | 10 +------- tests/test_context.py | 55 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 11 deletions(-) diff --git a/md_pdf/build/context.py b/md_pdf/build/context.py index 61506ff..2e2f018 100644 --- a/md_pdf/build/context.py +++ b/md_pdf/build/context.py @@ -21,8 +21,11 @@ EXTRA_CONTEXT = { def get_context(config, content): config = config.copy() - context = config['context'].copy() - del config['context'] + if 'context' in config: + context = config['context'].copy() + del config['context'] + else: + context = {} context = dict( config, **EXTRA_CONTEXT, diff --git a/md_pdf/utils.py b/md_pdf/utils.py index 101e639..4c39d48 100644 --- a/md_pdf/utils.py +++ b/md_pdf/utils.py @@ -25,6 +25,8 @@ def safe_list_get(l, idx, default): def get_plain_text(content): soup = BeautifulSoup(content, 'html.parser') body = soup.find('body') + if body is None: + return content try: body.find('h1', class_='references-title').extract() body.find('div', class_='references').extract() diff --git a/tests/__init__.py b/tests/__init__.py index 397c12c..5fe62ed 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,6 +4,18 @@ from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR class BaseTestCase(unittest.TestCase): + def setUp(self): + super().setUp() + self.BASE_VALID_CONFIG = { + 'title': 'test title', + 'input': 'test-files/*.md', + 'output_formats': [ + 'html', 'pdf' + ], + 'output_dir': 'out/', + + } + def removeFile(self, file): try: os.remove(file) diff --git a/tests/test_config.py b/tests/test_config.py index b6e34d2..15181ff 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -20,15 +20,7 @@ class ReadConfigTestCase(BaseTestCase): class ConfigValidatorBaseTestCase(BaseTestCase): def setUp(self): - self.BASE_VALID_CONFIG = { - 'title': 'test title', - 'input': 'test-files/*.md', - 'output_formats': [ - 'html', 'pdf' - ], - 'output_dir': 'out/', - - } + super().setUp() validate.validate_config(self.BASE_VALID_CONFIG) diff --git a/tests/test_context.py b/tests/test_context.py index 69d9ee4..4a1722c 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -3,6 +3,7 @@ from md_pdf.build.context import get_context, EXTRA_CONTEXT from md_pdf import consts from datetime import datetime from md_pdf import __version__ +import os class ExtraContextTestCase(BaseTestCase): @@ -20,3 +21,57 @@ class ExtraContextTestCase(BaseTestCase): def test_version(self): self.assertEqual(EXTRA_CONTEXT['mdp_version'], __version__) + + +class ContextTestCase(BaseTestCase): + def test_context_contains_extra(self): + context = get_context(self.BASE_VALID_CONFIG, 'test') + for key in EXTRA_CONTEXT.keys(): + self.assertIn(key, context) + + def test_context_contains_context(self): + config = dict(self.BASE_VALID_CONFIG, **{ + 'context': { + '1': '2' + } + }) + context = get_context(config, 'test') + self.assertEqual(context['1'], '2') + self.assertNotIn('context', context) + + def test_context_contains_config(self): + context = get_context(self.BASE_VALID_CONFIG, 'test') + for key in self.BASE_VALID_CONFIG.keys(): + self.assertIn(key, context) + + def test_has_output_dir(self): + context = get_context(self.BASE_VALID_CONFIG, 'test') + self.assertEqual(context['output_dir'], os.path.abspath(self.BASE_VALID_CONFIG['output_dir'])) + + def test_word_count(self): + config = dict(self.BASE_VALID_CONFIG, **{ + 'show_word_count': True + }) + context = get_context(config, 'testy test test') + self.assertEqual(context['word_count'], 3) + + def test_transparent_datetime_for_submission_date(self): + for value in [ + datetime.now().date(), + datetime.now().time(), + datetime.now() + ]: + config = dict(self.BASE_VALID_CONFIG, **{ + 'submission_date': value + }) + context = get_context(config, 'test') + self.assertEqual(context['submission_date'], value.strftime(consts.DATE_FORMAT)) + + def test_date_format(self): + config = dict(self.BASE_VALID_CONFIG, **{ + 'submission_date': '2017-01-01' + }) + context = get_context(config, 'test') + self.assertEqual(context['submission_date'], '01 January 2017') + +