Skip to content

Fix tests on Schema index helpers #3236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
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
80 changes: 46 additions & 34 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Laravel\Schema\Blueprint;
use MongoDB\Model\IndexInfo;

use function assert;
use function collect;
Expand Down Expand Up @@ -81,21 +82,21 @@ public function testIndex(): void
$collection->index('mykey1');
});

$index = $this->getIndex('newcollection', 'mykey1');
$index = $this->getIndex('newcollection', 'mykey1_1');
$this->assertEquals(1, $index['key']['mykey1']);

Schema::table('newcollection', function ($collection) {
$collection->index(['mykey2']);
});

$index = $this->getIndex('newcollection', 'mykey2');
$index = $this->getIndex('newcollection', 'mykey2_1');
$this->assertEquals(1, $index['key']['mykey2']);

Schema::table('newcollection', function ($collection) {
$collection->string('mykey3')->index();
});

$index = $this->getIndex('newcollection', 'mykey3');
$index = $this->getIndex('newcollection', 'mykey3_1');
$this->assertEquals(1, $index['key']['mykey3']);
}

Expand All @@ -105,7 +106,7 @@ public function testPrimary(): void
$collection->string('mykey', 100)->primary();
});

$index = $this->getIndex('newcollection', 'mykey');
$index = $this->getIndex('newcollection', 'mykey_1');
$this->assertEquals(1, $index['unique']);
}

Expand All @@ -115,7 +116,7 @@ public function testUnique(): void
$collection->unique('uniquekey');
});

$index = $this->getIndex('newcollection', 'uniquekey');
$index = $this->getIndex('newcollection', 'uniquekey_1');
$this->assertEquals(1, $index['unique']);
}

Expand All @@ -126,15 +127,15 @@ public function testDropIndex(): void
$collection->dropIndex('uniquekey_1');
});

$index = $this->getIndex('newcollection', 'uniquekey');
$index = $this->getIndex('newcollection', 'uniquekey_1');
$this->assertEquals(null, $index);

Schema::table('newcollection', function ($collection) {
$collection->unique('uniquekey');
$collection->dropIndex(['uniquekey']);
});

$index = $this->getIndex('newcollection', 'uniquekey');
$index = $this->getIndex('newcollection', 'uniquekey_1');
$this->assertEquals(null, $index);

Schema::table('newcollection', function ($collection) {
Expand All @@ -149,35 +150,37 @@ public function testDropIndex(): void
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);
$this->assertNull($index);

$indexName = 'field_a_-1_field_b_1';
Schema::table('newcollection', function ($collection) {
$collection->index(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$index = $this->getIndex('newcollection', $indexName);
$this->assertNotNull($index);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the assertion was always true, $index was false for missing index.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having assertIndexExists() and assertIndexNotExists() would make the tests more readable if you wanted to go that route. I realize you can't get rid of getIndex() entirely, since some later tests expect to make assertions on the IndexInfo structure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, that's more explicit with the new assert methods.


Schema::table('newcollection', function ($collection) {
$collection->dropIndex(['field_a' => -1, 'field_b' => 1]);
});

$index = $this->getIndex('newcollection', 'field_a_-1_field_b_1');
$this->assertFalse($index);
$index = $this->getIndex('newcollection', $indexName);
$this->assertNull($index);

Schema::table('newcollection', function ($collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
$indexName = 'custom_index_name';
Schema::table('newcollection', function ($collection) use ($indexName) {
$collection->index(['field_a', 'field_b'], $indexName);
});

$index = $this->getIndex('newcollection', 'custom_index_name');
$index = $this->getIndex('newcollection', $indexName);
$this->assertNotNull($index);

Schema::table('newcollection', function ($collection) {
$collection->dropIndex('custom_index_name');
Schema::table('newcollection', function ($collection) use ($indexName) {
$collection->dropIndex($indexName);
});

$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertFalse($index);
$index = $this->getIndex('newcollection', $indexName);
$this->assertNull($index);
}

public function testDropIndexIfExists(): void
Expand Down Expand Up @@ -210,7 +213,7 @@ public function testDropIndexIfExists(): void
});

$index = $this->getIndex('newcollection', 'field_a_1_field_b_1');
$this->assertFalse($index);
$this->assertNull($index);

Schema::table('newcollection', function (Blueprint $collection) {
$collection->index(['field_a', 'field_b'], 'custom_index_name');
Expand All @@ -224,7 +227,7 @@ public function testDropIndexIfExists(): void
});

$index = $this->getIndex('newcollection', 'custom_index_name');
$this->assertFalse($index);
$this->assertNull($index);
}

public function testHasIndex(): void
Expand Down Expand Up @@ -256,7 +259,8 @@ public function testSparse(): void
$collection->sparse('sparsekey');
});

$index = $this->getIndex('newcollection', 'sparsekey');
$index = $this->getIndex('newcollection', 'sparsekey_1');
$this->assertNotNull($index);
$this->assertEquals(1, $index['sparse']);
}

Expand All @@ -266,7 +270,8 @@ public function testExpire(): void
$collection->expire('expirekey', 60);
});

$index = $this->getIndex('newcollection', 'expirekey');
$index = $this->getIndex('newcollection', 'expirekey_1');
$this->assertNotNull($index);
$this->assertEquals(60, $index['expireAfterSeconds']);
}

Expand All @@ -280,7 +285,8 @@ public function testSoftDeletes(): void
$collection->string('email')->nullable()->index();
});

$index = $this->getIndex('newcollection', 'email');
$index = $this->getIndex('newcollection', 'email_1');
$this->assertNotNull($index);
$this->assertEquals(1, $index['key']['email']);
}

Expand All @@ -292,10 +298,12 @@ public function testFluent(): void
$collection->timestamp('created_at');
});

$index = $this->getIndex('newcollection', 'email');
$index = $this->getIndex('newcollection', 'email_1');
$this->assertNotNull($index);
$this->assertEquals(1, $index['key']['email']);

$index = $this->getIndex('newcollection', 'token');
$index = $this->getIndex('newcollection', 'token_1');
$this->assertNotNull($index);
$this->assertEquals(1, $index['key']['token']);
}

Expand All @@ -307,13 +315,16 @@ public function testGeospatial(): void
$collection->geospatial('continent', '2dsphere');
});

$index = $this->getIndex('newcollection', 'point');
$index = $this->getIndex('newcollection', 'point_2d');
$this->assertNotNull($index);
$this->assertEquals('2d', $index['key']['point']);

$index = $this->getIndex('newcollection', 'area');
$index = $this->getIndex('newcollection', 'area_2d');
$this->assertNotNull($index);
$this->assertEquals('2d', $index['key']['area']);

$index = $this->getIndex('newcollection', 'continent');
$index = $this->getIndex('newcollection', 'continent_2dsphere');
$this->assertNotNull($index);
$this->assertEquals('2dsphere', $index['key']['continent']);
}

Expand All @@ -332,7 +343,8 @@ public function testSparseUnique(): void
$collection->sparse_and_unique('sparseuniquekey');
});

$index = $this->getIndex('newcollection', 'sparseuniquekey');
$index = $this->getIndex('newcollection', 'sparseuniquekey_1');
$this->assertNotNull($index);
$this->assertEquals(1, $index['sparse']);
$this->assertEquals(1, $index['unique']);
}
Expand Down Expand Up @@ -573,23 +585,23 @@ public function testVectorSearchIndex()
self::assertSame('vector', $index['latestDefinition']['fields'][0]['type']);
}

protected function getIndex(string $collection, string $name)
protected function getIndex(string $collection, string $name): ?IndexInfo
{
$collection = DB::getCollection($collection);
$collection = $this->getConnection('mongodb')->getCollection($collection);
assert($collection instanceof Collection);

foreach ($collection->listIndexes() as $index) {
if (isset($index['key'][$name])) {
if ($index->getName() === $name) {
return $index;
}
}

return false;
return null;
}

protected function getSearchIndex(string $collection, string $name): ?array
{
$collection = DB::getCollection($collection);
$collection = $this->getConnection('mongodb')->getCollection($collection);
assert($collection instanceof Collection);

foreach ($collection->listSearchIndexes(['name' => $name, 'typeMap' => ['root' => 'array', 'array' => 'array', 'document' => 'array']]) as $index) {
Expand Down
Loading