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

590 lines
19 KiB
PHP
Raw Normal View History

2022-06-30 12:00:00 +00:00
<?php
2025-07-27 09:18:19 +00:00
namespace Tests\E2E\Services\GraphQL\Grids;
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;
2025-06-13 08:34:23 +00:00
use Utopia\Database\Query;
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-07-27 09:53:07 +00:00
$query = $this->getQuery(self::$GRIDS_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']);
2025-07-27 09:53:07 +00:00
$database = $database['body']['data']['gridsCreateDatabase'];
2022-07-07 00:53:58 +00:00
$this->assertEquals('Actors', $database['name']);
return $database;
}
/**
* @depends testCreateDatabase
*/
public function testCreateTable($database): array
2022-07-07 00:53:58 +00:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_TABLE);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $database['_id'],
'tableId' => 'actors',
2022-07-07 00:53:58 +00:00
'name' => 'Actors',
'rowSecurity' => false,
2022-09-21 08:17:17 +00:00
'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
]
];
$table = $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($table['body']['data']);
$this->assertArrayNotHasKey('errors', $table['body']);
2025-07-27 09:53:07 +00:00
$table = $table['body']['data']['gridsCreateTable'];
$this->assertEquals('Actors', $table['name']);
2022-07-07 00:53:58 +00:00
return [
'table' => $table,
2022-07-07 00:53:58 +00:00
'database' => $database,
];
}
/**
* @depends testCreateTable
2022-07-07 00:53:58 +00:00
*/
public function testCreateStringColumn($data): array
2022-07-07 00:53:58 +00:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_STRING_COLUMN);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'tableId' => $data['table']['_id'],
2022-07-07 00:53:58 +00:00
'key' => 'name',
'size' => 256,
'required' => true,
]
];
$column = $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->assertArrayNotHasKey('errors', $column['body']);
$this->assertIsArray($column['body']['data']);
2025-07-27 09:53:07 +00:00
$this->assertIsArray($column['body']['data']['gridsCreateStringColumn']);
2022-07-07 00:53:58 +00:00
return $data;
}
/**
* @depends testCreateTable
2022-07-07 00:53:58 +00:00
*/
public function testCreateIntegerColumn($data): array
2022-07-07 00:53:58 +00:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_INTEGER_COLUMN);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'tableId' => $data['table']['_id'],
2022-07-07 00:53:58 +00:00
'key' => 'age',
'min' => 18,
'max' => 150,
'required' => true,
]
];
$column = $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->assertArrayNotHasKey('errors', $column['body']);
$this->assertIsArray($column['body']['data']);
2025-07-27 09:53:07 +00:00
$this->assertIsArray($column['body']['data']['gridsCreateIntegerColumn']);
2022-07-07 00:53:58 +00:00
return $data;
}
/**
* @depends testCreateStringColumn
* @depends testCreateIntegerColumn
2022-07-07 00:53:58 +00:00
*/
public function testCreateRow($data): array
2022-07-07 00:53:58 +00:00
{
sleep(1);
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_ROW);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'tableId' => $data['table']['_id'],
'rowId' => 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
]
];
$row = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
2022-07-07 00:53:58 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertArrayNotHasKey('errors', $row['body']);
$this->assertIsArray($row['body']['data']);
2022-07-07 00:53:58 +00:00
2025-07-27 09:53:07 +00:00
$row = $row['body']['data']['gridsCreateRow'];
$this->assertIsArray($row);
2022-07-07 00:53:58 +00:00
return [
'database' => $data['database'],
'table' => $data['table'],
'row' => $row,
2022-07-07 00:53:58 +00:00
];
}
/**
* @depends testCreateTable
2022-07-07 00:53:58 +00:00
* @throws \Exception
*/
public function testGetRows($data): void
2022-07-07 00:53:58 +00:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_ROWS);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'tableId' => $data['table']['_id'],
2022-07-07 00:53:58 +00:00
]
];
$rows = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
2022-07-07 00:53:58 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertArrayNotHasKey('errors', $rows['body']);
$this->assertIsArray($rows['body']['data']);
2025-07-27 09:53:07 +00:00
$this->assertIsArray($rows['body']['data']['gridsListRows']);
2022-07-07 00:53:58 +00:00
}
/**
* @depends testCreateRow
2022-07-07 00:53:58 +00:00
* @throws \Exception
*/
public function testGetDocument($data): void
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$GET_ROW);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'tableId' => $data['table']['_id'],
'rowId' => $data['row']['_id'],
2022-07-07 00:53:58 +00:00
]
];
$row = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
2022-07-07 00:53:58 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertArrayNotHasKey('errors', $row['body']);
$this->assertIsArray($row['body']['data']);
2025-07-27 09:53:07 +00:00
$this->assertIsArray($row['body']['data']['gridsGetRow']);
2022-07-07 00:53:58 +00:00
}
/**
* @depends testCreateRow
2022-07-07 00:53:58 +00:00
* @throws \Exception
*/
public function testUpdateRow($data): void
2022-07-07 00:53:58 +00:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$UPDATE_ROW);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'tableId' => $data['table']['_id'],
'rowId' => $data['row']['_id'],
2022-07-07 00:53:58 +00:00
'data' => [
'name' => 'New Row Name',
2022-07-07 00:53:58 +00:00
],
]
];
$row = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
2022-07-07 00:53:58 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertArrayNotHasKey('errors', $row['body']);
$this->assertIsArray($row['body']['data']);
2025-07-27 09:53:07 +00:00
$row = $row['body']['data']['gridsUpdateRow'];
$this->assertIsArray($row);
2022-12-08 03:08:57 +00:00
$this->assertStringContainsString('New Row Name', $row['data']);
2022-07-07 00:53:58 +00:00
}
/**
* @depends testCreateRow
2022-07-07 00:53:58 +00:00
* @throws \Exception
*/
public function testDeleteRow($data): void
2022-07-07 00:53:58 +00:00
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$DELETE_ROW);
2022-07-07 00:53:58 +00:00
$gqlPayload = [
'query' => $query,
'variables' => [
2022-12-08 03:08:57 +00:00
'databaseId' => $data['database']['_id'],
'tableId' => $data['table']['_id'],
'rowId' => $data['row']['_id'],
2022-07-07 00:53:58 +00:00
]
];
$row = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
2022-07-07 00:53:58 +00:00
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertIsNotArray($row['body']);
$this->assertEquals(204, $row['headers']['status-code']);
2022-07-07 00:53:58 +00:00
}
2025-06-13 08:34:23 +00:00
/**
* @throws \Exception
*/
public function testBulkCreate(): 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-07-27 09:53:07 +00:00
$query = $this->getQuery(self::$GRIDS_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']);
2025-07-27 09:53:07 +00:00
$databaseId = $res['body']['data']['gridsCreateDatabase']['_id'];
2025-06-13 08:34:23 +00:00
// Step 2: Create table
$query = $this->getQuery(self::$CREATE_TABLE);
$payload['query'] = $query;
$payload['variables'] = [
'databaseId' => $databaseId,
'tableId' => 'operations',
'name' => 'Operations',
'rowSecurity' => 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']);
2025-07-27 09:53:07 +00:00
$tableId = $res['body']['data']['gridsCreateTable']['_id'];
2025-06-13 08:34:23 +00:00
// Step 3: Create column
$query = $this->getQuery(self::$CREATE_STRING_COLUMN);
$payload['query'] = $query;
$payload['variables'] = [
'databaseId' => $databaseId,
'tableId' => $tableId,
'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 rows
$query = $this->getQuery(self::$CREATE_ROWS);
$rows = [];
for ($i = 1; $i <= 10; $i++) {
$rows[] = ['$id' => 'row' . $i, 'name' => 'Row #' . $i];
}
$payload['query'] = $query;
$payload['variables'] = [
'databaseId' => $databaseId,
'tableId' => $tableId,
'rows' => $rows,
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
2025-07-27 09:53:07 +00:00
$this->assertCount(10, $res['body']['data']['gridsCreateRows']['rows']);
2025-06-13 08:34:23 +00:00
return compact('databaseId', 'tableId', 'projectId');
}
/**
* @depends testBulkCreate
*/
public function testBulkUpdate(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'],
];
// Step 1: Bulk update rows
$query = $this->getQuery(self::$UPDATE_ROWS);
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'tableId' => $data['tableId'],
'data' => [
'name' => 'Rows Updated',
'$permissions' => $permissions,
],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
2025-07-27 09:53:07 +00:00
$this->assertCount(10, $res['body']['data']['gridsUpdateRows']['rows']);
2025-06-13 08:34:23 +00:00
// Step 2: Fetch and validate updated rows
$query = $this->getQuery(self::$GET_ROWS);
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'tableId' => $data['tableId'],
'queries' => [Query::equal('name', ['Rows Updated'])->toString()],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertEquals(200, $res['headers']['status-code']);
2025-07-27 09:53:07 +00:00
$fetched = $res['body']['data']['gridsListRows'];
2025-06-13 08:34:23 +00:00
$this->assertEquals(10, $fetched['total']);
foreach ($fetched['rows'] as $row) {
$this->assertEquals($permissions, $row['_permissions']);
$this->assertEquals($data['tableId'], $row['_tableId']);
$this->assertEquals($data['databaseId'], $row['_databaseId']);
$this->assertEquals('Rows Updated', json_decode($row['data'], true)['name']);
}
return $data;
}
/**
* @depends testBulkCreate
*/
public function testBulkUpsert(array $data): array
{
$userId = $this->getUser()['$id'];
$headers = [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
'x-appwrite-key' => $this->getProject()['apiKey'],
];
$permissions = [
Permission::read(Role::user($userId)),
Permission::update(Role::user($userId)),
Permission::delete(Role::user($userId)),
];
// Step 1: Mutate row 10 and add row 11
$query = $this->getQuery(self::$UPSERT_ROWS);
$upsertPayload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'tableId' => $data['tableId'],
'rows' => [
[
'$id' => 'row10',
'name' => 'Row #1000',
],
[
'name' => 'Row #11',
],
],
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $upsertPayload);
$this->assertArrayNotHasKey('errors', $response['body']);
2025-07-27 09:53:07 +00:00
$rows = $response['body']['data']['gridsUpsertRows']['rows'];
2025-06-13 08:34:23 +00:00
$this->assertCount(2, $rows);
$rowMap = [];
foreach ($rows as $row) {
$decoded = json_decode($row['data'], true);
$rowMap[$decoded['name']] = $decoded;
}
$this->assertArrayHasKey('Row #1000', $rowMap);
$this->assertArrayHasKey('Row #11', $rowMap);
// Step 2: Fetch all rows and confirm count is now 11
$query = $this->getQuery(self::$GET_ROWS);
$fetchPayload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'tableId' => $data['tableId'],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $fetchPayload);
$this->assertEquals(200, $res['headers']['status-code']);
2025-07-27 09:53:07 +00:00
$fetched = $res['body']['data']['gridsListRows'];
2025-06-13 08:34:23 +00:00
$this->assertEquals(11, $fetched['total']);
// Step 3: Upsert row with new permissions using `tablesUpsertRow`
$query = $this->getQuery(self::$UPSERT_ROW);
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'tableId' => $data['tableId'],
'rowId' => 'row10',
'data' => ['name' => 'Row #10 Patched'],
'permissions' => $permissions,
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
2025-07-27 09:53:07 +00:00
$updated = $res['body']['data']['gridsUpsertRow'];
2025-06-13 08:34:23 +00:00
$this->assertEquals('Row #10 Patched', json_decode($updated['data'], true)['name']);
$this->assertEquals($data['databaseId'], $updated['_databaseId']);
$this->assertEquals($data['tableId'], $updated['_tableId']);
return $data;
}
/**
* @depends testBulkUpsert
*/
public function testBulkDelete(array $data): array
{
$headers = [
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
'x-appwrite-key' => $this->getProject()['apiKey'],
];
// Step 1: Perform bulk delete
$query = $this->getQuery(self::$DELETE_ROWS);
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'tableId' => $data['tableId'],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertArrayNotHasKey('errors', $res['body']);
2025-07-27 09:53:07 +00:00
$deleted = $res['body']['data']['gridsDeleteRows']['rows'];
2025-06-13 08:34:23 +00:00
$this->assertIsArray($deleted);
$this->assertCount(11, $deleted);
// Step 2: Confirm deletion via refetch
$query = $this->getQuery(self::$GET_ROWS);
$payload = [
'query' => $query,
'variables' => [
'databaseId' => $data['databaseId'],
'tableId' => $data['tableId'],
],
];
$res = $this->client->call(Client::METHOD_POST, '/graphql', $headers, $payload);
$this->assertEquals(200, $res['headers']['status-code']);
2025-07-27 09:53:07 +00:00
$this->assertEquals(0, $res['body']['data']['gridsListRows']['total']);
2025-06-13 08:34:23 +00:00
return $data;
}
2022-07-06 03:51:37 +00:00
}