Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/includes/fundamentals/as-avs/AtlasSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\DB;
use MongoDB\Builder\Query;
use MongoDB\Builder\Search;
use MongoDB\Collection;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Laravel\Schema\Builder;
use MongoDB\Laravel\Tests\TestCase;
Expand All @@ -32,6 +33,7 @@ protected function setUp(): void
parent::setUp();

$moviesCollection = DB::connection('mongodb')->getCollection('movies');
self::assertInstanceOf(Collection::class, $moviesCollection);
$moviesCollection->drop();

Movie::insert([
Expand All @@ -49,7 +51,10 @@ protected function setUp(): void
['title' => 'D', 'plot' => 'Stranded on a distant planet, astronauts must repair their ship before supplies run out.'],
]));

$moviesCollection = DB::connection('mongodb')->getCollection('movies');
// Waits for the search index created in the previous test to be deleted
while ($moviesCollection->listSearchIndexes()->count()) {
usleep(1000);
}

try {
$moviesCollection->createSearchIndex([
Expand Down Expand Up @@ -87,9 +92,7 @@ protected function setUp(): void
$ready = true;
usleep(10_000);
foreach ($moviesCollection->listSearchIndexes() as $index) {
if ($index['status'] !== 'READY') {
$ready = false;
}
$ready = $ready && $index['queryable'];
}
} while (! $ready);
}
Expand Down
18 changes: 11 additions & 7 deletions tests/AtlasSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function setUp(): void
{
parent::setUp();

$collection = $this->getConnection('mongodb')->getCollection('books');
assert($collection instanceof MongoDBCollection);
$collection->drop();

Book::insert($this->addVector([
['title' => 'Introduction to Algorithms'],
['title' => 'Clean Code: A Handbook of Agile Software Craftsmanship'],
Expand All @@ -54,10 +58,12 @@ public function setUp(): void
['title' => 'Pattern Recognition and Machine Learning'],
]));

$collection = $this->getConnection('mongodb')->getCollection('books');
assert($collection instanceof MongoDBCollection);

try {
// Waits for the search index created in the previous test to be deleted
while ($collection->listSearchIndexes()->count()) {
usleep(1000);
}

$collection->createSearchIndex([
'mappings' => [
'fields' => [
Expand Down Expand Up @@ -92,11 +98,9 @@ public function setUp(): void
// Wait for the index to be ready
do {
$ready = true;
usleep(10_000);
usleep(1000);
foreach ($collection->listSearchIndexes() as $index) {
if ($index['status'] !== 'READY') {
$ready = false;
}
$ready = $ready && $index['queryable'];
}
} while (! $ready);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Scout/ScoutIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ public function testItCanCreateTheCollection()
self::assertSame(['mappings' => ['dynamic' => true, 'fields' => ['bool_field' => ['type' => 'boolean']]]], iterator_to_array($searchIndexes)[0]['latestDefinition']);

// Wait for all documents to be indexed asynchronously
$i = 100;
$i = 1000;
while (true) {
usleep(10_000);
$indexedDocuments = $collection->aggregate([
['$search' => ['index' => 'scout', 'exists' => ['path' => 'name']]],
])->toArray();
Expand All @@ -125,8 +126,6 @@ public function testItCanCreateTheCollection()
if ($i-- === 0) {
self::fail('Documents not indexed');
}

usleep(100_000);
}

self::assertCount(44, $indexedDocuments);
Expand All @@ -135,7 +134,7 @@ public function testItCanCreateTheCollection()
#[Depends('testItCanCreateTheCollection')]
public function testItCanUseBasicSearch()
{
// All the search queries use "sort" option to ensure the results are deterministic
// All the search queries use the "sort" option to ensure the results are deterministic
$results = ScoutUser::search('lar')->take(10)->orderBy('id')->get();

self::assertSame([
Expand Down
Loading