|
1 | 1 | import copy |
2 | 2 | from datetime import datetime, timedelta |
3 | 3 |
|
| 4 | +from django.conf import settings |
4 | 5 | from django.utils import timezone |
5 | 6 |
|
6 | 7 | from utils.api.tests import APITestCase |
@@ -181,3 +182,111 @@ def setUp(self): |
181 | 182 | def get_contest_rank(self): |
182 | 183 | resp = self.client.get(self.url + "?contest_id=" + self.acm_contest.id) |
183 | 184 | self.assertSuccess(resp) |
| 185 | + |
| 186 | + |
| 187 | +class ContestParticipantsAPITest(APITestCase): |
| 188 | + |
| 189 | + def setUp(self): |
| 190 | + from problem.tests import DEFAULT_PROBLEM_DATA, ProblemCreateTestBase |
| 191 | + |
| 192 | + self.create_school_fixtures(college_id=1, college_name="Test", department_id=1, department_name="Test") |
| 193 | + self.admin = self.create_admin() |
| 194 | + self.user = self.create_user(email="test@test.com", username="test", password="test1234!", login=False) |
| 195 | + self.contest = Contest.objects.create(created_by=self.admin, **DEFAULT_CONTEST_DATA) |
| 196 | + self.problem = ProblemCreateTestBase.add_problem(DEFAULT_PROBLEM_DATA, self.admin) |
| 197 | + self.problem.contest_id = self.contest.id |
| 198 | + self.problem.save() |
| 199 | + self.url = self.reverse("contest_participants_api") |
| 200 | + |
| 201 | + def test_participants_are_grouped_by_user_id(self): |
| 202 | + from submission.models import JudgeStatus, Submission |
| 203 | + |
| 204 | + Submission.objects.create( |
| 205 | + user_id=self.user.id, |
| 206 | + username="oldname", |
| 207 | + language="C++", |
| 208 | + code="test code", |
| 209 | + problem_id=self.problem.id, |
| 210 | + ip="127.0.0.1", |
| 211 | + contest_id=self.contest.id, |
| 212 | + result=JudgeStatus.PENDING, |
| 213 | + statistic_info={"time_cost": "100", "memory_cost": "1024"}, |
| 214 | + shared=False, |
| 215 | + first_failed_tc_idx=None, |
| 216 | + ) |
| 217 | + Submission.objects.create( |
| 218 | + user_id=self.user.id, |
| 219 | + username="newname", |
| 220 | + language="C++", |
| 221 | + code="test code", |
| 222 | + problem_id=self.problem.id, |
| 223 | + ip="127.0.0.1", |
| 224 | + contest_id=self.contest.id, |
| 225 | + result=JudgeStatus.PENDING, |
| 226 | + statistic_info={"time_cost": "100", "memory_cost": "1024"}, |
| 227 | + shared=False, |
| 228 | + first_failed_tc_idx=None, |
| 229 | + ) |
| 230 | + |
| 231 | + resp = self.client.get(f"{self.url}?contest_id={self.contest.id}") |
| 232 | + self.assertSuccess(resp) |
| 233 | + |
| 234 | + participants = resp.data["data"] |
| 235 | + self.assertEqual(len(participants), 1) |
| 236 | + self.assertEqual(participants[0]["user_id"], self.user.id) |
| 237 | + self.assertEqual(participants[0]["username"], self.user.username) |
| 238 | + self.assertEqual(participants[0]["submission_count"], 2) |
| 239 | + |
| 240 | + def test_participants_requires_contest_id(self): |
| 241 | + resp = self.client.get(self.url) |
| 242 | + self.assertFailed(resp, "Invalid parameter, contest_id is required") |
| 243 | + |
| 244 | + def test_participants_rejects_invalid_contest_id(self): |
| 245 | + resp = self.client.get(f"{self.url}?contest_id=abc") |
| 246 | + self.assertFailed(resp, "Invalid parameter, contest_id is required") |
| 247 | + |
| 248 | + def test_participants_fallback_to_submission_username_when_user_is_missing(self): |
| 249 | + from submission.models import JudgeStatus, Submission |
| 250 | + |
| 251 | + older_submission = Submission.objects.create( |
| 252 | + user_id=self.user.id, |
| 253 | + username="zzz_user", |
| 254 | + language="C++", |
| 255 | + code="test code", |
| 256 | + problem_id=self.problem.id, |
| 257 | + ip="9.9.9.9", |
| 258 | + contest_id=self.contest.id, |
| 259 | + result=JudgeStatus.PENDING, |
| 260 | + statistic_info={"time_cost": "100", "memory_cost": "1024"}, |
| 261 | + shared=False, |
| 262 | + first_failed_tc_idx=None, |
| 263 | + ) |
| 264 | + latest_submission = Submission.objects.create( |
| 265 | + user_id=self.user.id, |
| 266 | + username="aaa_user", |
| 267 | + language="C++", |
| 268 | + code="test code", |
| 269 | + problem_id=self.problem.id, |
| 270 | + ip="1.1.1.1", |
| 271 | + contest_id=self.contest.id, |
| 272 | + result=JudgeStatus.PENDING, |
| 273 | + statistic_info={"time_cost": "100", "memory_cost": "1024"}, |
| 274 | + shared=False, |
| 275 | + first_failed_tc_idx=None, |
| 276 | + ) |
| 277 | + Submission.objects.filter(id=older_submission.id).update(create_time=timezone.now() - timedelta(minutes=1)) |
| 278 | + Submission.objects.filter(id=latest_submission.id).update(create_time=timezone.now()) |
| 279 | + |
| 280 | + self.user.delete() |
| 281 | + |
| 282 | + resp = self.client.get(f"{self.url}?contest_id={self.contest.id}") |
| 283 | + self.assertSuccess(resp) |
| 284 | + |
| 285 | + participants = resp.data["data"] |
| 286 | + self.assertEqual(len(participants), 1) |
| 287 | + self.assertEqual(participants[0]["username"], "aaa_user") |
| 288 | + self.assertEqual(participants[0]["email"], "") |
| 289 | + self.assertEqual(participants[0]["avatar"], f"{settings.AVATAR_URI_PREFIX}/default.png") |
| 290 | + self.assertEqual(participants[0]["school"], "") |
| 291 | + self.assertEqual(participants[0]["major"], "") |
| 292 | + self.assertEqual(participants[0]["last_submission_ip"], "1.1.1.1") |
0 commit comments