mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2025-01-01 10:02:01 +00:00
Merge pull request #68 from zhanghedr/2.0-dev
Add contest tests and fix problem admin filter bug
This commit is contained in:
commit
97a9433420
107
contest/tests.py
107
contest/tests.py
@ -0,0 +1,107 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import copy
|
||||
from django.utils import timezone
|
||||
|
||||
from utils.api._serializers import DateTimeTZField
|
||||
from utils.api.tests import APITestCase
|
||||
from .models import ContestAnnouncement
|
||||
from .models import ContestRuleType
|
||||
|
||||
DEFAULT_CONTEST_DATA = {"title": "test title", "description": "test description",
|
||||
"start_time": timezone.localtime(timezone.now()),
|
||||
"end_time": timezone.localtime(timezone.now()) + timedelta(days=1),
|
||||
"rule_type": ContestRuleType.ACM,
|
||||
"password": "123",
|
||||
"visible": True, "real_time_rank": True}
|
||||
|
||||
|
||||
class ContestAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
self.create_super_admin()
|
||||
self.url = self.reverse("contest_api")
|
||||
self.data = DEFAULT_CONTEST_DATA
|
||||
|
||||
def test_create_contest(self):
|
||||
response = self.client.post(self.url, data=self.data)
|
||||
self.assertSuccess(response)
|
||||
return response
|
||||
|
||||
def test_update_contest(self):
|
||||
id = self.test_create_contest().data["data"]["id"]
|
||||
update_data = {"id": id, "title": "update title",
|
||||
"description": "update description",
|
||||
"password": "12345",
|
||||
"visible": False, "real_time_rank": False}
|
||||
data = copy.deepcopy(self.data)
|
||||
data.update(update_data)
|
||||
response = self.client.put(self.url, data=data)
|
||||
self.assertSuccess(response)
|
||||
response_data = response.data["data"]
|
||||
datetime_tz_field = DateTimeTZField()
|
||||
for k in data.keys():
|
||||
if isinstance(data[k], datetime):
|
||||
data[k] = datetime_tz_field.to_representation(data[k])
|
||||
self.assertEqual(response_data[k], data[k])
|
||||
|
||||
def test_get_contests(self):
|
||||
self.test_create_contest()
|
||||
response = self.client.get(self.url)
|
||||
self.assertSuccess(response)
|
||||
|
||||
def test_get_one_contest(self):
|
||||
id = self.test_create_contest().data["data"]["id"]
|
||||
response = self.client.get("{}?id={}".format(self.url, id))
|
||||
self.assertSuccess(response)
|
||||
|
||||
|
||||
class ContestAnnouncementAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
self.create_super_admin()
|
||||
self.url = self.reverse("contest_announcement_admin_api")
|
||||
contest_id = self.create_contest().data["data"]["id"]
|
||||
self.data = {"title": "test title", "content": "test content", "contest_id": contest_id}
|
||||
|
||||
def create_contest(self):
|
||||
url = self.reverse("contest_api")
|
||||
data = DEFAULT_CONTEST_DATA
|
||||
return self.client.post(url, data=data)
|
||||
|
||||
def test_create_contest_announcement(self):
|
||||
response = self.client.post(self.url, data=self.data)
|
||||
self.assertSuccess(response)
|
||||
return response
|
||||
|
||||
def test_delete_contest_announcement(self):
|
||||
id = self.test_create_contest_announcement().data["data"]["id"]
|
||||
response = self.client.delete("{}?id={}".format(self.url, id))
|
||||
self.assertSuccess(response)
|
||||
self.assertFalse(ContestAnnouncement.objects.filter(id=id).exists())
|
||||
|
||||
def test_get_contest_announcements(self):
|
||||
self.test_create_contest_announcement()
|
||||
response = self.client.get(self.url)
|
||||
self.assertSuccess(response)
|
||||
|
||||
def test_get_one_contest_announcement(self):
|
||||
id = self.test_create_contest_announcement().data["data"]["id"]
|
||||
response = self.client.get("{}?id={}".format(self.url, id))
|
||||
self.assertSuccess(response)
|
||||
|
||||
|
||||
class ContestAnnouncementListAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
self.create_super_admin()
|
||||
self.url = self.reverse("contest_list_api")
|
||||
|
||||
def create_contest_announcements(self):
|
||||
contest_id = self.client.post(self.reverse("contest_api"), data=DEFAULT_CONTEST_DATA).data["data"]["id"]
|
||||
url = self.reverse("contest_announcement_admin_api")
|
||||
self.client.post(url, data={"title": "test title1", "content": "test content1", "contest_id": contest_id})
|
||||
self.client.post(url, data={"title": "test title2", "content": "test content2", "contest_id": contest_id})
|
||||
return contest_id
|
||||
|
||||
def test_get_contest_announcement_list(self):
|
||||
contest_id = self.create_contest_announcements()
|
||||
response = self.client.get(self.url, data={"contest_id": contest_id})
|
||||
self.assertSuccess(response)
|
@ -1,7 +1,6 @@
|
||||
import dateutil.parser
|
||||
|
||||
from utils.api import APIView, validate_serializer
|
||||
|
||||
from ..models import Contest, ContestAnnouncement
|
||||
from ..serializers import (ContestAnnouncementSerializer, ContestSerializer,
|
||||
CreateConetestSeriaizer,
|
||||
@ -20,8 +19,8 @@ class ContestAPI(APIView):
|
||||
return self.error("Start time must occur earlier than end time")
|
||||
if not data["password"]:
|
||||
data["password"] = None
|
||||
Contest.objects.create(**data)
|
||||
return self.success()
|
||||
contest = Contest.objects.create(**data)
|
||||
return self.success(ContestSerializer(contest).data)
|
||||
|
||||
@validate_serializer(EditConetestSeriaizer)
|
||||
def put(self, request):
|
||||
@ -90,7 +89,8 @@ class ContestAnnouncementAPI(APIView):
|
||||
contest_announcement_id = request.GET.get("id")
|
||||
if contest_announcement_id:
|
||||
if request.user.is_admin():
|
||||
ContestAnnouncement.objects.filter(id=contest_announcement_id, contest__created_by=request.user).delete()
|
||||
ContestAnnouncement.objects.filter(id=contest_announcement_id,
|
||||
contest__created_by=request.user).delete()
|
||||
else:
|
||||
ContestAnnouncement.objects.filter(id=contest_announcement_id).delete()
|
||||
return self.success()
|
||||
|
@ -63,7 +63,7 @@ class FPSParser(object):
|
||||
if not lang:
|
||||
raise ValueError("Invalid " + tag + ", language name is missed")
|
||||
problem[tag].append({"language": lang, "code": item.text})
|
||||
elif tag == 'spj':
|
||||
elif tag == "spj":
|
||||
lang = item.attrib.get("language")
|
||||
if not lang:
|
||||
raise ValueError("Invalid spj, language name if missed")
|
||||
|
@ -5,5 +5,5 @@ from ..views.admin import ProblemAPI, TestCaseUploadAPI, ContestProblemAPI
|
||||
urlpatterns = [
|
||||
url(r"^test_case/upload$", TestCaseUploadAPI.as_view(), name="test_case_upload_api"),
|
||||
url(r"^problem$", ProblemAPI.as_view(), name="problem_api"),
|
||||
url(r'contest/problem$', ContestProblemAPI.as_view(), name="contest_problem_api")
|
||||
url(r"^contest/problem$", ContestProblemAPI.as_view(), name="contest_problem_api")
|
||||
]
|
||||
|
@ -162,15 +162,15 @@ class ProblemAPI(APIView):
|
||||
if problem_id:
|
||||
try:
|
||||
problem = Problem.objects.get(id=problem_id)
|
||||
if not user.can_mgmt_all_problem():
|
||||
problem = problem.get(created_by=request.user)
|
||||
if not user.can_mgmt_all_problem() and problem.created_by != user:
|
||||
return self.error("Problem does not exist")
|
||||
return self.success(ProblemSerializer(problem).data)
|
||||
except Problem.DoesNotExist:
|
||||
return self.error("Problem does not exist")
|
||||
|
||||
problems = Problem.objects.all().order_by("-create_time")
|
||||
if not user.can_mgmt_all_problem():
|
||||
problems = problems.filter(created_by=request.user)
|
||||
problems = problems.filter(created_by=user)
|
||||
keyword = request.GET.get("keyword")
|
||||
if keyword:
|
||||
problems = problems.filter(title__contains=keyword)
|
||||
@ -185,8 +185,8 @@ class ProblemAPI(APIView):
|
||||
|
||||
try:
|
||||
problem = Problem.objects.get(id=problem_id)
|
||||
if not user.can_mgmt_all_problem():
|
||||
problem = problem.get(created_by=request.user)
|
||||
if not user.can_mgmt_all_problem() and problem.created_by != user:
|
||||
return self.error("Problem does not exist")
|
||||
except Problem.DoesNotExist:
|
||||
return self.error("Problem does not exist")
|
||||
|
||||
@ -290,7 +290,7 @@ class ContestProblemAPI(APIView):
|
||||
if problem_id:
|
||||
try:
|
||||
problem = ContestProblem.objects.get(id=problem_id)
|
||||
if request.user.is_admin() and problem.contest.created_by != user:
|
||||
if user.is_admin() and problem.contest.created_by != user:
|
||||
return self.error("Problem does not exist")
|
||||
except ContestProblem.DoesNotExist:
|
||||
return self.error("Problem does not exist")
|
||||
|
Loading…
Reference in New Issue
Block a user