Example #1
0
    def test_can_save_duplicate_submission(self):
        s = Submission(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="")
        s.save()
        s = Submission(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="")
        s.save()

        self.assertEqual(Submission.objects.count(), 2)
Example #2
0
    def test_view_all_submissions_does_not_show_code_for_submissions(self):
        second_submission = Submission(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="time")
        second_submission.save()
        response = self.client.get(path='/challenges/{}/submissions/all'.format(self.challenge.id), HTTP_AUTHORIZATION=self.auth_token)

        self.assertEqual(response.status_code, 200)
        for submission in response.data:
            self.assertNotIn('code', submission.keys())
Example #3
0
    def test_view_all_submissions(self):
        second_submission = Submission(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="")
        second_submission.save()
        response = self.client.get(path='/challenges/{}/submissions/all'.format(self.challenge.id), HTTP_AUTHORIZATION=self.auth_token)

        self.assertEqual(response.status_code, 200)
        # Should order them by creation date descending
        self.assertEqual(LimitedSubmissionSerializer([second_submission, self.submission], many=True).data, response.data)
Example #4
0
def grade_result(submission: Submission, timed_out_percentage: int,
                 elapsed_seconds: float):
    """
    Given a tested submission and the percentage of test cases that have timed out,
        update its score in accordance to the number of test cases passed
        and if more than 40% of the test cases have timed out, set the Submission's timed_out field as true
    """
    challenge: Challenge = submission.challenge

    num_successful_tests = len([
        True for ts_cs in submission.testcase_set.all()
        if not ts_cs.pending and ts_cs.success
    ])
    result_per_test = challenge.score / challenge.test_case_count

    submission.result_score = num_successful_tests * result_per_test
    submission.pending = False
    submission.elapsed_seconds = elapsed_seconds
    if timed_out_percentage >= SUBMISSION_MINIMUM_TIMED_OUT_PERCENTAGE:
        submission.timed_out = True
    submission.save()
Example #5
0
    def create(self, request, *args, **kwargs):
        challenge_pk = kwargs.get('challenge_pk')
        code_given = request.data.get('code')
        language_given = request.data.get('language')

        challenge, language, is_valid, response = self.validate_data(
            challenge_pk, code_given, language_given, request.user)
        if not is_valid:
            return response  # invalid data

        submission = Submission(code=code_given,
                                author=request.user,
                                challenge=challenge,
                                task_id=1,
                                language=language)
        submission.save()
        celery_task = run_grader_task.delay(
            test_case_count=challenge.test_case_count,
            test_folder_name=challenge.test_file_name,
            code=code_given,
            lang=language.name,
            submission_id=submission.id)

        request.user.last_submit_at = timezone.now()
        request.user.save()

        submission.task_id = celery_task
        submission.save()

        # Create the test cases
        TestCase.objects.bulk_create([
            TestCase(submission=submission)
            for _ in range(challenge.test_case_count)
        ])

        return Response(data=SubmissionSerializer(submission).data, status=201)
Example #6
0
    def test_absolute_url(self):
        s = Submission(language=self.python_language, challenge=self.challenge, author=self.auth_user, code="")
        s.save()

        self.assertEqual(s.get_absolute_url(), '/challenges/{}/submissions/{}'.format(s.challenge_id, s.id))