Skip to content

Commit fad0b6b

Browse files
committed
test(indexes): cover unique index creation over duplicate rows on every adapter
Each adapter must reject a unique index on a string or integer attribute whose rows already share a value with UniqueException, and leave the index out of the collection metadata.
1 parent ceab24e commit fad0b6b

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎tests/e2e/Adapter/Scopes/IndexTests.php‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Utopia\Database\Exception\Duplicate as DuplicateException;
1111
use Utopia\Database\Exception\Limit as LimitException;
1212
use Utopia\Database\Exception\Query as QueryException;
13+
use Utopia\Database\Exception\Unique as UniqueException;
1314
use Utopia\Database\Helpers\ID;
1415
use Utopia\Database\Helpers\Permission;
1516
use Utopia\Database\Helpers\Role;
@@ -511,6 +512,46 @@ public function testCreateIndexOnSequence(): void
511512
$database->deleteCollection(__FUNCTION__);
512513
}
513514

515+
public function testCreateUniqueIndexOverDuplicates(): void
516+
{
517+
/** @var Database $database */
518+
$database = $this->getDatabase();
519+
520+
if (!$database->getAdapter()->getSupportForUniqueIndex()) {
521+
$this->expectNotToPerformAssertions();
522+
return;
523+
}
524+
525+
$database->createCollection(__FUNCTION__);
526+
$database->createAttribute(__FUNCTION__, 'name', Database::VAR_STRING, 128, false);
527+
$database->createAttribute(__FUNCTION__, 'age', Database::VAR_INTEGER, 0, false);
528+
529+
foreach (['first', 'second'] as $id) {
530+
$database->createDocument(__FUNCTION__, new Document([
531+
'$id' => $id,
532+
'$permissions' => [
533+
Permission::read(Role::any()),
534+
],
535+
'name' => 'chester',
536+
'age' => 7,
537+
]));
538+
}
539+
540+
foreach (['name', 'age'] as $attribute) {
541+
try {
542+
$database->createIndex(__FUNCTION__, "unique_{$attribute}", Database::INDEX_UNIQUE, [$attribute]);
543+
$this->fail('Failed to throw exception');
544+
} catch (Exception $e) {
545+
$this->assertInstanceOf(UniqueException::class, $e);
546+
}
547+
548+
$indexes = $database->getCollection(__FUNCTION__)->getAttribute('indexes');
549+
$this->assertNotContains("unique_{$attribute}", \array_map(fn ($index) => $index->getId(), $indexes));
550+
}
551+
552+
$database->deleteCollection(__FUNCTION__);
553+
}
554+
514555
public function testIndexLengthZero(): void
515556
{
516557
/** @var Database $database */

0 commit comments

Comments
 (0)