This repository has been archived by the owner on May 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
102: Add feature tests r=curquiza a=mmachatschek WIP This adds feature tests to the repo and solves #90 and #81 ~~TODOS:~~ - [x] ~~Add documentation how to run feature tests locally~~ - [x] Add more tests (based on https://github.com/meilisearch/meilisearch-symfony/) - [x] Update the cleanup method and add a `SCOUT_PREFIX` config with a test key and only remove indexes with that prefix to avoid deleting documents from a local meilisearch instance ~~I added a matrix for the meilisearch containers to also test the library against older versions. If this should be removed, just tell me @shokme or @curquiza.~~ Co-authored-by: Markus Machatschek <mmachatschek@yahoo.com>
- Loading branch information
Showing
10 changed files
with
364 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Meilisearch\Scout\Tests\Feature; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use MeiliSearch\Client; | ||
use MeiliSearch\Endpoints\Indexes; | ||
use Meilisearch\Scout\Tests\TestCase; | ||
|
||
abstract class FeatureTestCase extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('searchable_models', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('title'); | ||
$table->timestamps(); | ||
}); | ||
|
||
$this->cleanUp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->cleanUp(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
protected function cleanUp(): void | ||
{ | ||
collect(resolve(Client::class)->getAllIndexes())->each(function (Indexes $index) { | ||
// Starts with prefix | ||
if (substr($index->getUid(), 0, strlen($this->getPrefix())) === $this->getPrefix()) { | ||
$index->delete(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Meilisearch\Scout\Tests\Feature; | ||
|
||
use MeiliSearch\Client; | ||
use MeiliSearch\Exceptions\HTTPRequestException; | ||
|
||
class MeilisearchConsoleCommandTest extends FeatureTestCase | ||
{ | ||
/** @test */ | ||
public function nameArgumentIsRequired() | ||
{ | ||
$this->expectExceptionMessage('Not enough arguments (missing: "name").'); | ||
$this->artisan('scout:index') | ||
->execute(); | ||
} | ||
|
||
/** @test */ | ||
public function indexCanBeCreatedAndDeleted() | ||
{ | ||
$indexUid = $this->getPrefixedIndexUid('testindex'); | ||
|
||
$this->artisan('scout:index', [ | ||
'name' => $indexUid, | ||
]) | ||
->expectsOutput('Index "'.$indexUid.'" created.') | ||
->assertExitCode(0) | ||
->run(); | ||
|
||
$indexResponse = resolve(Client::class)->index($indexUid)->fetchRawInfo(); | ||
|
||
$this->assertIsArray($indexResponse); | ||
$this->assertSame($indexUid, $indexResponse['uid']); | ||
|
||
$this->artisan('scout:index', [ | ||
'name' => $indexUid, | ||
'--delete' => true, | ||
]) | ||
->expectsOutput('Index "'.$indexUid.'" deleted.') | ||
->assertExitCode(0) | ||
->run(); | ||
|
||
try { | ||
resolve(Client::class)->index($indexUid)->fetchRawInfo(); | ||
$this->fail('Exception should be thrown that index doesn\'t exist!'); | ||
} catch (HTTPRequestException $exception) { | ||
$this->assertTrue(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
<?php | ||
|
||
namespace Meilisearch\Scout\Tests\Feature; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Laravel\Scout\EngineManager; | ||
use MeiliSearch\Client; | ||
use Meilisearch\Scout\Engines\MeilisearchEngine; | ||
use Meilisearch\Scout\Tests\Fixtures\SearchableModel as BaseSearchableModel; | ||
|
||
class MeilisearchTest extends FeatureTestCase | ||
{ | ||
use WithFaker; | ||
|
||
/** @test */ | ||
public function clientAndEngineCanBeResolved() | ||
{ | ||
$this->assertInstanceOf(Client::class, resolve(Client::class)); | ||
$this->assertInstanceOf(EngineManager::class, resolve(EngineManager::class)); | ||
$this->assertInstanceOf(MeilisearchEngine::class, resolve(EngineManager::class)->engine('meilisearch')); | ||
} | ||
|
||
/** @test */ | ||
public function clientCanTalkToMeilisearch() | ||
{ | ||
/** @var Client $engine */ | ||
$engine = resolve(Client::class); | ||
|
||
$this->assertNull($engine->health()); | ||
$versionResponse = $engine->version(); | ||
$this->assertIsArray($versionResponse); | ||
$this->assertArrayHasKey('commitSha', $versionResponse); | ||
$this->assertArrayHasKey('buildDate', $versionResponse); | ||
$this->assertArrayHasKey('pkgVersion', $versionResponse); | ||
} | ||
|
||
/** @test */ | ||
public function searchReturnsModels() | ||
{ | ||
$model = $this->createSearchableModel('foo'); | ||
$this->createSearchableModel('bar'); | ||
|
||
$this->assertDatabaseCount('searchable_models', 2); | ||
|
||
$searchResponse = $this->waitForPendingUpdates($model, function () { | ||
return SearchableModel::search('bar')->raw(); | ||
}); | ||
|
||
$this->assertIsArray($searchResponse); | ||
$this->assertArrayHasKey('hits', $searchResponse); | ||
$this->assertArrayHasKey('query', $searchResponse); | ||
$this->assertTrue(1 === count($searchResponse['hits'])); | ||
} | ||
|
||
/** @test */ | ||
public function searchReturnsCorrectModelAfterUpdate() | ||
{ | ||
$fooModel = $this->createSearchableModel('foo'); | ||
$this->createSearchableModel('bar'); | ||
|
||
$this->assertDatabaseCount('searchable_models', 2); | ||
|
||
$searchResponse = $this->waitForPendingUpdates($fooModel, function () { | ||
return SearchableModel::search('foo')->raw(); | ||
}); | ||
|
||
$this->assertIsArray($searchResponse); | ||
$this->assertArrayHasKey('hits', $searchResponse); | ||
$this->assertArrayHasKey('query', $searchResponse); | ||
$this->assertTrue(1 === count($searchResponse['hits'])); | ||
$this->assertTrue('foo' === $searchResponse['hits'][0]['title']); | ||
|
||
$fooModel->update(['title' => 'lorem']); | ||
|
||
$searchResponse = $this->waitForPendingUpdates($fooModel, function () { | ||
return SearchableModel::search('lorem')->raw(); | ||
}); | ||
|
||
$this->assertIsArray($searchResponse); | ||
$this->assertArrayHasKey('hits', $searchResponse); | ||
$this->assertArrayHasKey('query', $searchResponse); | ||
$this->assertTrue(1 === count($searchResponse['hits'])); | ||
$this->assertTrue('lorem' === $searchResponse['hits'][0]['title']); | ||
} | ||
|
||
/** @test */ | ||
public function customSearchReturnsResults() | ||
{ | ||
$models = $this->createMultipleSearchableModels(10); | ||
|
||
$this->assertDatabaseCount('searchable_models', 10); | ||
|
||
$searchResponse = $this->waitForPendingUpdates($models->first(), function () { | ||
return SearchableModel::search('', function ($meilisearch, $query, $options) { | ||
$options['limit'] = 2; | ||
|
||
return $meilisearch->search($query, $options); | ||
})->raw(); | ||
}); | ||
|
||
$this->assertIsArray($searchResponse); | ||
$this->assertArrayHasKey('hits', $searchResponse); | ||
$this->assertArrayHasKey('query', $searchResponse); | ||
$this->assertTrue(2 === $searchResponse['limit']); | ||
$this->assertTrue(2 === count($searchResponse['hits'])); | ||
} | ||
|
||
/** | ||
* Fixes race condition and waits some time for the indexation to complete. | ||
* | ||
* @param Model $model | ||
* @param callable $callback | ||
* | ||
* @return mixed | ||
*/ | ||
protected function waitForPendingUpdates($model, $callback) | ||
{ | ||
$index = resolve(Client::class)->index($model->searchableAs()); | ||
$pendingUpdates = $index->getAllUpdateStatus(); | ||
|
||
foreach ($pendingUpdates as $pendingUpdate) { | ||
if ('processed' !== $pendingUpdate['status']) { | ||
$index->waitForPendingUpdate($pendingUpdate['updateId']); | ||
} | ||
} | ||
|
||
return $callback(); | ||
} | ||
|
||
protected function createMultipleSearchableModels(int $times = 1) | ||
{ | ||
$models = collect(); | ||
|
||
for ($i = 1; $i <= $times; ++$i) { | ||
$models->add($this->createSearchableModel()); | ||
} | ||
|
||
return $models; | ||
} | ||
|
||
protected function createSearchableModel(?string $title = null) | ||
{ | ||
return SearchableModel::create([ | ||
'title' => $title ?? $this->faker->sentence, | ||
]); | ||
} | ||
} | ||
|
||
class SearchableModel extends BaseSearchableModel | ||
{ | ||
public function searchableAs() | ||
{ | ||
return config('scout.prefix').$this->getTable(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.