add problem related tests

This commit is contained in:
virusdefender 2017-02-10 15:13:03 +08:00
parent 29aa457a2d
commit f58fd53557
5 changed files with 90 additions and 4 deletions

View File

@ -39,7 +39,7 @@ class CreateOrEditProblemSerializer(serializers.Serializer):
input_description = serializers.CharField()
output_description = serializers.CharField()
samples = serializers.ListField(child=CreateSampleSerializer(), allow_empty=False)
test_case_id = serializers.CharField(max_length=32)
test_case_id = serializers.CharField(min_length=32, max_length=32)
test_case_score = serializers.ListField(child=CreateTestCaseScoreSerializer(), allow_empty=False)
time_limit = serializers.IntegerField(min_value=1, max_value=1000 * 60)
memory_limit = serializers.IntegerField(min_value=1, max_value=1024)

View File

@ -1,3 +1,4 @@
import copy
import os
import shutil
from zipfile import ZipFile
@ -5,7 +6,6 @@ from zipfile import ZipFile
from django.conf import settings
from utils.api.tests import APITestCase
from .models import ProblemTag
from .views.admin import TestCaseUploadAPI
@ -73,3 +73,57 @@ class TestCaseUploadAPITest(APITestCase):
name = item["input_name"]
with open(os.path.join(test_case_dir, name), "r", encoding="utf-8") as f:
self.assertEqual(f.read(), name + "\n" + name + "\n" + "end")
class ProblemAPITest(APITestCase):
def setUp(self):
self.url = self.reverse("problem_api")
self.create_super_admin()
self.data = {"_id": "A-110", "title": "test", "description": "<p>test</p>", "input_description": "test",
"output_description": "test", "time_limit": 1000, "memory_limit": 256, "difficulty": "Low",
"visible": True, "tags": ["test"], "languages": ["C", "C++", "Java", "Python2"], "template": {},
"samples": [{"input": "test", "output": "test"}], "spj": False, "spj_language": "C",
"spj_code": "", "test_case_id": "499b26290cc7994e0b497212e842ea85",
"test_case_score": [{"output_name": "1.out", "input_name": "1.in", "output_size": 0,
"stripped_output_md5": "d41d8cd98f00b204e9800998ecf8427e",
"input_size": 0, "score": 0}],
"rule_type": "ACM", "hint": "<p>test</p>", "source": "test"}
def test_create_problem(self):
resp = self.client.post(self.url, data=self.data)
self.assertSuccess(resp)
return resp
def test_duplicate_display_id(self):
self.test_create_problem()
resp = self.client.post(self.url, data=self.data)
self.assertFailed(resp, "Display ID already exists")
def test_spj(self):
data = copy.deepcopy(self.data)
data["spj"] = True
resp = self.client.post(self.url, data)
self.assertFailed(resp, "Invalid spj")
data["spj_code"] = "test"
resp = self.client.post(self.url, data=data)
self.assertSuccess(resp)
def test_get_problem(self):
self.test_create_problem()
resp = self.client.get(self.url)
self.assertSuccess(resp)
def test_get_one_problem(self):
problem_id = self.test_create_problem().data["data"]["id"]
resp = self.client.get(self.url + "?id=" + str(problem_id))
self.assertSuccess(resp)
def test_edit_problem(self):
problem_id = self.test_create_problem().data["data"]["id"]
data = copy.deepcopy(self.data)
data["id"] = problem_id
resp = self.client.put(self.url, data=data)
self.assertSuccess(resp)

View File

@ -138,6 +138,8 @@ class ProblemAPI(APIView):
data["created_by"] = request.user
tags = data.pop("tags")
data["languages"] = list(data["languages"])
problem = Problem.objects.create(**data)
if not _id:
@ -150,7 +152,7 @@ class ProblemAPI(APIView):
except ProblemTag.DoesNotExist:
tag = ProblemTag.objects.create(name=item)
problem.tags.add(tag)
return self.success()
return self.success(ProblemSerializer(problem).data)
@problem_permission_required
def get(self, request):

28
run_test.py Normal file
View File

@ -0,0 +1,28 @@
import sys
import getopt
import os
opts, args = getopt.getopt(sys.argv[1:], "cm:", ["coverage=", "module="])
is_coverage = False
test_module = ""
waf_addr = "127.0.0.1:50001"
setting = "oj.settings"
for opt, arg in opts:
if opt in ['-c', '--coverage']:
is_coverage = True
if opt in ['-m', '--module']:
test_module = arg
print("Coverage: {cov}".format(cov=is_coverage))
print("Module: {mod}".format(mod=(test_module if test_module else "All")))
print("running flake8...")
if os.system("flake8 --statistics ."):
exit()
ret = os.system("coverage run ./manage.py test {module} --settings={setting}".format(module=test_module, setting=setting))
if not ret and is_coverage:
os.system("coverage html && open htmlcov/index.html")

View File

@ -32,5 +32,7 @@ class APITestCase(TestCase):
if not response.data["error"] is None:
raise AssertionError("response with errors, response: " + str(response.data))
def assertFailed(self, response):
def assertFailed(self, response, msg=None):
self.assertTrue(response.data["error"] is not None)
if msg:
self.assertEqual(response.data["data"], msg)