mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
added tests for orderBy parameter
This commit is contained in:
parent
9a51454241
commit
716d8ea8e7
@ -4,6 +4,7 @@ namespace Database\Factories;
|
||||
use App\CarbonDateRange;
|
||||
use App\GenreAnime;
|
||||
use App\Anime;
|
||||
use App\Http\QueryBuilder\AnimeSearchQueryBuilder;
|
||||
use App\Testing\JikanDataGenerator;
|
||||
use Illuminate\Support\Collection;
|
||||
use MongoDB\BSON\UTCDateTime;
|
||||
@ -127,6 +128,60 @@ class AnimeFactory extends JikanModelFactory
|
||||
return $this->state($this->serializeStateDefinition($overrides));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to create records in db in sequential order.
|
||||
* The records are created with an increasing value for the specified field.
|
||||
* @param string $orderByField The field to order items by. Used to generate increasing value for the field.
|
||||
* @return Collection The created items/records
|
||||
*/
|
||||
public function createManyWithOrder(string $orderByField): Collection
|
||||
{
|
||||
$count = $this->count ?? 3;
|
||||
$items = collect();
|
||||
|
||||
$fieldValueGenerator = match($orderByField) {
|
||||
"aired.from", "aired.to" => ((function() {
|
||||
$randomDate = $this->createRandomDateTime("-5 years");
|
||||
return fn($i) => $randomDate->copy()->addDays($i);
|
||||
})()),
|
||||
"rating" => ((function() {
|
||||
$validRatingNumbers = array_map(fn($el) => floatval($el), range(1, 9));
|
||||
$validRatingNumbersCount = count($validRatingNumbers);
|
||||
return fn($i) => $validRatingNumbers[$i % $validRatingNumbersCount];
|
||||
})()),
|
||||
"title" => ((function() {
|
||||
$alphabet = range("a", "z");
|
||||
$alphabetCount = count($alphabet);
|
||||
return fn($i) => $alphabet[$i % $alphabetCount];
|
||||
})()),
|
||||
"type" => ((function() {
|
||||
$types = array_values(AnimeSearchQueryBuilder::MAP_TYPES);
|
||||
$typesCount = count($types);
|
||||
return fn($i) => $types[$i % $typesCount];
|
||||
})()),
|
||||
default => fn($i) => $i,
|
||||
};
|
||||
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
if ($orderByField === "aired.from") {
|
||||
$createdItem = $this->createOne($this->serializeStateDefinition([
|
||||
"aired" => new CarbonDateRange($fieldValueGenerator($i), null)
|
||||
]));
|
||||
} else if ($orderByField === "aired.to") {
|
||||
$createdItem = $this->createOne($this->serializeStateDefinition([
|
||||
"aired" => new CarbonDateRange(null, $fieldValueGenerator($i))
|
||||
]));
|
||||
} else {
|
||||
$createdItem = $this->createOne([
|
||||
$orderByField => $fieldValueGenerator($i)
|
||||
]);
|
||||
}
|
||||
$items->add($createdItem);
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private function getRandomRating(): string
|
||||
{
|
||||
return $this->faker->randomElement([
|
||||
|
@ -2,9 +2,12 @@
|
||||
namespace Tests\Integration;
|
||||
use App\Anime;
|
||||
use App\CarbonDateRange;
|
||||
use App\Http\QueryBuilder\AnimeSearchQueryBuilder;
|
||||
use App\Http\QueryBuilder\MediaSearchQueryBuilder;
|
||||
use App\Testing\ScoutFlush;
|
||||
use App\Testing\SyntheticMongoDbTransaction;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AnimeSearchEndpointTest extends TestCase
|
||||
@ -140,6 +143,18 @@ class AnimeSearchEndpointTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
public function orderByFieldMappingProvider(): array
|
||||
{
|
||||
$orderByFieldMappings = array_merge(MediaSearchQueryBuilder::ORDER_BY, AnimeSearchQueryBuilder::ORDER_BY);
|
||||
$params = [];
|
||||
|
||||
foreach ($orderByFieldMappings as $paramName => $orderByField) {
|
||||
$params[] = [$paramName, $orderByField];
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
@ -350,6 +365,36 @@ class AnimeSearchEndpointTest extends TestCase
|
||||
$this->assertCount(5, $content["data"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider orderByFieldMappingProvider
|
||||
*/
|
||||
public function testOrdering(string $paramName, string $orderByField)
|
||||
{
|
||||
$expectedCount = 3;
|
||||
$f = Anime::factory($expectedCount);
|
||||
/**
|
||||
* @var Collection $items
|
||||
*/
|
||||
$items = $f->createManyWithOrder($orderByField);
|
||||
$content = $this->getJsonResponse([
|
||||
"orderBy" => $paramName
|
||||
]);
|
||||
|
||||
$this->seeStatusCode(200);
|
||||
$this->assertPaginationData($expectedCount);
|
||||
$this->assertIsArray($content["data"]);
|
||||
$this->assertCount($expectedCount, $content["data"]);
|
||||
$expectedItems = $items->map(fn($elem) => data_get($elem, $orderByField));
|
||||
$actualItems = collect($content["data"])->map(fn($elem) => data_get($elem, $orderByField));
|
||||
|
||||
if ($actualItems->first() instanceof Carbon && $expectedItems->first() instanceof Carbon) {
|
||||
$expectedItems = $expectedItems->map(fn(Carbon $elem) => $elem->getTimestamp());
|
||||
$actualItems = $actualItems->map(fn(Carbon $elem) => $elem->getTimestamp());
|
||||
}
|
||||
|
||||
$this->assertEquals(0, $expectedItems->diff($actualItems)->count());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidRatingParameterProvider
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user