Skip to content

Commit

Permalink
fix: boolean attribute default value
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Sep 30, 2022
1 parent 7880add commit d45464c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/databases.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ function createAttribute(string $databaseId, string $collectionId, Document $att
}

// Must throw here since dbForProject->createAttribute is performed by db worker
if ($required && $default) {
if ($required && isset($default)) {
throw new Exception(Exception::ATTRIBUTE_DEFAULT_UNSUPPORTED, 'Cannot set default value for required attribute');
}

if ($array && $default) {
if ($array && isset($default)) {
throw new Exception(Exception::ATTRIBUTE_DEFAULT_UNSUPPORTED, 'Cannot set default value for array attributes');
}

Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions tests/e2e/Services/Databases/DatabasesBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2968,4 +2968,52 @@ public function testUpdatePermissionsWithEmptyPayload(): array

return [];
}

/**
* @depends testCreateDatabase
*/
public function testAttributeBooleanDefault(array $data): void
{
$databaseId = $data['databaseId'];

/**
* Test for SUCCESS
*/
$collection = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'collectionId' => ID::unique(),
'name' => 'Boolean'
]);

$this->assertEquals(201, $collection['headers']['status-code']);

$collectionId = $collection['body']['$id'];

$true = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/boolean', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'true',
'required' => false,
'default' => true
]);

$this->assertEquals(202, $true['headers']['status-code']);

$false = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/boolean', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'false',
'required' => false,
'default' => false
]);

$this->assertEquals(202, $false['headers']['status-code']);
}
}

0 comments on commit d45464c

Please sign in to comment.