Skip to content

Commit 0a4fd4b

Browse files
committed
Use a distinct collection names for each test using atlas search indexes
1 parent dee170a commit 0a4fd4b

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

docs/includes/fundamentals/as-avs/AtlasSearchTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\DB;
99
use MongoDB\Builder\Query;
1010
use MongoDB\Builder\Search;
11+
use MongoDB\Collection;
1112
use MongoDB\Driver\Exception\ServerException;
1213
use MongoDB\Laravel\Schema\Builder;
1314
use MongoDB\Laravel\Tests\TestCase;
@@ -32,6 +33,7 @@ protected function setUp(): void
3233
parent::setUp();
3334

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

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

52-
$moviesCollection = DB::connection('mongodb')->getCollection('movies');
54+
// Waits for the search index created in previous test to be deleted
55+
do {
56+
usleep(1_000);
57+
} while ($moviesCollection->listSearchIndexes()->count());
5358

5459
try {
5560
$moviesCollection->createSearchIndex([
@@ -87,9 +92,7 @@ protected function setUp(): void
8792
$ready = true;
8893
usleep(10_000);
8994
foreach ($moviesCollection->listSearchIndexes() as $index) {
90-
if ($index['status'] !== 'READY') {
91-
$ready = false;
92-
}
95+
$ready = $ready && $index['queryable'];
9396
}
9497
} while (! $ready);
9598
}

tests/AtlasSearchTest.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class AtlasSearchTest extends TestCase
2929

3030
public function setUp(): void
3131
{
32+
// Use a unique prefix per test to avoid collisions when the search index is
33+
// deleted asynchronously while we try to create it in setup of the next test
34+
$_SERVER['DB_PREFIX'] = 'AtlasSearchTest_' . $this->name() . '_';
35+
3236
parent::setUp();
3337

3438
Book::insert($this->addVector([
@@ -54,7 +58,7 @@ public function setUp(): void
5458
['title' => 'Pattern Recognition and Machine Learning'],
5559
]));
5660

57-
$collection = $this->getConnection('mongodb')->getCollection('books');
61+
$collection = $this->getConnection('mongodb')->getCollection($this->getBookCollectionName());
5862
assert($collection instanceof MongoDBCollection);
5963

6064
try {
@@ -92,25 +96,24 @@ public function setUp(): void
9296
// Wait for the index to be ready
9397
do {
9498
$ready = true;
95-
usleep(10_000);
99+
usleep(100);
96100
foreach ($collection->listSearchIndexes() as $index) {
97-
if ($index['status'] !== 'READY') {
98-
$ready = false;
99-
}
101+
$ready = $ready && $index['queryable'];
100102
}
101103
} while (! $ready);
102104
}
103105

104106
public function tearDown(): void
105107
{
106-
$this->getConnection('mongodb')->getCollection('books')->drop();
108+
$this->getConnection('mongodb')->getCollection($this->getBookCollectionName())->drop();
109+
unset($_SERVER['DB_PREFIX']);
107110

108111
parent::tearDown();
109112
}
110113

111114
public function testGetIndexes()
112115
{
113-
$indexes = Schema::getIndexes('books');
116+
$indexes = Schema::getIndexes($this->getBookCollectionName());
114117

115118
self::assertIsArray($indexes);
116119
self::assertCount(4, $indexes);
@@ -171,7 +174,7 @@ public function testEloquentBuilderSearch()
171174

172175
public function testDatabaseBuilderSearch()
173176
{
174-
$results = $this->getConnection('mongodb')->table('books')
177+
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
175178
->search(Search::text('title', 'systems'), sort: ['title' => 1]);
176179

177180
self::assertInstanceOf(LaravelCollection::class, $results);
@@ -199,7 +202,7 @@ public function testEloquentBuilderAutocomplete()
199202

200203
public function testDatabaseBuilderAutocomplete()
201204
{
202-
$results = $this->getConnection('mongodb')->table('books')
205+
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
203206
->autocomplete('title', 'system');
204207

205208
self::assertInstanceOf(LaravelCollection::class, $results);
@@ -213,7 +216,7 @@ public function testDatabaseBuilderAutocomplete()
213216

214217
public function testDatabaseBuilderVectorSearch()
215218
{
216-
$results = $this->getConnection('mongodb')->table('books')
219+
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
217220
->vectorSearch(
218221
index: 'vector',
219222
path: 'vector4',
@@ -253,6 +256,15 @@ public function testEloquentBuilderVectorSearch()
253256
);
254257
}
255258

259+
private function getBookCollectionName(): string
260+
{
261+
$name = (new Book())->getTable();
262+
263+
self::assertStringStartsWith('AtlasSearchTest_', $name);
264+
265+
return $name;
266+
}
267+
256268
/** Generate random vectors using fixed seed to make tests deterministic */
257269
private function addVector(array $items): array
258270
{

tests/Models/Book.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class Book extends Model
2323
protected $table = 'books';
2424
protected static $unguarded = true;
2525

26+
public function __construct(array $attributes = [])
27+
{
28+
/* @TODO remove when connection prefix is supported
29+
* @see https://jira.mongodb.org/browse/PHPORM-433 */
30+
$this->table = ($_SERVER['DB_PREFIX'] ?? '') . $this->table;
31+
32+
parent::__construct($attributes);
33+
}
34+
2635
public function author(): BelongsTo
2736
{
2837
return $this->belongsTo(User::class, 'author_id');

tests/Scout/ScoutIntegrationTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ public function testItCanCreateTheCollection()
112112
self::assertSame(['mappings' => ['dynamic' => true, 'fields' => ['bool_field' => ['type' => 'boolean']]]], iterator_to_array($searchIndexes)[0]['latestDefinition']);
113113

114114
// Wait for all documents to be indexed asynchronously
115-
$i = 100;
115+
$i = 1000;
116116
while (true) {
117+
usleep(10_000);
117118
$indexedDocuments = $collection->aggregate([
118119
['$search' => ['index' => 'scout', 'exists' => ['path' => 'name']]],
119120
])->toArray();
@@ -125,8 +126,6 @@ public function testItCanCreateTheCollection()
125126
if ($i-- === 0) {
126127
self::fail('Documents not indexed');
127128
}
128-
129-
usleep(100_000);
130129
}
131130

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

141140
self::assertSame([

0 commit comments

Comments
 (0)