Add tests for content parser

This commit is contained in:
Jake Howard 2017-05-28 22:12:22 +01:00
parent 538685e5b2
commit 58e825c43b
3 changed files with 70 additions and 7 deletions

View file

@ -19,7 +19,7 @@ def fix_references_title(content, config):
return soup.prettify() return soup.prettify()
def add_base_tag(doc, config): def make_images_relative(doc, config):
logger.debug("Adding Base Tag...") logger.debug("Adding Base Tag...")
soup = BeautifulSoup(doc, 'html.parser') soup = BeautifulSoup(doc, 'html.parser')
for img in soup.findAll('img'): for img in soup.findAll('img'):
@ -32,6 +32,8 @@ def add_base_tag(doc, config):
def add_body_class(doc, config): def add_body_class(doc, config):
logger.debug("Adding Body Class...") logger.debug("Adding Body Class...")
soup = BeautifulSoup(doc, 'html.parser') soup = BeautifulSoup(doc, 'html.parser')
if not soup.body:
return doc
soup.body['class'] = 'content' soup.body['class'] = 'content'
return soup.prettify() return soup.prettify()
@ -47,7 +49,7 @@ def parse_template(doc, config):
parsed_doc = doc parsed_doc = doc
for parser in [ for parser in [
fix_references_title, fix_references_title,
add_base_tag, make_images_relative,
add_body_class, add_body_class,
]: ]:
parsed_doc = parser(parsed_doc, config) parsed_doc = parser(parsed_doc, config)

View file

@ -1,6 +1,7 @@
import unittest import unittest
import os import os
from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR
from bs4 import BeautifulSoup
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
@ -16,22 +17,25 @@ class BaseTestCase(unittest.TestCase):
} }
def removeFile(self, file): def parse_html(self, html):
return BeautifulSoup(html, 'html.parser')
def remove_file(self, file):
try: try:
os.remove(file) os.remove(file)
except OSError: except OSError:
pass pass
def deleteTemplates(self): def delete_templates(self):
for template in [ for template in [
'header.html', 'header.html',
'footer.html', 'footer.html',
'cover.html', 'cover.html',
'toc.xsl', 'toc.xsl',
]: ]:
self.removeFile(os.path.join(TEMPLATES_DIR, template)) self.remove_file(os.path.join(TEMPLATES_DIR, template))
def tearDown(self): def tearDown(self):
self.deleteTemplates() self.delete_templates()
self.removeFile(os.path.join(STATIC_DIR, 'style.css')) self.remove_file(os.path.join(STATIC_DIR, 'style.css'))

57
tests/test_parser.py Normal file
View file

@ -0,0 +1,57 @@
from tests import BaseTestCase
from md_pdf.build import content
import os
class FixReferencesTitleTestCase(BaseTestCase):
def test_adds_reference_title(self):
html = '<div class="references"></div>'
output = content.fix_references_title(html, self.BASE_VALID_CONFIG)
self.assertIn('references-title', output)
self.assertIn('References', output)
def test_doesnt_modify_if_no_references(self):
html = 'test text'
output = content.fix_references_title(html, self.BASE_VALID_CONFIG)
self.assertNotIn('references-title', output)
self.assertNotIn('References', output)
class RelativeImageTestCase(BaseTestCase):
def test_makes_image_relative(self):
html = '<img src="test-files/test-image.png" />'
output = self.parse_html(content.make_images_relative(html, self.BASE_VALID_CONFIG))
self.assertEqual(output.find('img').attrs['src'], os.path.abspath('test-files/test-image.png'))
def test_leaves_remote_images(self):
html = '<img src="http://example.com/image.png" />'
output = self.parse_html(content.make_images_relative(html, self.BASE_VALID_CONFIG))
self.assertEqual(output.find('img').attrs['src'], 'http://example.com/image.png')
class AddBodyClassTestCase(BaseTestCase):
def test_adds_class(self):
html = '<body></body>'
output = self.parse_html(content.add_body_class(html, self.BASE_VALID_CONFIG))
self.assertEqual(output.body.attrs['class'], ['content'])
def test_doesnt_change(self):
html = 'test content'
output = content.add_body_class(html, self.BASE_VALID_CONFIG)
self.assertEqual(output, html)
class RenderTemplateTestCase(BaseTestCase):
def test_renders_template(self):
html = 'test {{ test }}'
output = content.render_template(html, dict(self.BASE_VALID_CONFIG, **{
'test': 'content'
}))
self.assertEqual(output, 'test content')
def test_changes_nothing(self):
html = 'test test'
output = content.render_template(html, dict(self.BASE_VALID_CONFIG, **{
'test': 'content'
}))
self.assertEqual(output, html)