Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5132,8 +5132,8 @@ public function increaseDocumentAttribute(

/** @var Document $attr */
$attr = \end($attr);
if (!in_array($attr->getAttribute('type'), $whiteList)) {
throw new TypeException('Attribute type must be one of: ' . implode(',', $whiteList));
if (!\in_array($attr->getAttribute('type'), $whiteList) || $attr->getAttribute('array')) {
throw new TypeException('Attribute must be an integer or float and can not be an array.');
}

$document = $this->withTransaction(function () use ($collection, $id, $attribute, $value, $max) {
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/Adapter/Scopes/AttributeTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ function (mixed $value) {
try {
$queries = [Query::equal('title', ['test'])];
$database->find($col->getId(), $queries);
} catch (Throwable $e) {
} catch (Throwable) {
$this->fail('Should not have thrown error');
}
}
Expand Down
36 changes: 29 additions & 7 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Utopia\Database\Exception\Conflict as ConflictException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Exception\Type as TypeException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
Expand Down Expand Up @@ -550,7 +551,7 @@ public function testUpsertDocumentsInc(): void

$database->createOrUpdateDocumentsWithIncrease(
collection: __FUNCTION__,
attribute:'integer',
attribute: 'integer',
documents: $documents
);

Expand All @@ -565,7 +566,7 @@ public function testUpsertDocumentsInc(): void

$database->createOrUpdateDocumentsWithIncrease(
collection: __FUNCTION__,
attribute:'integer',
attribute: 'integer',
documents: $documents
);

Expand Down Expand Up @@ -964,6 +965,7 @@ public function testCreateDocumentDefaults(): void
// cleanup collection
$database->deleteCollection('defaults');
}

public function testIncreaseDecrease(): Document
{
/** @var Database $database */
Expand All @@ -976,12 +978,14 @@ public function testIncreaseDecrease(): Document
$this->assertEquals(true, $database->createAttribute($collection, 'decrease', Database::VAR_INTEGER, 0, true));
$this->assertEquals(true, $database->createAttribute($collection, 'increase_text', Database::VAR_STRING, 255, true));
$this->assertEquals(true, $database->createAttribute($collection, 'increase_float', Database::VAR_FLOAT, 0, true));
$this->assertEquals(true, $database->createAttribute($collection, 'sizes', Database::VAR_INTEGER, 8, required: false, array: true));

$document = $database->createDocument($collection, new Document([
'increase' => 100,
'decrease' => 100,
'increase_float' => 100,
'increase_text' => 'some text',
'sizes' => [10, 20, 30],
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Expand Down Expand Up @@ -1016,6 +1020,7 @@ public function testIncreaseDecrease(): Document

return $document;
}

/**
* @depends testIncreaseDecrease
*/
Expand All @@ -1040,7 +1045,6 @@ public function testDecreaseLimitMin(Document $document): void
$this->assertEquals(false, $database->decreaseDocumentAttribute('increase_decrease', $document->getId(), 'decrease', 10, 99));
}


/**
* @depends testIncreaseDecrease
*/
Expand All @@ -1049,8 +1053,28 @@ public function testIncreaseTextAttribute(Document $document): void
/** @var Database $database */
$database = static::getDatabase();

$this->expectException(Exception::class);
$this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text'));
try {
$this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text'));
$this->fail('Expected TypeException not thrown');
} catch (Exception $e) {
$this->assertInstanceOf(TypeException::class, $e, $e->getMessage());
}
}

/**
* @depends testIncreaseDecrease
*/
public function testIncreaseArrayAttribute(Document $document): void
{
/** @var Database $database */
$database = static::getDatabase();

try {
$this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'sizes'));
$this->fail('Expected TypeException not thrown');
} catch (Exception $e) {
$this->assertInstanceOf(TypeException::class, $e);
}
}

/**
Expand Down Expand Up @@ -1081,8 +1105,6 @@ public function testGetDocument(Document $document): Document
return $document;
}



/**
* @depends testCreateDocument
*/
Expand Down
Loading