Skip to content

Commit 686e1e0

Browse files
committed
Revert to only wait for the previous search indexes to be dropped
1 parent f4c2e44 commit 686e1e0

File tree

3 files changed

+17
-34
lines changed

3 files changed

+17
-34
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ protected function setUp(): void
5252
]));
5353

5454
// Waits for the search index created in the previous test to be deleted
55-
do {
56-
usleep(1_000);
57-
} while ($moviesCollection->listSearchIndexes()->count());
55+
while ($moviesCollection->listSearchIndexes()->count()) {
56+
usleep(1000);
57+
}
5858

5959
try {
6060
$moviesCollection->createSearchIndex([

tests/AtlasSearchTest.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ 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-
3632
parent::setUp();
3733

34+
$collection = $this->getConnection('mongodb')->getCollection('books');
35+
assert($collection instanceof MongoDBCollection);
36+
$collection->drop();
37+
3838
Book::insert($this->addVector([
3939
['title' => 'Introduction to Algorithms'],
4040
['title' => 'Clean Code: A Handbook of Agile Software Craftsmanship'],
@@ -58,10 +58,12 @@ public function setUp(): void
5858
['title' => 'Pattern Recognition and Machine Learning'],
5959
]));
6060

61-
$collection = $this->getConnection('mongodb')->getCollection($this->getBookCollectionName());
62-
assert($collection instanceof MongoDBCollection);
63-
6461
try {
62+
// Waits for the search index created in the previous test to be deleted
63+
while ($collection->listSearchIndexes()->count()) {
64+
usleep(1000);
65+
}
66+
6567
$collection->createSearchIndex([
6668
'mappings' => [
6769
'fields' => [
@@ -105,15 +107,14 @@ public function setUp(): void
105107

106108
public function tearDown(): void
107109
{
108-
$this->getConnection('mongodb')->getCollection($this->getBookCollectionName())->drop();
109-
unset($_SERVER['DB_PREFIX']);
110+
$this->getConnection('mongodb')->getCollection('books')->drop();
110111

111112
parent::tearDown();
112113
}
113114

114115
public function testGetIndexes()
115116
{
116-
$indexes = Schema::getIndexes($this->getBookCollectionName());
117+
$indexes = Schema::getIndexes('books');
117118

118119
self::assertIsArray($indexes);
119120
self::assertCount(4, $indexes);
@@ -174,7 +175,7 @@ public function testEloquentBuilderSearch()
174175

175176
public function testDatabaseBuilderSearch()
176177
{
177-
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
178+
$results = $this->getConnection('mongodb')->table('books')
178179
->search(Search::text('title', 'systems'), sort: ['title' => 1]);
179180

180181
self::assertInstanceOf(LaravelCollection::class, $results);
@@ -202,7 +203,7 @@ public function testEloquentBuilderAutocomplete()
202203

203204
public function testDatabaseBuilderAutocomplete()
204205
{
205-
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
206+
$results = $this->getConnection('mongodb')->table('books')
206207
->autocomplete('title', 'system');
207208

208209
self::assertInstanceOf(LaravelCollection::class, $results);
@@ -216,7 +217,7 @@ public function testDatabaseBuilderAutocomplete()
216217

217218
public function testDatabaseBuilderVectorSearch()
218219
{
219-
$results = $this->getConnection('mongodb')->table($this->getBookCollectionName())
220+
$results = $this->getConnection('mongodb')->table('books')
220221
->vectorSearch(
221222
index: 'vector',
222223
path: 'vector4',
@@ -256,15 +257,6 @@ public function testEloquentBuilderVectorSearch()
256257
);
257258
}
258259

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

tests/Models/Book.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ 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-
3526
public function author(): BelongsTo
3627
{
3728
return $this->belongsTo(User::class, 'author_id');

0 commit comments

Comments
 (0)