[后端]修改我的提交页面,去掉了冗余语句,并添加测试

This commit is contained in:
esp 2015-08-21 20:58:04 +08:00
parent 75eb1192fb
commit d2242a78c6
2 changed files with 45 additions and 9 deletions

View File

@ -1,3 +1,44 @@
from django.test import TestCase
from account.models import User, REGULAR_USER
from submission.models import Submission
from rest_framework.test import APITestCase, APIClient
# Create your tests here.
class SubmissionsListPageTest(TestCase):
def setUp(self):
self.client = APIClient()
self.user = User.objects.create(username="gogoing", admin_type=REGULAR_USER)
self.user2 = User.objects.create(username="cool", admin_type=REGULAR_USER)
self.user2.set_password("666666")
self.user.set_password("666666")
self.user.save()
# self.client.login(username="gogoing", password="666666")
self.submission = Submission.objects.create(user_id=self.user.id,
language=1,
code='#include "stdio.h"\nint main(){\n\treturn 0;\n}',
problem_id=1)
def test_visit_submissionsListPage_successfully(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/my_submissions/1/')
self.assertEqual(response.status_code, 200)
def test_visit_submissionsListPage_without_page_successfully(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/my_submissions/')
self.assertEqual(response.status_code, 200)
def test_submissionsListPage_does_not_exist(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/my_submissions/5/')
self.assertTemplateUsed(response, "utils/error.html")
def test_submissionsListPage_page_not_exist(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/my_submissions/5/')
self.assertTemplateUsed(response, "utils/error.html")
def test_submissionsListPage_have_no_submission(self):
self.client.login(username="cool", password="666666")
response = self.client.get('/my_submissions/')
self.assertEqual(response.status_code, 200)

View File

@ -106,10 +106,9 @@ class SubmissionAdminAPIView(APIView):
@login_required
def my_submission_list_page(request, page = 1):
try:
if not page:
page = 1
submissions = Submission.objects.filter(user_id=request.user.id)
except Submission.DoesNotExist:
return error_page(request, u"你还没有提交过任何问题")
paginator = Paginator(submissions, 20)
try:
current_page = paginator.page(int(page))
@ -120,7 +119,6 @@ def my_submission_list_page(request, page = 1):
previous_page = current_page.previous_page_number()
except Exception:
pass
try:
next_page = current_page.next_page_number()
except Exception:
@ -129,6 +127,3 @@ def my_submission_list_page(request, page = 1):
return render(request, "oj/submission/my_submissions_list.html",
{"submissions": current_page, "page": int(page),
"previous_page": previous_page, "next_page": next_page})
return render(request, "oj/submission/my_submissions_list.html", {"submissions": submission})