2015-08-02 00:50:19 +00:00
|
|
|
# coding=utf-8
|
2015-06-26 07:59:53 +00:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-08-09 12:03:22 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from rest_framework.test import APITestCase, APIClient
|
|
|
|
|
|
|
|
from account.models import User, SUPER_ADMIN
|
|
|
|
from problem.models import Problem, ProblemTag
|
|
|
|
|
2015-08-02 00:50:19 +00:00
|
|
|
|
|
|
|
class ProblemPageTest(TestCase):
|
|
|
|
pass
|
2015-08-09 12:03:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProblemAdminTest(APITestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.url = reverse("problem_admin_api")
|
|
|
|
user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
|
|
|
|
user.set_password("testaa")
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
# 以下是发布题目的测试
|
|
|
|
def test_invalid_format(self):
|
|
|
|
self.client.login(username="test", password="testaa")
|
|
|
|
data = {"title": "test1"}
|
|
|
|
response = self.client.post(self.url, data=data)
|
|
|
|
self.assertEqual(response.data["code"], 1)
|
|
|
|
|
|
|
|
def test_success_problem(self):
|
|
|
|
self.client.login(username="test", password="testaa")
|
2015-08-10 13:44:19 +00:00
|
|
|
ProblemTag.objects.create(name="tag1")
|
2015-08-09 12:58:08 +00:00
|
|
|
data = {"title": "title1",
|
|
|
|
"description": "des1",
|
|
|
|
"test_case_id": "1",
|
|
|
|
"source": "source1",
|
2015-08-10 14:55:22 +00:00
|
|
|
"samples": [{"input": "1 1", "output": "2"}],
|
2015-08-09 12:58:08 +00:00
|
|
|
"time_limit": "100",
|
|
|
|
"memory_limit": "1000",
|
|
|
|
"difficulty": "1",
|
|
|
|
"hint": "hint1",
|
|
|
|
"tags": [1]}
|
2015-08-09 12:03:22 +00:00
|
|
|
response = self.client.post(self.url, data=data)
|
|
|
|
self.assertEqual(response.data["code"], 0)
|
|
|
|
|
|
|
|
# 以下是编辑题目的测试
|
|
|
|
def test_put_invalid_data(self):
|
|
|
|
self.client.login(username="test", password="testaa")
|
|
|
|
data = {"title": "test0"}
|
|
|
|
response = self.client.put(self.url, data=data)
|
|
|
|
self.assertEqual(response.data["code"], 1)
|
|
|
|
|
|
|
|
def test_problem_does_not_exist(self):
|
|
|
|
self.client.login(username="test", password="testaa")
|
2015-08-10 13:44:19 +00:00
|
|
|
ProblemTag.objects.create(name="tag1")
|
2015-08-09 12:03:22 +00:00
|
|
|
tags = ProblemTag.objects.filter(id__in=[1])
|
2015-08-09 12:58:08 +00:00
|
|
|
problem = Problem.objects.create(title="title1",
|
|
|
|
description="des1",
|
|
|
|
test_case_id="1",
|
|
|
|
source="source1",
|
2015-08-10 14:55:22 +00:00
|
|
|
samples=[{"input": "1 1", "output": "2"}],
|
2015-08-09 12:58:08 +00:00
|
|
|
time_limit=100,
|
|
|
|
memory_limit=1000,
|
|
|
|
difficulty=1,
|
|
|
|
hint="hint1",
|
2015-08-09 12:03:22 +00:00
|
|
|
created_by=User.objects.get(username="test"))
|
|
|
|
problem.tags.add(*tags)
|
2015-08-09 12:58:08 +00:00
|
|
|
data = {"id": 2,
|
|
|
|
"title": "title1",
|
|
|
|
"description": "des1",
|
|
|
|
"test_case_id": "1",
|
|
|
|
"source": "source1",
|
2015-08-10 14:55:22 +00:00
|
|
|
"samples": [{"input": "1 1", "output": "2"}],
|
2015-08-09 12:58:08 +00:00
|
|
|
"time_limit": "100",
|
|
|
|
"memory_limit": "1000",
|
|
|
|
"difficulty": "1",
|
|
|
|
"hint": "hint1",
|
|
|
|
"tags": [1]}
|
2015-08-09 12:03:22 +00:00
|
|
|
response = self.client.put(self.url, data=data)
|
|
|
|
self.assertEqual(response.data, {"code": 1, "data": u"该题目不存在!"})
|
|
|
|
|
|
|
|
def test_success_edit_problem(self):
|
|
|
|
self.client.login(username="test", password="testaa")
|
|
|
|
self.client.login(username="test", password="testaa")
|
2015-08-10 13:44:19 +00:00
|
|
|
ProblemTag.objects.create(name="tag1")
|
|
|
|
ProblemTag.objects.create(name="tag2")
|
2015-08-09 12:03:22 +00:00
|
|
|
tags = ProblemTag.objects.filter(id__in=[1])
|
2015-08-09 12:58:08 +00:00
|
|
|
problem0 = Problem.objects.create(title="title1",
|
|
|
|
description="des1",
|
|
|
|
test_case_id="1",
|
|
|
|
source="source1",
|
2015-08-10 14:55:22 +00:00
|
|
|
samples=[{"input": "1 1", "output": "2"}],
|
2015-08-09 12:58:08 +00:00
|
|
|
time_limit=100,
|
|
|
|
memory_limit=1000,
|
|
|
|
difficulty=1,
|
|
|
|
hint="hint1",
|
2015-08-09 12:03:22 +00:00
|
|
|
created_by=User.objects.get(username="test"))
|
|
|
|
problem0.tags.add(*tags)
|
2015-08-09 12:58:08 +00:00
|
|
|
data = {"id": 1,
|
|
|
|
"title": "title1",
|
|
|
|
"description": "des1",
|
|
|
|
"test_case_id": "1",
|
|
|
|
"source": "source1",
|
2015-08-10 14:55:22 +00:00
|
|
|
"samples": [{"input": "1 1", "output": "2"}],
|
2015-08-09 12:58:08 +00:00
|
|
|
"time_limit": "100",
|
|
|
|
"memory_limit": "1000",
|
|
|
|
"difficulty": "1",
|
|
|
|
"hint": "hint1",
|
|
|
|
"visible": True,
|
|
|
|
"tags": [1, 2]}
|
2015-08-09 12:03:22 +00:00
|
|
|
problem = Problem.objects.get(id=data["id"])
|
|
|
|
problem.tags.remove(*problem.tags.all())
|
|
|
|
problem.tags.add(*ProblemTag.objects.filter(id__in=data["tags"]))
|
|
|
|
response = self.client.put(self.url, data=data)
|
|
|
|
self.assertEqual(response.data["code"], 0)
|