fixed several issues

- search indexes should be different for testing
- added ability to flush the search index between tests
- fixed missing attributes in model factories
This commit is contained in:
pushrbx 2022-12-04 19:53:27 +00:00
parent 8f102d2b56
commit 72b6bb9442
11 changed files with 108 additions and 15 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
namespace App\Http\Concerns;
trait MakesHttpRequestsEx trait MakesHttpRequestsEx
{ {
@ -6,9 +7,9 @@ trait MakesHttpRequestsEx
* Visit the given URI with a JSON GET request. * Visit the given URI with a JSON GET request.
* @param string $uri * @param string $uri
* @param array $headers * @param array $headers
* @return TestCase * @return self
*/ */
public function getJson(string $uri, array $headers = []): TestCase public function getJson(string $uri, array $headers = []): self
{ {
return $this->json('GET', $uri, [], $headers); return $this->json('GET', $uri, [], $headers);
} }

View File

@ -39,7 +39,7 @@ abstract class JikanApiSearchableModel extends JikanApiModel implements Typesens
*/ */
public function searchableAs(): string public function searchableAs(): string
{ {
return strtolower($this->table) . '_index'; return strtolower($this->table) . '_index' . (env("APP_ENV") === "testing" ? "_testing" : "");
} }
/** /**

View File

@ -1,5 +1,5 @@
<?php <?php
namespace Database\Factories; namespace App\Testing;
use Faker\Generator; use Faker\Generator;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
@ -126,4 +126,9 @@ trait JikanDataGenerator
{ {
return $this->faker->randomElements($this->dummyGenres, $count); return $this->faker->randomElements($this->dummyGenres, $count);
} }
public function stuff()
{
return "";
}
} }

View File

@ -0,0 +1,16 @@
<?php
namespace App\Testing;
trait ScoutFlush
{
public function runScoutFlush(): void
{
$this->artisan("scout:flush App\\Anime");
$this->artisan("scout:flush App\\Manga");
$this->artisan("scout:flush App\\Character");
$this->artisan("scout:flush App\\GenreAnime");
$this->artisan("scout:flush App\\GenreManga");
$this->artisan("scout:flush App\\Person");
}
}

View File

@ -2,6 +2,8 @@
namespace Database\Factories; namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use App\Anime; use App\Anime;
use App\Testing\JikanDataGenerator;
use MongoDB\BSON\UTCDateTime;
class AnimeFactory extends Factory class AnimeFactory extends Factory
@ -55,7 +57,7 @@ class AnimeFactory extends Factory
"favorites" => $this->faker->randomDigitNotNull(), "favorites" => $this->faker->randomDigitNotNull(),
"synopsis" => "test", "synopsis" => "test",
"background" => "test", "background" => "test",
"season" => $this->faker->randomElement(["winter", "spring", "fall", "summer"]), "premiered" => $this->faker->randomElement(["Winter", "Spring", "Fall", "Summer"]),
"broadcast" => [ "broadcast" => [
"day" => "", "day" => "",
"time" => "", "time" => "",
@ -96,7 +98,10 @@ class AnimeFactory extends Factory
"name" => "Shounen", "name" => "Shounen",
"url" => "https://myanimelist.net/anime/genre/27/Shounen" "url" => "https://myanimelist.net/anime/genre/27/Shounen"
] ]
] ],
"createdAt" => new UTCDateTime(),
"modifiedAt" => new UTCDateTime(),
"request_hash" => sprintf("request:%s:%s", "v4", sha1("http://localhost-test/v4/anime/" . $mal_id))
]; ];
} }
} }

View File

@ -2,8 +2,10 @@
namespace Database\Factories; namespace Database\Factories;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use App\Character; use App\Character;
use MongoDB\BSON\UTCDateTime;
class CharacterFactory extends Factory class CharacterFactory extends Factory
@ -25,7 +27,10 @@ class CharacterFactory extends Factory
"name_kanji" => "", "name_kanji" => "",
"nicknames" => [], "nicknames" => [],
"favorites" => $this->faker->randomDigitNotNull(), "favorites" => $this->faker->randomDigitNotNull(),
"about" => "test" "about" => "test",
"createdAt" => new UTCDateTime(),
"modifiedAt" => new UTCDateTime(),
"request_hash" => sprintf("request:%s:%s", "v4", sha1("http://localhost-test/v4/character/" . $mal_id))
]; ];
} }
} }

View File

@ -1,6 +1,8 @@
<?php <?php
namespace Database\Factories; namespace Database\Factories;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use MongoDB\BSON\UTCDateTime;
abstract class GenreFactory extends Factory abstract class GenreFactory extends Factory
{ {
@ -18,7 +20,10 @@ abstract class GenreFactory extends Factory
"mal_id" => $mal_id, "mal_id" => $mal_id,
"name" => $name, "name" => $name,
"url" => $url, "url" => $url,
"count" => $this->faker->randomDigit() "count" => $this->faker->randomDigit(),
"createdAt" => new UTCDateTime(),
"modifiedAt" => new UTCDateTime(),
"request_hash" => sprintf("request:%s:%s", "v4", sha1("http://localhost-test/v4/genres/". $this->mediaType ."/" . $mal_id))
]; ];
} }
} }

View File

@ -1,7 +1,9 @@
<?php <?php
namespace Database\Factories; namespace Database\Factories;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use App\Manga; use App\Manga;
use MongoDB\BSON\UTCDateTime;
class MangaFactory extends Factory class MangaFactory extends Factory
{ {
@ -79,7 +81,10 @@ class MangaFactory extends Factory
"name" => "Shounen", "name" => "Shounen",
"url" => "https://myanimelist.net/manga/genre/27/Shounen" "url" => "https://myanimelist.net/manga/genre/27/Shounen"
] ]
] ],
"createdAt" => new UTCDateTime(),
"modifiedAt" => new UTCDateTime(),
"request_hash" => sprintf("request:%s:%s", "v4", sha1("http://localhost-test/v4/manga/" . $mal_id))
]; ];
} }
} }

View File

@ -1,7 +1,9 @@
<?php <?php
namespace Database\Factories; namespace Database\Factories;
use App\Testing\JikanDataGenerator;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use App\Person; use App\Person;
use MongoDB\BSON\UTCDateTime;
class PersonFactory extends Factory class PersonFactory extends Factory
@ -33,7 +35,10 @@ class PersonFactory extends Factory
"alternate_names" => [], "alternate_names" => [],
"birthday" => $this->createRandomDateTime("-80 years")->toAtomString(), "birthday" => $this->createRandomDateTime("-80 years")->toAtomString(),
"favorites" => $this->faker->randomDigitNotNull(), "favorites" => $this->faker->randomDigitNotNull(),
"about" => "test" "about" => "test",
"createdAt" => new UTCDateTime(),
"modifiedAt" => new UTCDateTime(),
"request_hash" => sprintf("request:%s:%s", "v4", sha1("http://localhost-test/v4/people/" . $mal_id))
]; ];
} }
} }

View File

@ -1,10 +1,12 @@
<?php <?php
use App\Testing\ScoutFlush;
use Laravel\Lumen\Testing\DatabaseMigrations; use Laravel\Lumen\Testing\DatabaseMigrations;
use App\Anime; use App\Anime;
class AnimeSearchEndpointTest extends TestCase class AnimeSearchEndpointTest extends TestCase
{ {
use DatabaseMigrations; use DatabaseMigrations, ScoutFlush;
public function limitParameterCombinationsProvider(): array public function limitParameterCombinationsProvider(): array
{ {
@ -36,9 +38,29 @@ class AnimeSearchEndpointTest extends TestCase
$overrides = []; $overrides = [];
// let's make all database items the same type // let's make all database items the same type
if (array_key_exists("type", $additionalParams)) { if (array_key_exists("type", $additionalParams)) {
$overrides["type"] = $additionalParams["type"]; $overrides["type"] = match ($additionalParams["type"]) {
"ova" => "OVA",
"movie" => "Movie",
default => "TV"
};
} }
$f->make($overrides); if (array_key_exists("min_score", $additionalParams) && !array_key_exists("max_score", $additionalParams)) {
$overrides["score"] = $this->faker->randomFloat(2, floatval($additionalParams["min_score"]), 9.99);
}
if (!array_key_exists("min_score", $additionalParams) && array_key_exists("max_score", $additionalParams)) {
$overrides["score"] = $this->faker->randomFloat(2, 1.00, floatval($additionalParams["max_score"]));
}
if (array_key_exists("min_score", $additionalParams) && array_key_exists("max_score", $additionalParams)) {
$overrides["score"] = $this->faker->randomFloat(2, floatval($additionalParams["min_score"]), floatval($additionalParams["max_score"]));
}
if (array_key_exists("status", $additionalParams)) {
$overrides["score"] = match ($additionalParams["status"]) {
"complete" => "Completed",
"airing" => "Currently Airing",
"upcoming" => "Upcoming"
};
}
$f->create($overrides);
$parameters = http_build_query([ $parameters = http_build_query([
"limit" => $limitCount, "limit" => $limitCount,
...$additionalParams ...$additionalParams

View File

@ -1,17 +1,41 @@
<?php <?php
use App\Http\Concerns\MakesHttpRequestsEx;
use App\Testing\ScoutFlush;
abstract class TestCase extends Laravel\Lumen\Testing\TestCase abstract class TestCase extends Laravel\Lumen\Testing\TestCase
{ {
use MakesHttpRequestsEx; use MakesHttpRequestsEx;
protected Faker\Generator $faker;
protected function setUp(): void
{
parent::setUp();
$this->faker = Faker\Factory::create();
}
/** /**
* Creates the application. * Creates the application.
* *
* @return \Laravel\Lumen\Application * @return \Laravel\Lumen\Application
*/ */
public function createApplication() public function createApplication()
{ {
return require __DIR__.'/../bootstrap/app.php'; $app = require __DIR__.'/../bootstrap/app.php';
$database = env('DB_DATABASE', 'jikan_tests');
$app['config']->set('database.connections.mongodb.database', $database === 'jikan' ? 'jikan_test' : $database);
return $app;
}
protected function tearDown(): void
{
parent::tearDown();
$uses = array_flip(class_uses_recursive(get_class($this)));
// we want to empty the search index
if (isset($uses[ScoutFlush::class])) {
/** @noinspection PhpUndefinedMethodInspection */
$this->runScoutFlush();
}
} }
} }