updated endpoints

This commit is contained in:
ArnabChatterjee20k
2025-09-17 13:54:27 +05:30
parent bfacb900e2
commit b452bd0d44
14 changed files with 921 additions and 30 deletions
@@ -6697,4 +6697,246 @@ class DatabasesCustomServerTest extends Scope
'x-appwrite-key' => $this->getProject()['apiKey']
]));
}
public function testCreatedByUpdatedByModify(): void
{
// Create database
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'databaseId' => ID::unique(),
'name' => 'CreatedBy UpdatedBy Test DB'
]);
$this->assertEquals(201, $database['headers']['status-code']);
$databaseId = $database['body']['$id'];
// Create table
$table = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'tableId' => ID::unique(),
'name' => 'CreatedBy UpdatedBy Table',
'documentSecurity' => false,
'permissions' => [
Permission::create(Role::any()),
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]);
$this->assertEquals(201, $table['headers']['status-code']);
$tableId = $table['body']['$id'];
// Add columns
$this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'key' => 'title',
'size' => 255,
'required' => true,
]);
$this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/integer', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'key' => 'count',
'required' => false,
]);
sleep(2);
// Test 1: Create row with manual $createdBy and $updatedBy
$row = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'rowId' => ID::unique(),
'data' => [
'title' => 'Server Side Test',
'count' => 10,
'$createdBy' => 'user-123',
'$updatedBy' => 'user-456'
]
]);
$this->assertEquals(201, $row['headers']['status-code']);
$this->assertEquals('user-123', $row['body']['$createdBy']);
$this->assertEquals('user-456', $row['body']['$updatedBy']);
$rowId = $row['body']['$id'];
// Test 2: Update row with different $updatedBy
$updatedRow = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'data' => [
'title' => 'Updated Server Side Test',
'$updatedBy' => 'user-789'
]
]);
$this->assertEquals(200, $updatedRow['headers']['status-code']);
$this->assertEquals('user-123', $updatedRow['body']['$createdBy']); // Should remain unchanged
$this->assertEquals('user-789', $updatedRow['body']['$updatedBy']); // Should be updated
// Test 3: Bulk create with mixed $createdBy and $updatedBy values
// bulk1: both createdBy and updatedBy set
// bulk2: only createdBy set, updatedBy should be null
// bulk3: neither set, both should be null
$bulkRows = [
[
'$id' => 'bulk1',
'title' => 'Bulk Row 1',
'count' => 1,
'$createdBy' => 'bulk-user-1',
'$updatedBy' => 'bulk-user-1'
],
[
'$id' => 'bulk2',
'title' => 'Bulk Row 2',
'count' => 2,
'$createdBy' => 'bulk-user-2'
],
[
'$id' => 'bulk3',
'title' => 'Bulk Row 3',
'count' => 3
]
];
$bulkCreateResponse = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'rows' => $bulkRows
]);
$this->assertEquals(201, $bulkCreateResponse['headers']['status-code']);
$this->assertCount(3, $bulkCreateResponse['body']['rows']);
// Verify bulk create results
$this->assertEquals('bulk-user-1', $bulkCreateResponse['body']['rows'][0]['$createdBy']);
$this->assertEquals('bulk-user-1', $bulkCreateResponse['body']['rows'][0]['$updatedBy']);
$this->assertEquals('bulk-user-2', $bulkCreateResponse['body']['rows'][1]['$createdBy']);
// Test 4: Bulk update with $updatedBy
$bulkUpdateResponse = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'data' => [
'title' => 'Bulk Updated',
'$updatedBy' => 'bulk-updater'
],
'queries' => [Query::startsWith('$id', 'bulk')->toString()]
]);
$this->assertEquals(200, $bulkUpdateResponse['headers']['status-code']);
$this->assertCount(3, $bulkUpdateResponse['body']['rows']);
// Verify bulk update results - $createdBy should remain unchanged, $updatedBy should be updated
foreach ($bulkUpdateResponse['body']['rows'] as $doc) {
$this->assertEquals('bulk-updater', $doc['$updatedBy']);
$this->assertEquals('Bulk Updated', $doc['title']);
// $createdBy should remain as originally set
if ($doc['$id'] === 'bulk1') {
$this->assertEquals('bulk-user-1', $doc['$createdBy']);
} elseif ($doc['$id'] === 'bulk2') {
$this->assertEquals('bulk-user-2', $doc['$createdBy']);
}
}
// Test 5: Bulk upsert with $createdBy and $updatedBy
// bulk1: existing row - createdBy should be ignored, updatedBy should be updated
// new-upsert: new row - both should be set as specified
$upsertRows = [
[
'$id' => 'bulk1', // Existing row
'title' => 'Upserted Row 1',
'count' => 100,
'$createdBy' => 'should-not-change',
'$updatedBy' => 'upsert-updater-1'
],
[
'$id' => 'new-upsert',
'title' => 'New Upserted Row',
'count' => 200,
'$createdBy' => 'new-creator',
'$updatedBy' => 'new-updater'
]
];
$bulkUpsertResponse = $this->client->call(Client::METHOD_PUT, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'rows' => $upsertRows
]);
$this->assertEquals(200, $bulkUpsertResponse['headers']['status-code']);
$this->assertCount(2, $bulkUpsertResponse['body']['rows']);
// Verify upsert results
foreach ($bulkUpsertResponse['body']['rows'] as $doc) {
if ($doc['$id'] === 'bulk1') {
$this->assertEquals('should-not-change', $doc['$createdBy']);
$this->assertEquals('upsert-updater-1', $doc['$updatedBy']);
} elseif ($doc['$id'] === 'new-upsert') {
$this->assertEquals('new-creator', $doc['$createdBy']);
$this->assertEquals('new-updater', $doc['$updatedBy']);
}
}
// Test 6: Increment operation
// making requests via root
$incrementResponse = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId . '/count/increment', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'value' => 5
]);
$this->assertEquals(200, $incrementResponse['headers']['status-code']);
$this->assertEquals(15, $incrementResponse['body']['count']);
$this->assertEquals('user-123', $incrementResponse['body']['$createdBy']);
$this->assertEquals($this->getRoot()['$id'], $incrementResponse['body']['$updatedBy']);
// Test 7: Decrement operation
$decrementResponse = $this->client->call(Client::METHOD_PATCH, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/rows/' . $rowId . '/count/decrement', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'value' => 3
]);
$this->assertEquals(200, $decrementResponse['headers']['status-code']);
$this->assertEquals(12, $decrementResponse['body']['count']);
$this->assertEquals('user-123', $decrementResponse['body']['$createdBy']);
$this->assertEquals($this->getRoot()['$id'], $decrementResponse['body']['$updatedBy']);
// Cleanup
$this->client->call(Client::METHOD_DELETE, '/tablesdb/' . $databaseId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]);
}
}