Skip to content

Commit 3c3d256

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

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

tests/AtlasSearchTest.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use MongoDB\Laravel\Schema\Builder;
1313
use MongoDB\Laravel\Tests\Models\Book;
1414
use PHPUnit\Framework\Attributes\Group;
15+
use ReflectionClass;
1516

1617
use function array_map;
1718
use function assert;
@@ -29,6 +30,10 @@ class AtlasSearchTest extends TestCase
2930

3031
public function setUp(): void
3132
{
33+
// Use a unique prefix per test to avoid collisions when the search index is
34+
// deleted asynchronously while we try to create it in setup of the next test
35+
$_SERVER['DB_PREFIX'] = (new ReflectionClass($this))->getShortName() . '_' . $this->name() . '_';
36+
3237
parent::setUp();
3338

3439
Book::insert($this->addVector([
@@ -54,7 +59,7 @@ public function setUp(): void
5459
['title' => 'Pattern Recognition and Machine Learning'],
5560
]));
5661

57-
$collection = $this->getConnection('mongodb')->getCollection('books');
62+
$collection = $this->getConnection('mongodb')->getCollection($this->getBookCollectionName());
5863
assert($collection instanceof MongoDBCollection);
5964

6065
try {
@@ -92,25 +97,24 @@ public function setUp(): void
9297
// Wait for the index to be ready
9398
do {
9499
$ready = true;
95-
usleep(10_000);
100+
usleep(100);
96101
foreach ($collection->listSearchIndexes() as $index) {
97-
if ($index['status'] !== 'READY') {
98-
$ready = false;
99-
}
102+
$ready = $ready && $index['queryable'];
100103
}
101104
} while (! $ready);
102105
}
103106

104107
public function tearDown(): void
105108
{
106-
$this->getConnection('mongodb')->getCollection('books')->drop();
109+
$this->getConnection('mongodb')->getCollection($this->getBookCollectionName())->drop();
110+
unset($_SERVER['DB_PREFIX']);
107111

108112
parent::tearDown();
109113
}
110114

111115
public function testGetIndexes()
112116
{
113-
$indexes = Schema::getIndexes('books');
117+
$indexes = Schema::getIndexes($this->getBookCollectionName());
114118

115119
self::assertIsArray($indexes);
116120
self::assertCount(4, $indexes);
@@ -171,7 +175,7 @@ public function testEloquentBuilderSearch()
171175

172176
public function testDatabaseBuilderSearch()
173177
{
174-
$results = $this->getConnection('mongodb')->table('books')
178+
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
175179
->search(Search::text('title', 'systems'), sort: ['title' => 1]);
176180

177181
self::assertInstanceOf(LaravelCollection::class, $results);
@@ -199,7 +203,7 @@ public function testEloquentBuilderAutocomplete()
199203

200204
public function testDatabaseBuilderAutocomplete()
201205
{
202-
$results = $this->getConnection('mongodb')->table('books')
206+
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
203207
->autocomplete('title', 'system');
204208

205209
self::assertInstanceOf(LaravelCollection::class, $results);
@@ -213,7 +217,7 @@ public function testDatabaseBuilderAutocomplete()
213217

214218
public function testDatabaseBuilderVectorSearch()
215219
{
216-
$results = $this->getConnection('mongodb')->table('books')
220+
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
217221
->vectorSearch(
218222
index: 'vector',
219223
path: 'vector4',
@@ -253,6 +257,15 @@ public function testEloquentBuilderVectorSearch()
253257
);
254258
}
255259

260+
private function getBookCollectionName(): string
261+
{
262+
$name = (new Book())->getTable();
263+
264+
self::assertNotEquals('books', $name, 'DB_PREFIX is not set correctly');
265+
266+
return $name;
267+
}
268+
256269
/** Generate random vectors using fixed seed to make tests deterministic */
257270
private function addVector(array $items): array
258271
{

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)