2022-07-01 00:00:00 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\E2E\Services\GraphQL;
|
|
|
|
|
2022-07-07 12:53:58 +12:00
|
|
|
use Tests\E2E\Client;
|
2022-07-01 00:00:00 +12:00
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
|
|
|
use Tests\E2E\Scopes\Scope;
|
|
|
|
use Tests\E2E\Scopes\SideClient;
|
2023-01-16 22:25:40 +13:00
|
|
|
use Utopia\Database\Helpers\ID;
|
|
|
|
use Utopia\Database\Helpers\Permission;
|
|
|
|
use Utopia\Database\Helpers\Role;
|
2022-07-01 00:00:00 +12:00
|
|
|
|
2022-09-22 20:29:42 +12:00
|
|
|
class DatabaseClientTest extends Scope
|
2022-07-01 00:00:00 +12:00
|
|
|
{
|
|
|
|
use ProjectCustom;
|
|
|
|
use SideClient;
|
2022-09-22 20:29:42 +12:00
|
|
|
use Base;
|
2022-07-07 12:53:58 +12:00
|
|
|
|
|
|
|
public function testCreateDatabase(): array
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$CREATE_DATABASE);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-09-22 13:53:41 +12:00
|
|
|
'databaseId' => ID::unique(),
|
2022-07-07 12:53:58 +12:00
|
|
|
'name' => 'Actors',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
], $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($database['body']['data']);
|
|
|
|
$this->assertArrayNotHasKey('errors', $database['body']);
|
|
|
|
$database = $database['body']['data']['databasesCreate'];
|
|
|
|
$this->assertEquals('Actors', $database['name']);
|
|
|
|
|
|
|
|
return $database;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateDatabase
|
|
|
|
*/
|
|
|
|
public function testCreateCollection($database): array
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$CREATE_COLLECTION);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $database['_id'],
|
2022-07-07 12:53:58 +12:00
|
|
|
'collectionId' => 'actors',
|
|
|
|
'name' => 'Actors',
|
2022-09-21 20:17:17 +12:00
|
|
|
'documentSecurity' => false,
|
|
|
|
'permissions' => [
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::create(Role::users()),
|
|
|
|
Permission::update(Role::users()),
|
|
|
|
Permission::delete(Role::users()),
|
|
|
|
],
|
2022-07-07 12:53:58 +12:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$collection = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
], $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertIsArray($collection['body']['data']);
|
|
|
|
$this->assertArrayNotHasKey('errors', $collection['body']);
|
|
|
|
$collection = $collection['body']['data']['databasesCreateCollection'];
|
|
|
|
$this->assertEquals('Actors', $collection['name']);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'database' => $database,
|
|
|
|
'collection' => $collection,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
|
|
|
public function testCreateStringAttribute($data): array
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$CREATE_STRING_ATTRIBUTE);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $data['database']['_id'],
|
|
|
|
'collectionId' => $data['collection']['_id'],
|
2022-07-07 12:53:58 +12:00
|
|
|
'key' => 'name',
|
|
|
|
'size' => 256,
|
|
|
|
'required' => true,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$attribute = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
], $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('errors', $attribute['body']);
|
|
|
|
$this->assertIsArray($attribute['body']['data']);
|
|
|
|
$this->assertIsArray($attribute['body']['data']['databasesCreateStringAttribute']);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
*/
|
|
|
|
public function testCreateIntegerAttribute($data): array
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$CREATE_INTEGER_ATTRIBUTE);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $data['database']['_id'],
|
|
|
|
'collectionId' => $data['collection']['_id'],
|
2022-07-07 12:53:58 +12:00
|
|
|
'key' => 'age',
|
|
|
|
'min' => 18,
|
|
|
|
'max' => 150,
|
|
|
|
'required' => true,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$attribute = $this->client->call(Client::METHOD_POST, '/graphql', [
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey'],
|
|
|
|
], $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('errors', $attribute['body']);
|
|
|
|
$this->assertIsArray($attribute['body']['data']);
|
|
|
|
$this->assertIsArray($attribute['body']['data']['databasesCreateIntegerAttribute']);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateStringAttribute
|
|
|
|
* @depends testCreateIntegerAttribute
|
|
|
|
*/
|
|
|
|
public function testCreateDocument($data): array
|
|
|
|
{
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$CREATE_DOCUMENT);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $data['database']['_id'],
|
|
|
|
'collectionId' => $data['collection']['_id'],
|
2022-09-22 13:53:41 +12:00
|
|
|
'documentId' => ID::unique(),
|
2022-07-07 12:53:58 +12:00
|
|
|
'data' => [
|
|
|
|
'name' => 'John Doe',
|
|
|
|
'age' => 35,
|
|
|
|
],
|
2022-09-21 20:17:17 +12:00
|
|
|
'permissions' => [
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
],
|
2022-07-07 12:53:58 +12:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
|
|
|
$this->assertIsArray($document['body']['data']);
|
|
|
|
|
|
|
|
$document = $document['body']['data']['databasesCreateDocument'];
|
|
|
|
$this->assertIsArray($document);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'database' => $data['database'],
|
|
|
|
'collection' => $data['collection'],
|
|
|
|
'document' => $document,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateCollection
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testGetDocuments($data): void
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$GET_DOCUMENTS);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $data['database']['_id'],
|
|
|
|
'collectionId' => $data['collection']['_id'],
|
2022-07-07 12:53:58 +12:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$documents = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('errors', $documents['body']);
|
|
|
|
$this->assertIsArray($documents['body']['data']);
|
|
|
|
$this->assertIsArray($documents['body']['data']['databasesListDocuments']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateDocument
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testGetDocument($data): void
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$GET_DOCUMENT);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $data['database']['_id'],
|
|
|
|
'collectionId' => $data['collection']['_id'],
|
|
|
|
'documentId' => $data['document']['_id'],
|
2022-07-07 12:53:58 +12:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
|
|
|
$this->assertIsArray($document['body']['data']);
|
|
|
|
$this->assertIsArray($document['body']['data']['databasesGetDocument']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateDocument
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testUpdateDocument($data): void
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$UPDATE_DOCUMENT);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $data['database']['_id'],
|
|
|
|
'collectionId' => $data['collection']['_id'],
|
|
|
|
'documentId' => $data['document']['_id'],
|
2022-07-07 12:53:58 +12:00
|
|
|
'data' => [
|
|
|
|
'name' => 'New Document Name',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('errors', $document['body']);
|
|
|
|
$this->assertIsArray($document['body']['data']);
|
|
|
|
$document = $document['body']['data']['databasesUpdateDocument'];
|
|
|
|
$this->assertIsArray($document);
|
2022-12-08 16:08:57 +13:00
|
|
|
|
2022-07-07 12:53:58 +12:00
|
|
|
$this->assertStringContainsString('New Document Name', $document['data']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCreateDocument
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testDeleteDocument($data): void
|
|
|
|
{
|
|
|
|
$projectId = $this->getProject()['$id'];
|
|
|
|
$query = $this->getQuery(self::$DELETE_DOCUMENT);
|
|
|
|
$gqlPayload = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => [
|
2022-12-08 16:08:57 +13:00
|
|
|
'databaseId' => $data['database']['_id'],
|
|
|
|
'collectionId' => $data['collection']['_id'],
|
|
|
|
'documentId' => $data['document']['_id'],
|
2022-07-07 12:53:58 +12:00
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$document = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
|
|
|
|
'content-type' => 'application/json',
|
|
|
|
'x-appwrite-project' => $projectId,
|
|
|
|
], $this->getHeaders()), $gqlPayload);
|
|
|
|
|
2022-07-18 21:38:13 +12:00
|
|
|
$this->assertIsNotArray($document['body']);
|
2022-07-13 19:45:54 +12:00
|
|
|
$this->assertEquals(204, $document['headers']['status-code']);
|
2022-07-07 12:53:58 +12:00
|
|
|
}
|
2022-07-06 15:51:37 +12:00
|
|
|
}
|