--- a/tests/test_viewer.py Wed Jun 29 19:30:39 2016 +0800
+++ b/tests/test_viewer.py Thu Jun 30 20:21:30 2016 +0800
+from pytest import raises
+from tornado.web import HTTPError
from tornado.testing import AsyncHTTPTestCase
from candolint.models import database, Project, Change, Check
-from viewer import CandolintViewer
+from viewer import get_project_or_404, CandolintViewer
database.connect = Mock()
+ with database.transaction():
+ url='https://example.com/alice/hello-world',
+def test_get_project_or_404():
+ with raises(HTTPError) as error:
+ get_project_or_404('butt.cloud', 'cyber', 'wizard-attack')
+ assert error.value.status_code == 404
+ project = get_project_or_404('example.com', 'alice', 'hello-world')
+ assert project.id is not None
+ assert project.url == 'https://example.com/alice/hello-world'
class ViewerTestCase(AsyncHTTPTestCase):
--- a/viewer.py Wed Jun 29 19:30:39 2016 +0800
+++ b/viewer.py Thu Jun 30 20:21:30 2016 +0800
+def get_project_or_404(domain, user, name):
+ pq = Project.select().where(
+ Project.domain == domain,
+ Project.user == (user if user != '-' else None),
class BaseHandler(RequestHandler):
class ProjectHandler(BaseHandler):
def get(self, domain, user, name):
- pq = Project.select().where(
- Project.domain == domain,
- Project.user == (user if user != '-' else None),
- project = get_or_404(pq)
+ project = get_project_or_404(domain, user, name)
class CheckHandler(BaseHandler):
def get(self, domain, user, name, check_num):
- pq = Project.select().where(
- Project.domain == domain,
- Project.user == (user if user != '-' else None),
- project = get_or_404(pq)
+ project = get_project_or_404(domain, user, name)
if check_num == 'latest':
check = get_or_404(Check, project=project)
class StatusHandler(BaseHandler):
def get(self, domain, user, name):
- pq = Project.select().where(
- Project.domain == domain,
- Project.user == (user if user != '-' else None),
- project = get_or_404(pq)
+ project = get_project_or_404(domain, user, name)
check = get_or_404(Check, project=project)
parts = [('#555', 30, 14.5, 'lint')]