2017-01-25 15:01:33 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
from zipfile import ZipFile
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
2017-01-24 05:22:40 +00:00
|
|
|
from utils.api.tests import APITestCase
|
|
|
|
|
|
|
|
from .models import ProblemTag
|
2017-01-25 08:29:00 +00:00
|
|
|
from .views.admin import TestCaseUploadAPI
|
2017-01-24 05:22:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProblemTagListAPITest(APITestCase):
|
|
|
|
def test_get_tag_list(self):
|
|
|
|
ProblemTag.objects.create(name="name1")
|
|
|
|
ProblemTag.objects.create(name="name2")
|
|
|
|
resp = self.client.get(self.reverse("problem_tag_list_api"))
|
|
|
|
self.assertSuccess(resp)
|
|
|
|
self.assertEqual(resp.data["data"], ["name1", "name2"])
|
2017-01-25 08:29:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestCaseUploadAPITest(APITestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.api = TestCaseUploadAPI()
|
2017-01-25 15:01:33 +00:00
|
|
|
self.url = self.reverse("test_case_upload_api")
|
|
|
|
self.create_super_admin()
|
2017-01-25 08:29:00 +00:00
|
|
|
|
|
|
|
def test_filter_file_name(self):
|
|
|
|
self.assertEqual(self.api.filter_name_list(["1.in", "1.out", "2.in", ".DS_Store"], spj=False), ["1.in", "1.out"])
|
|
|
|
self.assertEqual(self.api.filter_name_list(["2.in", "2.out"], spj=False), [])
|
|
|
|
|
|
|
|
self.assertEqual(self.api.filter_name_list(["1.in", "1.out", "2.in"], spj=True), ["1.in", "2.in"])
|
|
|
|
self.assertEqual(self.api.filter_name_list(["2.in", "3.in"], spj=True), [])
|
2017-01-25 15:01:33 +00:00
|
|
|
|
|
|
|
def make_test_case_zip(self):
|
|
|
|
base_dir = os.path.join("/tmp", "test_case")
|
|
|
|
shutil.rmtree(base_dir, ignore_errors=True)
|
|
|
|
os.mkdir(base_dir)
|
|
|
|
file_names = ["1.in", "1.out", "2.in", ".DS_Store"]
|
|
|
|
for item in file_names:
|
|
|
|
with open(os.path.join(base_dir, item), "w", encoding="utf-8") as f:
|
|
|
|
f.write(item + "\n" + item + "\r\n" + "end")
|
|
|
|
zip_file = os.path.join(base_dir, "test_case.zip")
|
|
|
|
with ZipFile(os.path.join(base_dir, "test_case.zip"), "w") as f:
|
|
|
|
for item in file_names:
|
|
|
|
f.write(os.path.join(base_dir, item), item)
|
|
|
|
return zip_file
|
|
|
|
|
|
|
|
def test_upload_spj_test_case_zip(self):
|
|
|
|
with open(self.make_test_case_zip(), "rb") as f:
|
|
|
|
resp = self.client.post(self.url,
|
|
|
|
data={"spj": "true", "file": f}, format="multipart")
|
|
|
|
self.assertSuccess(resp)
|
|
|
|
data = resp.data["data"]
|
|
|
|
self.assertEqual(data["spj"], True)
|
|
|
|
test_case_dir = os.path.join(settings.TEST_CASE_DIR, data["id"])
|
|
|
|
self.assertTrue(os.path.exists(test_case_dir))
|
|
|
|
for item in data["info"]:
|
|
|
|
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")
|
|
|
|
|
|
|
|
def test_upload_test_case_zip(self):
|
|
|
|
with open(self.make_test_case_zip(), "rb") as f:
|
|
|
|
resp = self.client.post(self.url,
|
|
|
|
data={"spj": "false", "file": f}, format="multipart")
|
|
|
|
self.assertSuccess(resp)
|
|
|
|
data = resp.data["data"]
|
|
|
|
self.assertEqual(data["spj"], False)
|
|
|
|
test_case_dir = os.path.join(settings.TEST_CASE_DIR, data["id"])
|
|
|
|
self.assertTrue(os.path.exists(test_case_dir))
|
|
|
|
for item in data["info"]:
|
|
|
|
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")
|