add: graphql tests for bulk apis to server tests.

This commit is contained in:
Darshan
2025-06-13 14:13:26 +05:30
parent 6e1fa0526d
commit 9cfff792fd
@@ -12,6 +12,7 @@ use Utopia\Database\Database;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
use Utopia\Database\Query;
class DatabaseServerTest extends Scope
{
@@ -1465,4 +1466,282 @@ class DatabaseServerTest extends Scope
$this->assertIsNotArray($database['body']);
$this->assertEquals(204, $database['headers']['status-code']);
}
/**
* @throws Exception
*/
public function testBulkCreate(): array
{
$project = $this->getProject();
$projectId = $project['$id'];
$headers = array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders());
// Step 1: Create database
$query = $this->getQuery(self::$CREATE_DATABASE);
$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 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']);
$tableId = $res['body']['data']['databasesCreateTable']['_id'];
// 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']);
$this->assertCount(10, $res['body']['data']['tablesCreateRows']['rows']);
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 = array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
], $this->getHeaders());
// 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']);
$this->assertCount(10, $res['body']['data']['tablesUpdateRows']['rows']);
// 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']);
$fetched = $res['body']['data']['tablesListRows'];
$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 = array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
], $this->getHeaders());
$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']);
$rows = $response['body']['data']['tablesUpsertRows']['rows'];
$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']);
$fetched = $res['body']['data']['tablesListRows'];
$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']);
$updated = $res['body']['data']['tablesUpsertRow'];
$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 = array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $data['projectId'],
], $this->getHeaders());
// 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']);
$deleted = $res['body']['data']['tablesDeleteRows']['rows'];
$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']);
$this->assertEquals(0, $res['body']['data']['tablesListRows']['total']);
return $data;
}
}