1
Fork 0

Start adding basic unit tests

This commit is contained in:
Jake Howard 2016-09-11 21:26:30 +01:00
parent 7b93139c79
commit 9b05ee6f9a
Signed by: jake
GPG key ID: 57AFB45680EDD477
9 changed files with 118 additions and 3 deletions

View file

@ -50,11 +50,13 @@ node_modules:
test: lint spellcheck test: lint spellcheck
lint: lint:
$(ENV)/nose2 --verbose
$(NODE_BIN)/eslint 'theme/static/src/js/' $(NODE_BIN)/eslint 'theme/static/src/js/'
$(NODE_BIN)/sass-lint -vqc .sass-lint.yml $(NODE_BIN)/sass-lint -vqc .sass-lint.yml
$(ENV)/flake8 $(BASEDIR)/plugins/ $(FLAKE8_IGNORE) $(ENV)/flake8 $(BASEDIR)/plugins/ $(FLAKE8_IGNORE)
$(ENV)/flake8 $(BASEDIR)/scripts/ $(FLAKE8_IGNORE) $(ENV)/flake8 $(BASEDIR)/scripts/ $(FLAKE8_IGNORE)
$(ENV)/flake8 $(BASEDIR)/config/ $(FLAKE8_IGNORE) $(ENV)/flake8 $(BASEDIR)/config/ $(FLAKE8_IGNORE)
$(ENV)/flake8 $(BASEDIR)/tests/ $(FLAKE8_IGNORE)
spellcheck: spellcheck:
$(NODE_BIN)/mdspell --en-gb -ranx theme/templates/**/*.* theme/templates/*.* $(NODE_BIN)/mdspell --en-gb -ranx theme/templates/**/*.* theme/templates/*.*

View file

@ -10,6 +10,7 @@ def accounts():
links = {} links = {}
for key, (site, user, url, icon) in settings.accounts.items(): for key, (site, user, url, icon) in settings.accounts.items():
links[key] = DotDictionary({ links[key] = DotDictionary({
'key': key,
'site': site, 'site': site,
'username': user, 'username': user,
'url': url.format(user), 'url': url.format(user),

View file

@ -1,11 +1,12 @@
flake8==2.5.0 flake8==2.5.0
fontawesome_markdown==0.2.5 fontawesome_markdown==0.2.5
git+https://github.com/ryneeverett/python-markdown-comments.git
gitpython==2.0.7 gitpython==2.0.7
iso8601==0.1.11 iso8601==0.1.11
markdown==2.6.6 markdown==2.6.6
git+https://github.com/ryneeverett/python-markdown-comments.git nose2==0.6.5
pelican==3.6.3
pelican-minify==0.9 pelican-minify==0.9
pelican==3.6.3
pyembed-markdown==1.1.0 pyembed-markdown==1.1.0
python-resize-image==1.1.3 python-resize-image==1.1.3
pyyaml==3.12 pyyaml==3.12

33
tests/TestWrapper.py Normal file
View file

@ -0,0 +1,33 @@
import unittest
import os.path
from bs4 import BeautifulSoup
class TestClient:
output_path = os.path.realpath('./output')
def get(self, path):
file_path = self.build_path(path)
content = "".join(open(file_path).readlines())
if path.endswith('html'):
content = BeautifulSoup(content, 'html.parser')
return content
def build_path(self, path):
if path.startswith('/'):
path = path[1:]
return os.path.join(self.output_path, path)
def exists(self, path):
try:
open(self.build_path(path)).close()
return True
except FileNotFoundError:
return False
class TestCase(unittest.TestCase):
client = TestClient()
def assertTitle(self, content, title):
self.assertIn(title, content.title.string)

0
tests/__init__.py Normal file
View file

18
tests/test_client.py Normal file
View file

@ -0,0 +1,18 @@
from tests.TestWrapper import TestCase
from bs4 import BeautifulSoup
class TestClientTestCase(TestCase):
def test_client_fails(self):
with self.assertRaises(FileNotFoundError):
self.client.get('foo.bar')
def test_client_gets_data(self):
content = self.client.get('index.html')
self.assertIsInstance(content, BeautifulSoup)
def test_file_exists(self):
self.assertTrue(self.client.exists('index.html'))
def test_file_doesnt_exist(self):
self.assertFalse(self.client.exists('foo.bar'))

48
tests/tests_core.py Normal file
View file

@ -0,0 +1,48 @@
from tests.TestWrapper import TestCase
from config import settings
class CorePagesTestCase(TestCase):
def test_has_index(self):
content = self.client.get('index.html')
self.assertTitle(content, 'Homepage')
def test_has_robots(self):
content = self.client.get('robots.txt')
self.assertIn('Allow: /', content)
def test_has_sitemap(self):
content = self.client.get('sitemap.xml')
self.assertIn(settings.url, content)
def test_has_atom_feed(self):
content = self.client.get('feed.atom')
self.assertIn(settings.url, content)
def test_has_404_page(self):
content = self.client.get('.404.html')
self.assertTitle(content, '404')
def test_has_scripts(self):
content = self.client.get('index.html')
for script in content.find_all('script'):
self.client.exists(script.attrs['src'])
def test_has_stylesheet(self):
content = self.client.get('index.html')
for script in content.find_all('link', rel='stylesheet'):
self.assertTrue(self.client.exists(script.attrs['href']))
def test_has_link_icons(self):
content = self.client.get('index.html')
for script in content.find_all('link', rel='icon'):
self.assertTrue(self.client.exists(script.attrs['href']))
for script in content.find_all('link', rel='apple-touch-icon-precomposed'):
self.assertTrue(self.client.exists(script.attrs['href']))
def test_footer_links(self):
content = self.client.get('index.html')
footer = content.footer
for link in footer.find('p', class_="social").find_all('a'):
self.assertIn(link.attrs['alt'], settings.footer_accounts)
self.assertIn("fa fa-", str(list(link.children)[0]))

12
tests/tests_homepage.py Normal file
View file

@ -0,0 +1,12 @@
from tests.TestWrapper import TestCase
import os.path
class HomepageTestCase(TestCase):
def test_blog_links(self):
content = self.client.get('index.html')
blogs = content.find('section', id='blog').find_all('div', class_="col-xs-12")
self.assertTrue(len(blogs) <= 4)
for post in content.find('section', id='blog').find_all('div', class_="col-xs-12"):
url = os.path.join(post.find('a').attrs['href'], 'index.html')
self.assertTrue(self.client.exists(url))

View file

@ -4,7 +4,7 @@
<div class="col-xs-12"> <div class="col-xs-12">
<p class="social"> <p class="social">
{% for link in FOOTER_LINKS %} {% for link in FOOTER_LINKS %}
<a href="{{ link.url }}"><i class="fa {{ link.icon }}"></i></a> <a href="{{ link.url }}" alt="{{ link.key }}" class="footer-link"><i class="fa {{ link.icon }}"></i></a>
{% endfor %} {% endfor %}
</p> </p>
</div> </div>