From eee29940bb665cca2f0c0d8a6ef9cc4f4abf16b8 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 19 Mar 2023 19:09:17 +0200 Subject: [PATCH] Some tests --- app/controllers/api/databases.php | 17 ++-- app/workers/databases.php | 15 +-- docker-compose.yml | 4 +- .../Database/Validator/Query/Filter.php | 1 + .../e2e/Services/Databases/DatabasesBase.php | 99 ++++++++++++++++--- 5 files changed, 105 insertions(+), 31 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 02d40561e4..31a4cfd53d 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1558,7 +1558,8 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/relati 'twoWay' => $twoWay, 'twoWayKey' => $twoWayKey, 'onUpdate' => $onUpdate, - 'onDelete' => $onDelete + 'onDelete' => $onDelete, + 'id' => $key ] ]), $response, @@ -1568,13 +1569,13 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/relati ); $options = $attribute->getAttribute('options', []); - $attribute->setAttribute('relatedCollection', $options['relatedCollection'] || null); - $attribute->setAttribute('relationType', $options['relationType'] || null); - $attribute->setAttribute('twoWay', $options['twoWay'] || null); - $attribute->setAttribute('twoWayKey', $options['twoWayKey'] || null); - $attribute->setAttribute('onUpdate', $options['onUpdate'] || null); - $attribute->setAttribute('onDelete', $options['onDelete'] || null); - $attribute->setAttribute('side', $options['side'] || null); + $attribute->setAttribute('relatedCollection', $options['relatedCollection'] ?? null); + $attribute->setAttribute('relationType', $options['relationType'] ?? null); + $attribute->setAttribute('twoWay', $options['twoWay'] ?? null); + $attribute->setAttribute('twoWayKey', $options['twoWayKey'] ?? null); + $attribute->setAttribute('onUpdate', $options['onUpdate'] ?? null); + $attribute->setAttribute('onDelete', $options['onDelete'] ?? null); + $attribute->setAttribute('side', $options['side'] ?? null); $response ->setStatusCode(Response::STATUS_CODE_ACCEPTED) diff --git a/app/workers/databases.php b/app/workers/databases.php index 667bdf9aa0..20d9fd3f1d 100644 --- a/app/workers/databases.php +++ b/app/workers/databases.php @@ -97,16 +97,19 @@ class DatabaseV1 extends Worker try { if ($type === Database::VAR_RELATIONSHIP) { $relatedCollection = $dbForProject->getDocument('database_' . $database->getInternalId(), $options['relatedCollection']); + if ($relatedCollection->isEmpty()) { + throw new Exception('Missing collection'); + } if ( !$dbForProject->createRelationship( collection: 'database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), relatedCollection: 'database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId(), - type: $options['relationType'] || null, - twoWay: $options['twoWay'] || null, - id: $options['id'] || null, - twoWayKey: $options['twoWayKey'] || null, - onUpdate: $options['onUpdate'] || null, - onDelete: $options['onDelete'] || null, + type: $options['relationType'], + twoWay: $options['twoWay'], + id: $options['id'], + twoWayKey: $options['twoWayKey'], + onUpdate: $options['onUpdate'], + onDelete: $options['onDelete'], ) ) { throw new Exception('Failed to create Attribute'); diff --git a/docker-compose.yml b/docker-compose.yml index f846d1cc6d..8eb5862d8d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -345,8 +345,8 @@ services: volumes: - ./app:/usr/src/code/app - ./src:/usr/src/code/src - #- ./vendor/utopia-php/database:/usr/src/code/vendor/utopia-php/database - #- ./vendor/utopia-php/framework:/usr/src/code/vendor/utopia-php/framework + - ./vendor/utopia-php/database:/usr/src/code/vendor/utopia-php/database + - ./vendor/utopia-php/framework:/usr/src/code/vendor/utopia-php/framework depends_on: - redis - mariadb diff --git a/src/Appwrite/Utopia/Database/Validator/Query/Filter.php b/src/Appwrite/Utopia/Database/Validator/Query/Filter.php index 096d036907..bbb4424702 100644 --- a/src/Appwrite/Utopia/Database/Validator/Query/Filter.php +++ b/src/Appwrite/Utopia/Database/Validator/Query/Filter.php @@ -61,6 +61,7 @@ class Filter extends Base foreach ($values as $value) { $condition = match ($attributeType) { + Database::VAR_RELATIONSHIP => true, // Todo: ? Database::VAR_DATETIME => gettype($value) === Database::VAR_STRING, default => gettype($value) === $attributeType }; diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index bcb9200a6d..3710be1fa4 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -6,9 +6,11 @@ use Appwrite\Extend\Exception; use Tests\E2E\Client; use Utopia\Database\Database; use Utopia\Database\DateTime; +use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; +use Utopia\Database\Query; use Utopia\Database\Validator\Datetime as DatetimeValidator; trait DatabasesBase @@ -233,9 +235,6 @@ trait DatabasesBase return $data; } - - - /** * @depends testCreateAttributes */ @@ -251,12 +250,15 @@ trait DatabasesBase 'name' => 'person', 'permissions' => [ Permission::read(Role::user($this->getUser()['$id'])), - //Permission::update(Role::user($this->getUser()['$id'])), - // Permission::delete(Role::user($this->getUser()['$id'])), + Permission::update(Role::user($this->getUser()['$id'])), + Permission::delete(Role::user($this->getUser()['$id'])), + Permission::create(Role::user($this->getUser()['$id'])), ], 'documentSecurity' => true, ]); + $this->assertEquals(201, $person['headers']['status-code']); + $library = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -266,44 +268,111 @@ trait DatabasesBase 'name' => 'library', 'permissions' => [ Permission::read(Role::user($this->getUser()['$id'])), - //Permission::update(Role::user($this->getUser()['$id'])), - // Permission::delete(Role::user($this->getUser()['$id'])), + Permission::update(Role::user($this->getUser()['$id'])), + Permission::create(Role::user($this->getUser()['$id'])), ], 'documentSecurity' => true, ]); + $this->assertEquals(201, $library['headers']['status-code']); + $relation = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/attributes/relationship', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ - 'key' => 'library', + 'key' => 'libraryId', 'relatedCollectionId' => 'library', 'type' => Database::RELATION_ONE_TO_ONE, + 'onUpdate' => 'cascade', ]); - $name = $this->client->call(Client::METHOD_POST, '/databases/' . $library['body']['$id'] . '/string', array_merge([ + $libraryName = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $library['body']['$id'] . '/attributes/string', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ - 'key' => 'name', + 'key' => 'libraryName', 'size' => 255, 'required' => true, ]); - $area = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/string', array_merge([ + sleep(1); // Wait for worker + + $this->assertEquals(202, $libraryName['headers']['status-code']); + $this->assertEquals(202, $relation['headers']['status-code']); + $this->assertEquals('libraryId', $relation['body']['key']); + $this->assertEquals('relationship', $relation['body']['type']); + $this->assertEquals('processing', $relation['body']['status']); + + $attribute = $this->client->call(Client::METHOD_GET, "/databases/{$databaseId}/collections/{$person['body']['$id']}/attributes/libraryId", array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ - 'key' => 'area', - 'size' => 255, - 'required' => true, + ])); + + $this->assertEquals(200, $attribute['headers']['status-code']); + $this->assertEquals('available', $attribute['body']['status']); + $this->assertEquals('libraryId', $attribute['body']['key']); + $this->assertEquals('relationship', $attribute['body']['type']); + $this->assertEquals(false, $attribute['body']['required']); + $this->assertEquals(false, $attribute['body']['array']); + $this->assertEquals('oneToOne', $attribute['body']['options']['relationType']); + $this->assertEquals(false, $attribute['body']['options']['twoWay']); + $this->assertEquals('cascade', $attribute['body']['options']['onUpdate']); + $this->assertEquals('restrict', $attribute['body']['options']['onDelete']); + $this->assertEquals('libraryId', $attribute['body']['options']['id']); + + $person1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'documentId' => ID::unique(), + 'data' => [ + 'libraryId' => [ + '$id' => 'library1', + '$permissions' => [ + Permission::read(Role::any()), + ], + 'libraryName' => 'Library 1', + ], + ], + 'permissions' => [ + Permission::read(Role::user($this->getUser()['$id'])), + Permission::update(Role::user($this->getUser()['$id'])), + Permission::delete(Role::user($this->getUser()['$id'])), + ] ]); + $this->assertEquals('library1', $person1['body']['libraryId']); + + $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => ['equal("libraryId", "library1")'], + ]); + + $this->assertEquals(1, $documents['body']['total']); + $this->assertEquals('Library 1', $documents['body']['documents'][0]['libraryId']['libraryName']); + + +// $documents = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $person['body']['$id'] . '/documents', array_merge([ +// 'content-type' => 'application/json', +// 'x-appwrite-project' => $this->getProject()['$id'], +// ], $this->getHeaders()), [ +// 'queries' => ['equal("library.libraryName", "Library 1")'], +// ]); +// +// var_dump($documents); + + + die; + + + return []; }