Skip to content

Commit ee5d452

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

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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');

0 commit comments

Comments
 (0)