/**
  * Tests apiRankByProblemsSolved for a specific user
  */
 public function testUserRankByProblemsSolved()
 {
     // Create a user and sumbit a run with him
     $contestant = UserFactory::createUser();
     $problemData = ProblemsFactory::createProblem();
     $runData = RunsFactory::createRunToProblem($problemData, $contestant);
     RunsFactory::gradeRun($runData);
     // Call API
     $response = UserController::apiRankByProblemsSolved(new Request(array('username' => $contestant->getUsername())));
     $this->assertEquals($response['name'], $contestant->getName());
     $this->assertEquals($response['problems_solved'], 1);
 }
 /**
  * Test apiBestScore for submits in a problem for other user
  */
 public function testBestScoreInProblemOtherUser()
 {
     // Create problem
     $problemData = ProblemsFactory::createProblem();
     // Create contestant
     $contestant = UserFactory::createUser();
     // Create user who will use the API
     $user = UserFactory::createUser();
     // Create 2 runs, 100 and 50.
     $runData = RunsFactory::createRunToProblem($problemData, $contestant);
     $runDataPA = RunsFactory::createRunToProblem($problemData, $contestant);
     RunsFactory::gradeRun($runData);
     RunsFactory::gradeRun($runDataPA, 0.5, "PA");
     // Call API
     $response = ProblemController::apiBestScore(new Request(array("auth_token" => $this->login($user), "problem_alias" => $problemData["request"]["alias"], "username" => $contestant->getUsername())));
     $this->assertEquals(100.0, $response["score"]);
 }
 /**
  * Test apiBestScore for submits in a problem for other user
  */
 public function testBestScoreInProblemOtherUser()
 {
     // Create problem
     $problemData = ProblemsFactory::createProblem();
     // Create contestant
     $contestant = UserFactory::createUser();
     // Create user who will use the API
     $user = UserFactory::createUser();
     // Create 2 runs, 100 and 50.
     $runData = RunsFactory::createRunToProblem($problemData, $contestant);
     $runDataPA = RunsFactory::createRunToProblem($problemData, $contestant);
     RunsFactory::gradeRun($runData);
     RunsFactory::gradeRun($runDataPA, 0.5, 'PA');
     // Call API
     $response = ProblemController::apiBestScore(new Request(array('auth_token' => $this->login($user), 'problem_alias' => $problemData['request']['alias'], 'username' => $contestant->getUsername())));
     $this->assertEquals(100.0, $response['score']);
 }
Beispiel #4
0
 /**
  * Test 'page', 'order_by' and 'mode' parametes of the apiList() method, and search by title.
  */
 public function testProblemListPager()
 {
     // Create a user and some problems with submissions for the tests.
     $contestant = UserFactory::createUser();
     for ($i = 0; $i < 6; $i++) {
         $problemData[$i] = ProblemsFactory::createProblem(null, null, 1);
         $runs = $i / 2;
         for ($r = 0; $r < $runs; $r++) {
             $runData = RunsFactory::createRunToProblem($problemData[$i], $contestant);
             $points = rand(0, 100);
             $verdict = 'WA';
             if ($points > 0) {
                 $verdict = $points == 100 ? 'AC' : 'PA';
             }
             RunsFactory::gradeRun($runData, $points / 100, $verdict);
         }
     }
     $request = new Request();
     $request['auth_token'] = $this->login($contestant);
     $response = ProblemController::apiList($request);
     // Test search by title
     $titles = array();
     foreach ($response['results'] as $problem) {
         array_push($titles, $problem['title']);
     }
     foreach ($titles as $title) {
         $request['query'] = $title;
         $response = ProblemController::apiList($request);
         $this->assertTrue(count($response['results']) == 1);
         $this->assertTrue($title === $response['results'][0]['title']);
     }
     $request['query'] = null;
     $response = ProblemController::apiList($request);
     $total = $response['total'];
     $pages = intval(($total + PROBLEMS_PER_PAGE - 1) / PROBLEMS_PER_PAGE);
     // The following tests will try the different scenarios that can occur
     // with the additions of the three features to apiList(), that is, paging,
     // order by column and order mode: Call apiList() with and without
     // pagination, for each allowed ordering and each possible order mode.
     $modes = array('asc', 'desc');
     $columns = array('title', 'submissions', 'accepted', 'ratio', 'points', 'score');
     $counter = 0;
     for ($paging = 0; $paging <= 1; $paging++) {
         foreach ($columns as $col) {
             foreach ($modes as $mode) {
                 $first = null;
                 $last = null;
                 $request['mode'] = $mode;
                 $request['order_by'] = $col;
                 if ($paging == 1) {
                     // Clear offset and rowcount if set.
                     if (isset($request['offset'])) {
                         unset($request['offset']);
                     }
                     if (isset($request['rowcount'])) {
                         unset($request['rowcount']);
                     }
                     $request['page'] = 1;
                     $response = ProblemController::apiList($request);
                     $first = $response['results'];
                     $request['page'] = $pages;
                     $response = ProblemController::apiList($request);
                     $last = $response['results'];
                     // Test number of problems per page
                     $this->assertEquals(PROBLEMS_PER_PAGE, count($first));
                 } else {
                     $request['page'] = null;
                     $response = ProblemController::apiList($request);
                     $first = $response['results'];
                     $last = $first;
                 }
                 $i = 0;
                 $j = count($last) - 1;
                 if ($col === 'title') {
                     $comp = strcmp($first[$i]['title'], $last[$j]['title']);
                     if ($mode === 'asc') {
                         $this->assertTrue($comp <= 0);
                     } else {
                         $this->assertTrue($comp >= 0);
                     }
                 } else {
                     if ($mode === 'asc') {
                         $this->assertTrue($first[$i][$col] <= $last[$j][$col]);
                     } else {
                         $this->assertTrue($first[$i][$col] >= $last[$j][$col]);
                     }
                 }
             }
         }
     }
 }
 /**
  * Best score is returned, problem inside a contest
  */
 public function testScoreInDetailsInsideContest()
 {
     // Create problem and contest
     $problemData = ProblemsFactory::createProblem();
     $contestData = ContestsFactory::createContest();
     ContestsFactory::addProblemToContest($problemData, $contestData);
     // Create contestant
     $contestant = UserFactory::createUser();
     // Create 2 runs, 100 and 50.
     $runDataOutsideContest = RunsFactory::createRunToProblem($problemData, $contestant);
     $runDataInsideContest = RunsFactory::createRun($problemData, $contestData, $contestant);
     RunsFactory::gradeRun($runDataOutsideContest);
     RunsFactory::gradeRun($runDataInsideContest, 0.5, "PA");
     // Call API
     $response = ProblemController::apiDetails(new Request(array("auth_token" => $this->login($contestant), "problem_alias" => $problemData["request"]["alias"], "contest_alias" => $contestData["request"]["alias"])));
     $this->assertEquals(50.0, $response["score"]);
 }