Download:
child 79:efeafb44010c
parent 77:b9b80e8fff5f
78:06a1f6536049
Anton Shestakov <av6@dwimlabs.net>, Thu, 30 Jun 2016 20:21:30 +0800
viewer: a new helper for project/check/status handlers, get_project_or_404() Also test it.

2 файлов изменено, 31 вставок(+), 16 удалений(-) [+]
tests/test_viewer.py file | annotate | diff | comparison | revisions
viewer.py file | annotate | diff | comparison | revisions
--- 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
@@ -1,8 +1,10 @@
from mock import Mock
+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
def setup_module():
@@ -13,6 +15,23 @@
database.connect = Mock()
database.close = Mock()
+ with database.transaction():
+ Project.create(
+ url='https://example.com/alice/hello-world',
+ domain='example.com',
+ user='alice',
+ name='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):
def get_app(self):
--- a/viewer.py Wed Jun 29 19:30:39 2016 +0800
+++ b/viewer.py Thu Jun 30 20:21:30 2016 +0800
@@ -32,6 +32,14 @@
raise HTTPError(404)
+def get_project_or_404(domain, user, name):
+ pq = Project.select().where(
+ Project.domain == domain,
+ Project.user == (user if user != '-' else None),
+ Project.name == name)
+ return get_or_404(pq)
+
+
class BaseHandler(RequestHandler):
def prepare(self):
database.connect()
@@ -70,11 +78,7 @@
class ProjectHandler(BaseHandler):
def get(self, domain, user, name):
- pq = Project.select().where(
- Project.domain == domain,
- Project.user == (user if user != '-' else None),
- Project.name == name)
- project = get_or_404(pq)
+ project = get_project_or_404(domain, user, name)
checks = (Check
.select(Check, Change)
.join(Change)
@@ -85,11 +89,7 @@
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.name == name)
- project = get_or_404(pq)
+ project = get_project_or_404(domain, user, name)
if check_num == 'latest':
check = get_or_404(Check, project=project)
else:
@@ -100,11 +100,7 @@
class StatusHandler(BaseHandler):
def get(self, domain, user, name):
- pq = Project.select().where(
- Project.domain == domain,
- Project.user == (user if user != '-' else None),
- Project.name == name)
- 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')]
if not check.success: