2022-06-30 12:00:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
namespace Tests\E2E\Services\GraphQL\Tables;
|
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;
|
2025-05-09 14:34:02 +00:00
|
|
|
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'];
|
|
|
|
|
$query = $this->getQuery(self::$CREATE_DATABASE);
|
|
|
|
|
$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
|
|
|
|
|
*/
|
2025-05-09 14:34:02 +00:00
|
|
|
public function testCreateTable($database): array
|
2022-07-07 00:53:58 +00:00
|
|
|
{
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'tableId' => 'actors',
|
2022-07-07 00:53:58 +00:00
|
|
|
'name' => 'Actors',
|
2025-05-09 14:34:02 +00:00
|
|
|
'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
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertIsArray($table['body']['data']);
|
|
|
|
|
$this->assertArrayNotHasKey('errors', $table['body']);
|
|
|
|
|
$table = $table['body']['data']['databasesCreateTable'];
|
|
|
|
|
$this->assertEquals('Actors', $table['name']);
|
2022-07-07 00:53:58 +00:00
|
|
|
|
|
|
|
|
return [
|
2025-05-09 14:34:02 +00:00
|
|
|
'table' => $table,
|
2022-07-07 00:53:58 +00:00
|
|
|
'database' => $database,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-09 14:34:02 +00:00
|
|
|
* @depends testCreateTable
|
2022-07-07 00:53:58 +00:00
|
|
|
*/
|
2025-05-09 14:34:02 +00:00
|
|
|
public function testCreateStringColumn($data): array
|
2022-07-07 00:53:58 +00:00
|
|
|
{
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'tableId' => $data['table']['_id'],
|
2022-07-07 00:53:58 +00:00
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertArrayNotHasKey('errors', $column['body']);
|
|
|
|
|
$this->assertIsArray($column['body']['data']);
|
|
|
|
|
$this->assertIsArray($column['body']['data']['tablesCreateStringColumn']);
|
2022-07-07 00:53:58 +00:00
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-09 14:34:02 +00:00
|
|
|
* @depends testCreateTable
|
2022-07-07 00:53:58 +00:00
|
|
|
*/
|
2025-05-09 14:34:02 +00:00
|
|
|
public function testCreateIntegerColumn($data): array
|
2022-07-07 00:53:58 +00:00
|
|
|
{
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'tableId' => $data['table']['_id'],
|
2022-07-07 00:53:58 +00:00
|
|
|
'key' => 'age',
|
|
|
|
|
'min' => 18,
|
|
|
|
|
'max' => 150,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertArrayNotHasKey('errors', $column['body']);
|
|
|
|
|
$this->assertIsArray($column['body']['data']);
|
|
|
|
|
$this->assertIsArray($column['body']['data']['tablesCreateIntegerColumn']);
|
2022-07-07 00:53:58 +00:00
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-09 14:34:02 +00:00
|
|
|
* @depends testCreateStringColumn
|
|
|
|
|
* @depends testCreateIntegerColumn
|
2022-07-07 00:53:58 +00:00
|
|
|
*/
|
2025-05-09 14:34:02 +00:00
|
|
|
public function testCreateRow($data): array
|
2022-07-07 00:53:58 +00:00
|
|
|
{
|
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'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
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertArrayNotHasKey('errors', $row['body']);
|
|
|
|
|
$this->assertIsArray($row['body']['data']);
|
2022-07-07 00:53:58 +00:00
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$row = $row['body']['data']['tablesCreateRow'];
|
|
|
|
|
$this->assertIsArray($row);
|
2022-07-07 00:53:58 +00:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'database' => $data['database'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'table' => $data['table'],
|
|
|
|
|
'row' => $row,
|
2022-07-07 00:53:58 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-09 14:34:02 +00:00
|
|
|
* @depends testCreateTable
|
2022-07-07 00:53:58 +00:00
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2025-05-09 14:34:02 +00:00
|
|
|
public function testGetRows($data): void
|
2022-07-07 00:53:58 +00:00
|
|
|
{
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'tableId' => $data['table']['_id'],
|
2022-07-07 00:53:58 +00:00
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertArrayNotHasKey('errors', $rows['body']);
|
|
|
|
|
$this->assertIsArray($rows['body']['data']);
|
|
|
|
|
$this->assertIsArray($rows['body']['data']['tablesListRows']);
|
2022-07-07 00:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-09 14:34:02 +00:00
|
|
|
* @depends testCreateRow
|
2022-07-07 00:53:58 +00:00
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
public function testGetDocument($data): void
|
|
|
|
|
{
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'tableId' => $data['table']['_id'],
|
|
|
|
|
'rowId' => $data['row']['_id'],
|
2022-07-07 00:53:58 +00:00
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertArrayNotHasKey('errors', $row['body']);
|
|
|
|
|
$this->assertIsArray($row['body']['data']);
|
|
|
|
|
$this->assertIsArray($row['body']['data']['tablesGetRow']);
|
2022-07-07 00:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-09 14:34:02 +00:00
|
|
|
* @depends testCreateRow
|
2022-07-07 00:53:58 +00:00
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2025-05-09 14:34:02 +00:00
|
|
|
public function testUpdateRow($data): void
|
2022-07-07 00:53:58 +00:00
|
|
|
{
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'tableId' => $data['table']['_id'],
|
|
|
|
|
'rowId' => $data['row']['_id'],
|
2022-07-07 00:53:58 +00:00
|
|
|
'data' => [
|
2025-05-09 14:34:02 +00:00
|
|
|
'name' => 'New Row Name',
|
2022-07-07 00:53:58 +00:00
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertArrayNotHasKey('errors', $row['body']);
|
|
|
|
|
$this->assertIsArray($row['body']['data']);
|
|
|
|
|
$row = $row['body']['data']['tablesUpdateRow'];
|
|
|
|
|
$this->assertIsArray($row);
|
2022-12-08 03:08:57 +00:00
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertStringContainsString('New Row Name', $row['data']);
|
2022-07-07 00:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-05-09 14:34:02 +00:00
|
|
|
* @depends testCreateRow
|
2022-07-07 00:53:58 +00:00
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2025-05-09 14:34:02 +00:00
|
|
|
public function testDeleteRow($data): void
|
2022-07-07 00:53:58 +00:00
|
|
|
{
|
|
|
|
|
$projectId = $this->getProject()['$id'];
|
2025-05-09 14:34:02 +00:00
|
|
|
$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'],
|
2025-05-09 14:34:02 +00:00
|
|
|
'tableId' => $data['table']['_id'],
|
|
|
|
|
'rowId' => $data['row']['_id'],
|
2022-07-07 00:53:58 +00:00
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
|
2025-05-09 14:34:02 +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);
|
|
|
|
|
|
2025-05-09 14:34:02 +00:00
|
|
|
$this->assertIsNotArray($row['body']);
|
|
|
|
|
$this->assertEquals(204, $row['headers']['status-code']);
|
2022-07-07 00:53:58 +00:00
|
|
|
}
|
2022-07-06 03:51:37 +00:00
|
|
|
}
|