diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0e93b358..b5f5b11f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -115,6 +115,10 @@ jobs: run: ./vendor/bin/phpunit --coverage-clover coverage.xml + - name: Show logs after test failure + if: failure() + run: find ./storage/logs/daily/*.log | head -n 1 | xargs cat + - name: Upload coverage reports uses: codecov/codecov-action@v3 with: diff --git a/app/JikanApiSearchableModel.php b/app/JikanApiSearchableModel.php index 3d5ae204..01c5e766 100644 --- a/app/JikanApiSearchableModel.php +++ b/app/JikanApiSearchableModel.php @@ -21,12 +21,20 @@ public abstract function typesenseQueryBy(): array; */ public function getCollectionSchema(): array { + $titleAttributeName = $this->getTitleAttributeName(); + return [ 'name' => $this->searchableAs(), 'fields' => [ [ 'name' => '.*', 'type' => 'auto', + ], + [ + 'name' => $titleAttributeName, + 'type' => 'string', + 'sort' => true, + 'optional' => false ] ] ]; diff --git a/app/Producers.php b/app/Producers.php index 8e2c369a..f46216d8 100644 --- a/app/Producers.php +++ b/app/Producers.php @@ -59,11 +59,13 @@ public static function scrape(int $id) public function toSearchableArray(): array { + $titles = !is_null($this->titles) ? collect($this->titles)->map(fn ($x) => $x["title"])->toArray() : ['']; + return [ 'id' => (string) $this->mal_id, 'mal_id' => (int) $this->mal_id, 'url' => !is_null($this->url) ? $this->url : '', - 'titles' => !is_null($this->titles) ? collect($this->titles)->map(fn ($x) => $x["title"])->toArray() : [''], + 'titles' => implode(', ', $titles), 'established' => $this->convertToTimestamp($this->established), 'favorites' => $this->favorites, 'count' => $this->count diff --git a/app/Profile.php b/app/Profile.php index bfc26fc3..9cfd99fb 100644 --- a/app/Profile.php +++ b/app/Profile.php @@ -75,4 +75,9 @@ public function typesenseQueryBy(): array "username" ]; } + + public function getTitleAttributeName(): string + { + return "username"; + } } diff --git a/app/Testing/ScoutFlush.php b/app/Testing/ScoutFlush.php index 8a1a89ff..137c12f0 100644 --- a/app/Testing/ScoutFlush.php +++ b/app/Testing/ScoutFlush.php @@ -18,7 +18,8 @@ trait ScoutFlush "App\\Person", "App\\Club", "App\\Magazine", - "App\\Producers" + "App\\Producers", + "App\\Profile", ]; public function runScoutFlush(): void @@ -39,6 +40,8 @@ public function runScoutFlush(): void "filter_by" => "mal_id:>0", "batch_size" => 500 ]); + + $typeSenseClient->deleteCollection($modelInstance->searchableAs()); } } } diff --git a/tests/Unit/JikanApiSearchableModelTest.php b/tests/Unit/JikanApiSearchableModelTest.php new file mode 100644 index 00000000..861615ca --- /dev/null +++ b/tests/Unit/JikanApiSearchableModelTest.php @@ -0,0 +1,52 @@ +titleAttributeNameFixture; + } +} + +final class JikanApiSearchableModelTest extends TestCase +{ + public function titleFieldDataProvider() + { + return [ + ["name"], + ["username"], + ["title"] + ]; + } + + /** + * @dataProvider titleFieldDataProvider + */ + public function testGetCollectionSchemaShouldReturnSortableTitleFieldInSchemaConfig($titleAttributeNameFixture) + { + $fixture = new JikanApiSearchableModelFixture(); + $fixture->titleAttributeNameFixture = $titleAttributeNameFixture; + $schema = $fixture->getCollectionSchema(); + + $this->assertArrayHasKey('fields', $schema); + $this->assertArrayHasKey('name', $schema['fields'][1]); + $this->assertArrayHasKey('type', $schema['fields'][1]); + $this->assertArrayHasKey('sort', $schema['fields'][1]); + $this->assertArrayHasKey('optional', $schema['fields'][1]); + $this->assertEquals($titleAttributeNameFixture, $schema['fields'][1]['name']); + $this->assertEquals('string', $schema['fields'][1]['type']); + $this->assertTrue($schema['fields'][1]['sort']); + $this->assertFalse($schema['fields'][1]['optional']); + } +}