Skip to content

Commit

Permalink
Use ResultRow DTO now that we have it
Browse files Browse the repository at this point in the history
  • Loading branch information
nickygerritsen committed Jul 29, 2024
1 parent ca0665f commit 2e4abad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
4 changes: 2 additions & 2 deletions webapp/src/Command/CompareResultsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace App\Command;

use App\DataTransferObject\Result;
use App\DataTransferObject\ResultRow;
use App\Service\Compare\ResultsCompareService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @extends AbstractCompareCommand<Result[]>
* @extends AbstractCompareCommand<ResultRow[]>
*/
#[AsCommand(
name: 'compare:results',
Expand Down
16 changes: 0 additions & 16 deletions webapp/src/DataTransferObject/Result.php

This file was deleted.

20 changes: 10 additions & 10 deletions webapp/src/Service/Compare/ResultsCompareService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Service\Compare;

use App\DataTransferObject\Result;
use App\DataTransferObject\ResultRow;
use Symfony\Component\Serializer\Encoder\CsvEncoder;

/**
* @extends AbstractCompareService<Result[]>
* @extends AbstractCompareService<ResultRow[]>
*/
class ResultsCompareService extends AbstractCompareService
{
Expand All @@ -21,30 +21,30 @@ protected function parseFile(string $file)
$resultsContents = substr($resultsContents, strpos($resultsContents, "\n") + 1);

// Prefix file with a fake header, so we can deserialize them
$resultsContents = "team_id\trank\taward\tnum_solved\ttotal_time\tlast_time\tgroup_winner\n" . $resultsContents;
$resultsContents = "team_id\trank\taward\tnum_solved\ttotal_time\ttime_of_last_submission\tgroup_winner\n" . $resultsContents;

$results = $this->serializer->deserialize($resultsContents, Result::class . '[]', 'csv', [
$results = $this->serializer->deserialize($resultsContents, ResultRow::class . '[]', 'csv', [
CsvEncoder::DELIMITER_KEY => "\t",
]);

// Sort results: first by num_solved, then by total_time
usort($results, fn(
Result $a,
Result $b
ResultRow $a,
ResultRow $b
) => $a->numSolved === $b->numSolved ? $a->totalTime <=> $b->totalTime : $b->numSolved <=> $a->numSolved);

return $results;
}

public function compare($object1, $object2): void
{
/** @var array<string,Result> $results1Indexed */
/** @var array<string,ResultRow> $results1Indexed */
$results1Indexed = [];
foreach ($object1 as $result) {
$results1Indexed[$result->teamId] = $result;
}

/** @var array<string,Result> $results2Indexed */
/** @var array<string,ResultRow> $results2Indexed */
$results2Indexed = [];
foreach ($object2 as $result) {
$results2Indexed[$result->teamId] = $result;
Expand All @@ -67,8 +67,8 @@ public function compare($object1, $object2): void
if ($result->totalTime !== $result2->totalTime) {
$this->addMessage(MessageType::ERROR, sprintf('Team "%s" has different total time', $result->teamId), (string)$result->totalTime, (string)$result2->totalTime);
}
if ($result->lastTime !== $result2->lastTime) {
$this->addMessage(MessageType::ERROR, sprintf('Team "%s" has different last time', $result->teamId), (string)$result->lastTime, (string)$result2->lastTime);
if ($result->timeOfLastSubmission !== $result2->timeOfLastSubmission) {
$this->addMessage(MessageType::ERROR, sprintf('Team "%s" has different last time', $result->teamId), (string)$result->timeOfLastSubmission, (string)$result2->timeOfLastSubmission);
}
if ($result->groupWinner !== $result2->groupWinner) {
$this->addMessage(MessageType::WARNING, sprintf('Team "%s" has different group winner', $result->teamId), (string)$result->groupWinner, (string)$result2->groupWinner);
Expand Down
34 changes: 17 additions & 17 deletions webapp/tests/Unit/Service/Compare/ResultsCompareServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Tests\Unit\Service\Compare;

use App\DataTransferObject\Result;
use App\DataTransferObject\ResultRow;
use App\Service\Compare\Message;
use App\Service\Compare\MessageType;
use App\Service\Compare\ResultsCompareService;
Expand All @@ -13,8 +13,8 @@
class ResultsCompareServiceTest extends KernelTestCase
{
/**
* @param Result[] $results1
* @param Result[] $results2
* @param ResultRow[] $results1
* @param ResultRow[] $results2
* @param Message[] $expectedMessages
*
* @dataProvider provideCompare
Expand All @@ -34,43 +34,43 @@ public function provideCompare(): Generator
{
yield [[], [], []];
yield [
[new Result('team1', 1, '', 0, 0, 0)],
[new ResultRow('team1', 1, '', 0, 0, 0)],
[],
[new Message(MessageType::ERROR, 'Team "team1" not found in second file', null, null)],
];
yield [
[],
[new Result('team2', 1, '', 0, 0, 0)],
[new ResultRow('team2', 1, '', 0, 0, 0)],
[new Message(MessageType::ERROR, 'Team "team2" not found in first file', null, null)],
];
yield [
[new Result('team3', 1, '', 0, 0, 0)],
[new Result('team3', 2, '', 0, 0, 0)],
[new ResultRow('team3', 1, '', 0, 0, 0)],
[new ResultRow('team3', 2, '', 0, 0, 0)],
[new Message(MessageType::ERROR, 'Team "team3" has different rank', '1', '2')],
];
yield [
[new Result('team4', 1, 'award1', 0, 0, 0)],
[new Result('team4', 1, 'award2', 0, 0, 0)],
[new ResultRow('team4', 1, 'award1', 0, 0, 0)],
[new ResultRow('team4', 1, 'award2', 0, 0, 0)],
[new Message(MessageType::ERROR, 'Team "team4" has different award', 'award1', 'award2')],
];
yield [
[new Result('team5', 1, 'award3', 1, 0, 0)],
[new Result('team5', 1, 'award3', 2, 0, 0)],
[new ResultRow('team5', 1, 'award3', 1, 0, 0)],
[new ResultRow('team5', 1, 'award3', 2, 0, 0)],
[new Message(MessageType::ERROR, 'Team "team5" has different num solved', '1', '2')],
];
yield [
[new Result('team6', 1, 'award4', 1, 100, 0)],
[new Result('team6', 1, 'award4', 1, 200, 0)],
[new ResultRow('team6', 1, 'award4', 1, 100, 0)],
[new ResultRow('team6', 1, 'award4', 1, 200, 0)],
[new Message(MessageType::ERROR, 'Team "team6" has different total time', '100', '200')],
];
yield [
[new Result('team7', 1, 'award4', 1, 100, 10)],
[new Result('team7', 1, 'award4', 1, 100, 20)],
[new ResultRow('team7', 1, 'award4', 1, 100, 10)],
[new ResultRow('team7', 1, 'award4', 1, 100, 20)],
[new Message(MessageType::ERROR, 'Team "team7" has different last time', '10', '20')],
];
yield [
[new Result('team8', 1, 'award4', 1, 100, 10, 'winner1')],
[new Result('team8', 1, 'award4', 1, 100, 10, 'winner2')],
[new ResultRow('team8', 1, 'award4', 1, 100, 10, 'winner1')],
[new ResultRow('team8', 1, 'award4', 1, 100, 10, 'winner2')],
[new Message(MessageType::WARNING, 'Team "team8" has different group winner', 'winner1', 'winner2')],
];
}
Expand Down

0 comments on commit 2e4abad

Please sign in to comment.