From 1d02b3d450eef7086b7211c622828f17817c358e Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 30 Sep 2016 23:13:41 +0100 Subject: [PATCH] refactor tests --- tests/TestWrapper.py | 33 --------------------- tests/__init__.py | 33 +++++++++++++++++++++ tests/test_client.py | 2 +- tests/{tests_core.py => tests_common.py} | 2 +- tests/{tests_homepage.py => tests_pages.py} | 11 +++++-- 5 files changed, 44 insertions(+), 37 deletions(-) delete mode 100644 tests/TestWrapper.py rename tests/{tests_core.py => tests_common.py} (97%) rename tests/{tests_homepage.py => tests_pages.py} (50%) diff --git a/tests/TestWrapper.py b/tests/TestWrapper.py deleted file mode 100644 index 59854d9..0000000 --- a/tests/TestWrapper.py +++ /dev/null @@ -1,33 +0,0 @@ -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) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..49c4b3c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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: + with open(self.build_path(path)): + return True + except FileNotFoundError: + return False + + +class TestCase(unittest.TestCase): + client = TestClient() + + def assertTitle(self, content, title): + self.assertIn(title, content.title.string) diff --git a/tests/test_client.py b/tests/test_client.py index 718be4d..6e62675 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,4 +1,4 @@ -from tests.TestWrapper import TestCase +from tests import TestCase from bs4 import BeautifulSoup diff --git a/tests/tests_core.py b/tests/tests_common.py similarity index 97% rename from tests/tests_core.py rename to tests/tests_common.py index c607ea5..0229c61 100644 --- a/tests/tests_core.py +++ b/tests/tests_common.py @@ -1,4 +1,4 @@ -from tests.TestWrapper import TestCase +from tests import TestCase from config import settings diff --git a/tests/tests_homepage.py b/tests/tests_pages.py similarity index 50% rename from tests/tests_homepage.py rename to tests/tests_pages.py index 174de86..3c28e0b 100644 --- a/tests/tests_homepage.py +++ b/tests/tests_pages.py @@ -1,4 +1,4 @@ -from tests.TestWrapper import TestCase +from tests import TestCase import os.path @@ -7,6 +7,13 @@ class HomepageTestCase(TestCase): 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"): + for post in blogs: url = os.path.join(post.find('a').attrs['href'], 'index.html') self.assertTrue(self.client.exists(url)) + + def test_projects(self): + content = self.client.get('index.html') + projects = content.find('section', id='projects').find_all('a', class_='portfolio-box') + for project in projects: + url = os.path.join(project.attrs['href'], 'index.html') + self.assertTrue(self.client.exists(url))