1
0
Fork 0
mirror of synced 2024-09-29 17:01:37 +13:00

Some tests

This commit is contained in:
fogelito 2023-03-19 19:09:17 +02:00
parent 038f4fc073
commit eee29940bb
5 changed files with 105 additions and 31 deletions

View file

@ -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)

View file

@ -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');

View file

@ -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

View file

@ -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
};

View file

@ -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 [];
}