1
Fork 0
theorangeone.net-legacy/tests/__init__.py

47 lines
1.4 KiB
Python
Raw Normal View History

2017-05-21 22:12:40 +01:00
import unittest
import os.path
from bs4 import BeautifulSoup
class TestClient:
2017-05-21 22:17:38 +01:00
output_path = os.path.realpath('./public')
2017-05-21 22:12:40 +01:00
def get(self, path, JS=True):
file_path = self.build_path(path)
content = "".join(open(file_path).readlines())
if file_path.endswith('html'):
content = BeautifulSoup(content, 'html.parser')
if JS:
for script in content(["noscript"]): # Remove noscript tags
script.extract()
return content
def build_path(self, path):
2017-05-22 09:40:47 +01:00
if path.startswith('https://theorangeone.net'):
path = path.replace('https://theorangeone.net', '')
2017-05-21 22:12:40 +01:00
if path.startswith('/'):
path = path[1:]
if path.endswith('/'):
path += 'index.html'
return os.path.join(self.output_path, path)
def exists(self, path):
return os.path.exists(self.build_path(path))
class TestCase(unittest.TestCase):
client = TestClient()
def get_children(self, content):
return str(list(content.children)[0]).strip()
def assertTitle(self, content, title):
self.assertIn(title, content.title.string)
def assertHeaderTitle(self, content, title):
2017-05-21 22:17:38 +01:00
header_title = content.find('header').find('h2')
2017-05-21 22:12:40 +01:00
self.assertIn(title, self.get_children(header_title))
def assertSamePath(self, p1, p2):
self.assertEqual(self.client.build_path(p1), self.client.build_path(p2))