Test config generator

This commit is contained in:
Jake Howard 2017-05-28 16:58:58 +01:00
parent e71650cd3c
commit 538685e5b2
5 changed files with 75 additions and 11 deletions

View file

@ -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,

View file

@ -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()

View file

@ -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)

View file

@ -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)

View file

@ -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')