appwrite/tests/e2e/Services/GraphQL/Legacy/DatabaseClientTest.php

483 lines
16 KiB
PHP
Raw Normal View History

2022-06-30 12:00:00 +00:00
<?php
namespace Tests\E2E\Services\GraphQL\Legacy;
2022-06-30 12:00:00 +00:00
2022-07-07 00:53:58 +00:00
use Tests\E2E\Client;
2022-06-30 12:00:00 +00:00
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideClient;
use Tests\E2E\Services\GraphQL\Base;
2023-01-16 09:25:40 +00:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
2022-06-30 12:00:00 +00:00
2022-09-22 08:29:42 +00:00
class DatabaseClientTest extends Scope
2022-06-30 12:00:00 +00:00
{
use ProjectCustom;
use SideClient;
2022-09-22 08:29:42 +00:00
use Base;
2022-07-07 00:53:58 +00:00
public function testCreateDatabase(): array
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_DATABASE);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-09-22 01:53:41 +00:00
'databaseId' => ID::unique(),
2022-07-07 00:53:58 +00: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'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_COLLECTION);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $database['_id'],
'collectionId' => 'actors',
2022-07-07 00:53:58 +00:00
'name' => 'Actors',
2022-09-21 08:17:17 +00:00
'documentSecurity' => false,
'permissions' => [
Permission::read(Role::any()),
Permission::create(Role::users()),
Permission::update(Role::users()),
Permission::delete(Role::users()),
],
2022-07-07 00:53:58 +00:00
]
];
$collection = $this->client->call(Client::METHOD_POST, '/graphql', [
2022-07-07 00:53:58 +00:00
'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']);
2022-07-07 00:53:58 +00:00
return [
'database' => $database,
'collection' => $collection,
2022-07-07 00:53:58 +00:00
];
}
/**
* @depends testCreateCollection
*/
public function testCreateStringAttribute($data): array
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_STRING_ATTRIBUTE);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'collectionId' => $data['collection']['_id'],
2022-07-07 00:53:58 +00: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']);
2022-07-07 00:53:58 +00:00
return $data;
}
/**
* @depends testCreateCollection
*/
public function testCreateIntegerAttribute($data): array
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_INTEGER_ATTRIBUTE);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'collectionId' => $data['collection']['_id'],
2022-07-07 00:53:58 +00: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']);
2022-07-07 00:53:58 +00:00
return $data;
}
/**
* @depends testCreateStringAttribute
* @depends testCreateIntegerAttribute
*/
public function testCreateDocument($data): array
{
sleep(1);
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_DOCUMENT);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'collectionId' => $data['collection']['_id'],
'documentId' => ID::unique(),
2022-07-07 00:53:58 +00:00
'data' => [
'name' => 'John Doe',
'age' => 35,
],
2022-09-21 08:17:17 +00:00
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2022-07-07 00:53:58 +00: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'];
2022-07-07 00:53:58 +00:00
$this->assertIsArray($document);
return [
'database' => $data['database'],
'collection' => $data['collection'],
'document' => $document,
2022-07-07 00:53:58 +00:00
];
}
/**
* @depends testCreateCollection
* @throws \Exception
*/
public function testGetDocuments($data): void
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_DOCUMENTS);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'collectionId' => $data['collection']['_id'],
2022-07-07 00:53:58 +00: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']);
2022-07-07 00:53:58 +00:00
}
/**
* @depends testCreateDocument
* @throws \Exception
*/
public function testGetDocument($data): void
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_DOCUMENT);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'collectionId' => $data['collection']['_id'],
'documentId' => $data['document']['_id'],
2022-07-07 00:53:58 +00: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']);
2022-07-07 00:53:58 +00:00
}
/**
* @depends testCreateDocument
* @throws \Exception
*/
public function testUpdateDocument($data): void
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_DOCUMENT);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'collectionId' => $data['collection']['_id'],
'documentId' => $data['document']['_id'],
2022-07-07 00:53:58 +00: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'];
2022-07-07 00:53:58 +00:00
$this->assertIsArray($document);
2022-12-08 03:08:57 +00:00
2022-07-07 00:53:58 +00:00
$this->assertStringContainsString('New Document Name', $document['data']);
}
/**
* @depends testCreateDocument
* @throws \Exception
*/
public function testDeleteDocument($data): void
{
$projectId = $this->getProject()['$id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_DOCUMENT);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'collectionId' => $data['collection']['_id'],
'documentId' => $data['document']['_id'],
2022-07-07 00:53:58 +00: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 09:38:13 +00:00
$this->assertIsNotArray($document['body']);
2022-07-13 07:45:54 +00:00
$this->assertEquals(204, $document['headers']['status-code']);
2022-07-07 00:53:58 +00:00
}
2025-06-13 08:34:23 +00:00
/**
* @throws \Exception
*/
public function testBulkCreateDocuments(): array
{
$project = $this->getProject();
$projectId = $project['$id'];
$headers = [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $project['apiKey'],
];
// Step 1: Create database
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_DATABASE);
2025-06-13 08:34:23 +00:00
$payload = [
'query' => $query,
'variables' => [
'databaseId' => 'bulk',
'name' => 'Bulk',
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
$databaseId = $res['body']['data']['databasesCreate']['_id'];
// Step 2: Create collection
2025-08-19 11:14:36 +00:00
$query = $this->getQuery(self::CREATE_COLLECTION);
2025-06-13 08:34:23 +00:00
$payload['query'] = $query;
$payload['variables'] = [
'databaseId' => $databaseId,
'collectionId' => 'operations',
'name' => 'Operations',
'documentSecurity' => false,
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
$collectionId = $res['body']['data']['databasesCreateCollection']['_id'];
// Step 3: Create attribute
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_STRING_ATTRIBUTE);
2025-06-13 08:34:23 +00:00
$payload['query'] = $query;
$payload['variables'] = [
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'key' => 'name',
'size' => 256,
'required' => true,
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
sleep(1);
// Step 4: Create documents
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_DOCUMENTS);
2025-06-13 08:34:23 +00:00
$documents = [];
for ($i = 1; $i <= 10; $i++) {
$documents[] = ['$id' => 'doc' . $i, 'name' => 'Doc #' . $i];
}
$payload['query'] = $query;
$payload['variables'] = [
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'documents' => $documents,
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
$this->assertCount(10, $res['body']['data']['databasesCreateDocuments']['documents']);
2025-06-13 08:34:23 +00:00
return [
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'projectId' => $projectId,
];
}
/**
* @depends testBulkCreateDocuments
*/
public function testBulkUpdateDocuments(array $data): array
{
$userId = $this->getUser()['$id'];
$permissions = [
Permission::read(Role::user($userId)),
Permission::update(Role::user($userId)),
Permission::delete(Role::user($userId)),
];
$headers = [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
'x-appwrite-key' => $this->getProject()['apiKey'],
];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_DOCUMENTS);
2025-06-13 08:34:23 +00:00
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'collectionId' => $data['collectionId'],
'data' => [
'name' => 'Docs Updated',
'$permissions' => $permissions,
],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
$this->assertCount(10, $res['body']['data']['databasesUpdateDocuments']['documents']);
2025-06-13 08:34:23 +00:00
return $data;
}
/**
* @depends testBulkUpdateDocuments
*/
public function testBulkUpsertDocuments(array $data): array
{
$headers = [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
'x-appwrite-key' => $this->getProject()['apiKey'],
];
// Upsert: Update one, insert one
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPSERT_DOCUMENTS);
2025-06-13 08:34:23 +00:00
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'collectionId' => $data['collectionId'],
'documents' => [
['$id' => 'doc10', 'name' => 'Doc #1000'],
['name' => 'Doc #11'],
],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
$this->assertCount(2, $res['body']['data']['databasesUpsertDocuments']['documents']);
2025-06-13 08:34:23 +00:00
return $data;
}
/**
* @depends testBulkUpsertDocuments
*/
public function testBulkDeleteDocuments(array $data): array
{
$headers = [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
'x-appwrite-key' => $this->getProject()['apiKey'],
];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::DELETE_DOCUMENTS);
2025-06-13 08:34:23 +00:00
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'collectionId' => $data['collectionId'],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
$this->assertCount(11, $res['body']['data']['databasesDeleteDocuments']['documents']);
2025-06-13 08:34:23 +00:00
return $data;
}
2022-07-06 03:51:37 +00:00
}