mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
added letter parameter tests and updated episode model
This commit is contained in:
parent
fcea126272
commit
cefdbb1a1d
@ -2,16 +2,13 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Http\HttpHelper;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Jenssegers\Mongodb\Eloquent\Model;
|
||||
use Jikan\Helper\Media;
|
||||
use Jikan\Helper\Parser;
|
||||
use Jikan\Jikan;
|
||||
use Jikan\Model\Common\YoutubeMeta;
|
||||
use Jikan\Request\Anime\AnimeRequest;
|
||||
|
||||
class Episode extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
@ -34,7 +31,7 @@ class Episode extends Model
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'anime_episodes';
|
||||
protected $table = 'anime_episode';
|
||||
|
||||
/**
|
||||
* The attributes excluded from the model's JSON form.
|
||||
@ -42,6 +39,7 @@ class Episode extends Model
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'_id', 'request_hash'
|
||||
];
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -23,10 +23,10 @@ trait MakesHttpRequestsEx
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
public function getJsonResponse(array $params): array
|
||||
public function getJsonResponse(array $params, ?string $baseUri = null): array
|
||||
{
|
||||
$parameters = http_build_query($params);
|
||||
$uri = $this->getBaseUri() . "?" . $parameters;
|
||||
$uri = $baseUri ?? $this->getBaseUri() . "?" . $parameters;
|
||||
$this->getJson($uri);
|
||||
return $this->response->json();
|
||||
}
|
||||
|
@ -212,6 +212,24 @@ class AnimeFactory extends JikanModelFactory
|
||||
$overrides["type"] = $this->faker->randomElement(array_diff(array_keys($types), [$additionalParams["type"]]));
|
||||
}
|
||||
|
||||
if ($additionalParams->has("letter")) {
|
||||
$alphabet = array_filter(range("a", "z"), fn ($elem) => $elem !== $additionalParams["letter"]);
|
||||
$title = $this->faker->randomElement($alphabet) . $this->createTitle();
|
||||
$a = [
|
||||
"titles" => [
|
||||
[
|
||||
"type" => "Default",
|
||||
"title" => $title
|
||||
]
|
||||
],
|
||||
"title" => $title,
|
||||
"title_english" => $title,
|
||||
"title_japanese" => $title,
|
||||
"title_synonyms" => [$title],
|
||||
];
|
||||
$overrides = [...$overrides, ...$a];
|
||||
}
|
||||
|
||||
if ($additionalParams->has("min_score") && !$additionalParams->has("max_score")) {
|
||||
$min_score = floatval($additionalParams["min_score"]);
|
||||
if ($this->isScoreValueValid($min_score)) {
|
||||
@ -351,6 +369,23 @@ class AnimeFactory extends JikanModelFactory
|
||||
};
|
||||
}
|
||||
|
||||
if ($additionalParams->has("letter")) {
|
||||
$title = $additionalParams["letter"] . $this->createTitle();
|
||||
$a = [
|
||||
"titles" => [
|
||||
[
|
||||
"type" => "Default",
|
||||
"title" => $title
|
||||
]
|
||||
],
|
||||
"title" => $title,
|
||||
"title_english" => $title,
|
||||
"title_japanese" => $title,
|
||||
"title_synonyms" => [$title],
|
||||
];
|
||||
$overrides = [...$overrides, ...$a];
|
||||
}
|
||||
|
||||
if ($additionalParams->has("min_score") && !$additionalParams->has("max_score")) {
|
||||
$min_score = floatval($additionalParams["min_score"]);
|
||||
if ($this->isScoreValueValid($min_score)) {
|
||||
|
50
database/factories/EpisodeFactory.php
Normal file
50
database/factories/EpisodeFactory.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace Database\Factories;
|
||||
use App\Episode;
|
||||
use App\Testing\JikanDataGenerator;
|
||||
use MongoDB\BSON\UTCDateTime;
|
||||
|
||||
class EpisodeFactory extends JikanModelFactory
|
||||
{
|
||||
use JikanDataGenerator;
|
||||
|
||||
/**
|
||||
* The name of the factory's corresponding model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $model = Episode::class;
|
||||
|
||||
protected function definitionInternal(): array
|
||||
{
|
||||
$mal_id = $this->createMalId();
|
||||
$title = $this->createTitle();
|
||||
$score = $this->faker->randomFloat(2, 1.00, 9.99);
|
||||
$aired = $this->createRandomDateTime()->toAtomString();
|
||||
|
||||
return [
|
||||
"mal_id" => $mal_id,
|
||||
"url" => "https://myanimelist.net/anime/$mal_id/x/episode/$mal_id",
|
||||
"title" => $title,
|
||||
"title_japanese" => $title,
|
||||
"title_romaji" => $title,
|
||||
"aired" => $aired,
|
||||
"score" => $score,
|
||||
"filler" => false,
|
||||
"recap" => false,
|
||||
"forum_url" => "https://myanimelist.net/forum/?topicid=1919480",
|
||||
"synopsis" => "test",
|
||||
"createdAt" => new UTCDateTime(),
|
||||
"modifiedAt" => new UTCDateTime(),
|
||||
"request_hash" => "request:anime:" . sha1(env('APP_URL') . "/v4/" . "anime/" . $mal_id . "/episodes")
|
||||
];
|
||||
}
|
||||
|
||||
public function updateRequestHash(int $anime_mal_id): self
|
||||
{
|
||||
return $this->state([
|
||||
"request_hash" =>
|
||||
"request:anime:" . sha1(env('APP_URL') . "/v4/" . "anime/" . $anime_mal_id . "/episodes")
|
||||
]);
|
||||
}
|
||||
}
|
@ -155,6 +155,17 @@ class AnimeSearchEndpointTest extends TestCase
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function letterParameterProvider(): array
|
||||
{
|
||||
$letters = range("a", "f");
|
||||
$result = [];
|
||||
foreach ($letters as $letter) {
|
||||
$result[] = [["letter" => $letter], 5];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
@ -365,6 +376,25 @@ class AnimeSearchEndpointTest extends TestCase
|
||||
$this->assertCount(5, $content["data"]);
|
||||
}
|
||||
|
||||
public function testSearchByExplicitDefaultMinMaxScores()
|
||||
{
|
||||
// test for https://github.com/jikan-me/jikan-rest/issues/309
|
||||
Anime::factory(5)
|
||||
->overrideFromQueryStringParameters([
|
||||
"genres" => "1,2"
|
||||
])
|
||||
->create();
|
||||
$content = $this->getJsonResponse([
|
||||
"genres" => "1,2",
|
||||
"min_score" => "0.0",
|
||||
"max_score" => "10.0"
|
||||
]);
|
||||
$this->seeStatusCode(200);
|
||||
$this->assertPaginationData(5);
|
||||
$this->assertIsArray($content["data"]);
|
||||
$this->assertCount(5, $content["data"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider orderByFieldMappingProvider
|
||||
*/
|
||||
@ -410,6 +440,36 @@ class AnimeSearchEndpointTest extends TestCase
|
||||
$this->assertCount($expectedCount, $content["data"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider letterParameterProvider
|
||||
*/
|
||||
public function testSearchByLetter($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 testSearchByInvalidLetterParameter()
|
||||
{
|
||||
$expectedCount = 0;
|
||||
$this->generateFiveSpecificAndTenRandomElementsInDb([
|
||||
"letter" => "a"
|
||||
]);
|
||||
$content = $this->getJsonResponse([
|
||||
"letter" => "asd"
|
||||
]);
|
||||
|
||||
$this->seeStatusCode(200);
|
||||
$this->assertPaginationData($expectedCount);
|
||||
$this->assertIsArray($content["data"]);
|
||||
$this->assertCount($expectedCount, $content["data"]);
|
||||
}
|
||||
|
||||
public function testTypeSenseSearchPagination()
|
||||
{
|
||||
// this should test https://github.com/jikan-me/jikan-rest/issues/298
|
||||
|
Loading…
x
Reference in New Issue
Block a user