Skip to content

Commit

Permalink
Add content.py and test_content.py.
Browse files Browse the repository at this point in the history
Port test_docparser.py to pytest.
Use pytest for running tests.
  • Loading branch information
yaph committed Jan 23, 2020
1 parent 3d5a735 commit 309214a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ lint:
flake8 logya tests

test:
python setup.py test
python -m pytest tests/

coverage:
coverage run --source logya setup.py test
Expand Down
25 changes: 25 additions & 0 deletions logya/content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from pathlib import Path

from logya.docparser import parse


def content_type(path):
if path.suffix in ['.html', '.htm']:
return 'html'
if path.suffix in ['.md', '.markdown']:
return 'markdown'


def read(filename):
path = Path(filename)
content = path.read_text().strip()
try:
return parse(content, content_type=content_type(path))
except Exception as err:
print(f'Error parsing: {filename}\n{err}')
# TODO add url, created and updated attrs if not set


def write(filename, doc):
pass
6 changes: 3 additions & 3 deletions tests/fixtures/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
---
url: /test/url/
---
[Sample Link](/sample/link/)
[Link](/url/)
"""

markdown_attr_list = """
markdown_attrs = """
---
url: /test/url/
---
[Sample Link](/sample/link/){: class="foo bar" title="Some title!" }
[Link with attributes](/url/){: class="foo bar" title="Some title!" }
"""
5 changes: 5 additions & 0 deletions tests/fixtures/site/content/markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
url: /test/markdown/
---
* [Link](/url/)
* [Link with attributes](/url/){: class="foo bar" title="Some title!" }
9 changes: 9 additions & 0 deletions tests/test_content.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from logya.content import read, write


def test_read_markdown():
doc = read('tests/fixtures/site/content/markdown.md')
assert isinstance(doc, dict)
assert '/test/markdown/' == doc['url']
18 changes: 7 additions & 11 deletions tests/test_docparser.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest

from logya.docparser import parse

import tests.fixtures.docs as docs


class TestDocParser(unittest.TestCase):
def test_parse_markdown_link():
doc = parse(docs.markdown_link, content_type='markdown')
assert '<a href="/url/">Link</a>' in doc['body']

def test_markdown_link(self):
parsed = parse(docs.markdown_link, content_type='markdown')
self.assertIn(
'<a href="/sample/link/">Sample Link</a>', parsed.get('body'))

def test_markdown_attr_list(self):
parsed = parse(docs.markdown_attr_list, content_type='markdown')
self.assertIn(
'<a class="foo bar" href="/sample/link/" title="Some title!">Sample Link</a>', parsed.get('body'))
def test_parse_markdown_attrs():
doc = parse(docs.markdown_attrs, content_type='markdown')
assert '<a class="foo bar" href="/url/" title="Some title!">Link with attributes</a>' in doc['body']

0 comments on commit 309214a

Please sign in to comment.