added tests for rating parameter

This commit is contained in:
pushrbx 2022-12-16 19:11:33 +00:00
parent 2d2a1c97ed
commit 251fccb5f6
2 changed files with 70 additions and 9 deletions

View File

@ -49,13 +49,7 @@ class AnimeFactory extends JikanModelFactory
"airing" => $status == "Currently Airing", "airing" => $status == "Currently Airing",
"aired" => new CarbonDateRange($aired_from, $aired_to), "aired" => new CarbonDateRange($aired_from, $aired_to),
"duration" => "", "duration" => "",
"rating" => $this->faker->randomElement([ "rating" => $this->getRandomRating(),
"R - 17+ (violence & profanity)",
"PG - Children",
"PG-13 - Teens 13 or older",
"R+ - Mild Nudity",
"Rx - Hentai"
]),
"score" => $this->faker->randomFloat(2, 1.00, 9.99), "score" => $this->faker->randomFloat(2, 1.00, 9.99),
"scored_by" => $this->faker->randomDigitNotNull(), "scored_by" => $this->faker->randomDigitNotNull(),
"rank" => $this->faker->randomDigitNotNull(), "rank" => $this->faker->randomDigitNotNull(),
@ -133,6 +127,18 @@ class AnimeFactory extends JikanModelFactory
return $this->state($this->serializeStateDefinition($overrides)); return $this->state($this->serializeStateDefinition($overrides));
} }
private function getRandomRating(): string
{
return $this->faker->randomElement([
"G - All Ages",
"PG - Children",
"PG-13 - Teens 13 or older",
"R - 17+ (violence & profanity)",
"R+ - Mild Nudity",
"Rx - Hentai"
]);
}
private function isScoreValueValid($score): bool private function isScoreValueValid($score): bool
{ {
return $score <= 9.99 && $score >= 0.0; return $score <= 9.99 && $score >= 0.0;
@ -189,6 +195,19 @@ class AnimeFactory extends JikanModelFactory
$overrides["status"] = $statuses[$rndKey]; $overrides["status"] = $statuses[$rndKey];
} }
if ($additionalParams->has("rating")) {
$ratings = [
"g" => "G - All Ages",
"pg" => "PG - Children",
"pg13" => "PG-13 - Teens 13 or older",
"r17" => "R - 17+ (violence & profanity)",
"r" => "R+ - Mild Nudity",
"rx" => "Rx - Hentai",
];
$rndKey = $this->faker->randomElement(array_diff(array_keys($ratings), [strtolower($additionalParams["rating"])]));
$overrides["rating"] = $ratings[$rndKey];
}
if (($additionalParams->has("genres") && $additionalParams->has("genres_exclude")) || ( if (($additionalParams->has("genres") && $additionalParams->has("genres_exclude")) || (
!$additionalParams->has("genres") && $additionalParams->has("genres_exclude") !$additionalParams->has("genres") && $additionalParams->has("genres_exclude")
) ) { ) ) {
@ -314,6 +333,18 @@ class AnimeFactory extends JikanModelFactory
}; };
} }
if ($additionalParams->has("rating")) {
$overrides["rating"] = match (strtolower($additionalParams["rating"])) {
"g" => "G - All Ages",
"pg" => "PG - Children",
"pg13" => "PG-13 - Teens 13 or older",
"r17" => "R - 17+ (violence & profanity)",
"r" => "R+ - Mild Nudity",
"rx" => "Rx - Hentai",
default => $this->getRandomRating()
};
}
if ($additionalParams->has("genres")) { if ($additionalParams->has("genres")) {
$overrides["genres"] = []; $overrides["genres"] = [];
$genreIds = explode(",", $additionalParams["genres"]); $genreIds = explode(",", $additionalParams["genres"]);

View File

@ -111,7 +111,13 @@ class AnimeSearchEndpointTest extends TestCase
[["status" => "Upcoming"]], [["status" => "Upcoming"]],
[["max_score" => "8"]], [["max_score" => "8"]],
[["min_score" => "6"]], [["min_score" => "6"]],
[["max_score" => "7", "min_score" => "3"]] [["max_score" => "7", "min_score" => "3"]],
[["rating" => "pg"]],
[["rating" => "rx"]],
[["rating" => "r"]],
[["rating" => "pg13"]],
[["rating" => "g"]],
[["rating" => "r17"]],
]; ];
} }
@ -120,7 +126,17 @@ class AnimeSearchEndpointTest extends TestCase
return [ return [
[["max_score" => "634638"], 15], [["max_score" => "634638"], 15],
[["min_score" => "673473"], 0], [["min_score" => "673473"], 0],
[["max_score" => "72344", "min_score" => "3532325"], 0] [["max_score" => "72344", "min_score" => "3532325"], 0],
[["max_score" => 1, "min_score" => 5], 0],
];
}
public function invalidRatingParameterProvider(): array
{
return [
[["rating" => "6263ssd"], 15],
[["rating" => "rx1"], 15],
[["rating" => "pg133"], 15]
]; ];
} }
@ -334,6 +350,20 @@ class AnimeSearchEndpointTest extends TestCase
$this->assertCount(5, $content["data"]); $this->assertCount(5, $content["data"]);
} }
/**
* @dataProvider invalidRatingParameterProvider
*/
public function testSearchByInvalidRatingParameters($params, $expectedCount)
{
$this->generateFiveSpecificAndTenRandomElementsInDb($params);
$content = $this->getJsonResponse($params);
$this->seeStatusCode(200);
$this->assertPaginationData($expectedCount);
$this->assertIsArray($content["data"]);
$this->assertCount($expectedCount, $content["data"]);
}
public function testTypeSenseSearchPagination() public function testTypeSenseSearchPagination()
{ {
// this should test https://github.com/jikan-me/jikan-rest/issues/298 // this should test https://github.com/jikan-me/jikan-rest/issues/298