1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00
appwrite/tests/e2e/Services/Database/DatabaseCustomServerTest.php

407 lines
17 KiB
PHP
Raw Normal View History

2020-01-13 21:46:09 +13:00
<?php
namespace Tests\E2E\Services\Database;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
use Tests\E2E\Client;
2020-01-13 21:46:09 +13:00
class DatabaseCustomServerTest extends Scope
{
2020-12-05 18:34:58 +13:00
use DatabaseBase;
2020-01-13 21:46:09 +13:00
use ProjectCustom;
use SideServer;
public function testListCollections()
{
/**
* Test for SUCCESS
*/
$test1 = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'Test 1',
'collectionId' => 'first',
'read' => ['role:all'],
'write' => ['role:all'],
2021-08-23 02:06:59 +12:00
'permission' => 'document'
]);
$test2 = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'Test 2',
'collectionId' => 'second',
'read' => ['role:all'],
'write' => ['role:all'],
2021-08-23 02:06:59 +12:00
'permission' => 'document'
]);
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(2, $collections['body']['sum']);
$this->assertEquals($test1['body']['$id'], $collections['body']['collections'][0]['$id']);
$this->assertEquals($test2['body']['$id'], $collections['body']['collections'][1]['$id']);
/**
* Test for Order
*/
$base = array_reverse($collections['body']['collections']);
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'orderType' => 'DESC'
]);
$this->assertEquals(2, $collections['body']['sum']);
$this->assertEquals($base[0]['$id'], $collections['body']['collections'][0]['$id']);
$this->assertEquals($base[1]['$id'], $collections['body']['collections'][1]['$id']);
/**
* Test for After
*/
$base = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['collections'][0]['$id']
]);
$this->assertCount(1, $collections['body']['collections']);
$this->assertEquals($base['body']['collections'][1]['$id'], $collections['body']['collections'][0]['$id']);
$collections = $this->client->call(Client::METHOD_GET, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'after' => $base['body']['collections'][1]['$id']
]);
$this->assertCount(0, $collections['body']['collections']);
$this->assertEmpty($collections['body']['collections']);
}
2021-08-09 11:56:31 +12:00
public function testDeleteAttribute(): array
{
/**
* Test for SUCCESS
*/
// Create collection
$actors = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-19 21:26:00 +12:00
'collectionId' => 'unique()',
2021-06-16 02:24:51 +12:00
'name' => 'Actors',
'read' => ['role:all'],
'write' => ['role:all'],
2021-08-23 02:06:59 +12:00
'permission' => 'document'
]);
$this->assertEquals($actors['headers']['status-code'], 201);
$this->assertEquals($actors['body']['name'], 'Actors');
2021-06-12 06:07:05 +12:00
2021-07-22 03:05:18 +12:00
$firstName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([
2021-06-12 06:07:05 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-22 03:05:18 +12:00
'attributeId' => 'firstName',
2021-06-12 06:07:05 +12:00
'size' => 256,
'required' => true,
]);
2021-07-22 03:05:18 +12:00
$lastName = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([
2021-06-12 06:07:05 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-07-22 03:05:18 +12:00
'attributeId' => 'lastName',
2021-06-12 06:07:05 +12:00
'size' => 256,
'required' => true,
]);
2021-08-09 10:08:10 +12:00
$unneeded = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'attributeId' => 'unneeded',
'size' => 256,
'required' => true,
]);
// Wait for database worker to finish creating attributes
2021-08-23 16:06:53 +12:00
sleep(2);
2021-08-09 10:08:10 +12:00
$index = $this->client->call(Client::METHOD_POST, '/database/collections/' . $actors['body']['$id'] . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'indexId' => 'key_lastName',
'type' => 'key',
'attributes' => [
'lastName',
],
]);
// Wait for database worker to finish creating index
2021-08-23 16:06:53 +12:00
sleep(2);
2021-08-09 10:08:10 +12:00
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $actors['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), []);
2021-08-23 02:06:59 +12:00
$unneededId = $unneeded['body']['key'];
2021-08-09 10:08:10 +12:00
2021-08-23 16:06:53 +12:00
$this->assertEquals($collection['headers']['status-code'], 200);
2021-08-09 10:08:10 +12:00
$this->assertIsArray($collection['body']['attributes']);
$this->assertCount(3, $collection['body']['attributes']);
2021-08-23 02:06:59 +12:00
$this->assertEquals($collection['body']['attributes'][0]['key'], $firstName['body']['key']);
$this->assertEquals($collection['body']['attributes'][1]['key'], $lastName['body']['key']);
$this->assertEquals($collection['body']['attributes'][2]['key'], $unneeded['body']['key']);
2021-08-09 10:08:10 +12:00
$this->assertCount(1, $collection['body']['indexes']);
2021-08-23 04:36:26 +12:00
$this->assertEquals($collection['body']['indexes'][0]['key'], $index['body']['key']);
2021-08-09 10:08:10 +12:00
// Delete attribute
2021-08-23 16:06:53 +12:00
$attribute = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $actors ['body']['$id'] . '/attributes/' . $unneededId, array_merge([
2021-08-09 10:08:10 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
2021-08-23 16:06:53 +12:00
$this->assertEquals($attribute['headers']['status-code'], 204);
sleep(2);
2021-06-12 06:07:05 +12:00
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $actors['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), []);
2021-08-23 16:06:53 +12:00
$this->assertEquals($collection['headers']['status-code'], 200);
$this->assertIsArray($collection['body']['attributes']);
$this->assertCount(2, $collection['body']['attributes']);
2021-08-23 04:36:26 +12:00
$this->assertEquals($collection['body']['attributes'][0]['key'], $firstName['body']['key']);
$this->assertEquals($collection['body']['attributes'][1]['key'], $lastName['body']['key']);
2021-08-09 10:08:10 +12:00
return [
'collectionId' => $actors['body']['$id'],
2021-08-23 04:36:26 +12:00
'indexId' => $index['body']['key'],
2021-08-09 10:08:10 +12:00
];
}
2021-08-23 16:06:53 +12:00
2021-08-09 10:08:10 +12:00
/**
* @depends testDeleteAttribute
*/
2021-08-09 11:56:31 +12:00
public function testDeleteIndex($data): array
{
$index = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $data['collectionId'] . '/indexes/'. $data['indexId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
2021-08-23 16:06:53 +12:00
$this->assertEquals($index['headers']['status-code'], 204);
2021-08-09 11:56:31 +12:00
// Wait for database worker to finish deleting index
2021-08-23 16:06:53 +12:00
sleep(2);
2021-08-09 11:56:31 +12:00
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['collectionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
2021-08-23 16:06:53 +12:00
]), []);
2021-08-09 11:56:31 +12:00
$this->assertCount(0, $collection['body']['indexes']);
return $data;
}
/**
* @depends testDeleteIndex
*/
2021-08-09 10:08:10 +12:00
public function testDeleteCollection($data)
{
$collectionId = $data['collectionId'];
// Add Documents to the collection
2021-08-09 10:08:10 +12:00
$document1 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
'data' => [
'firstName' => 'Tom',
'lastName' => 'Holland',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
2021-08-09 10:08:10 +12:00
$document2 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
2021-07-19 21:26:00 +12:00
'documentId' => 'unique()',
'data' => [
'firstName' => 'Samuel',
'lastName' => 'Jackson',
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
]);
2021-06-12 06:07:05 +12:00
$this->assertEquals($document1['headers']['status-code'], 201);
2021-06-12 06:07:05 +12:00
$this->assertIsArray($document1['body']['$read']);
$this->assertIsArray($document1['body']['$write']);
$this->assertCount(1, $document1['body']['$read']);
$this->assertCount(1, $document1['body']['$write']);
$this->assertEquals($document1['body']['firstName'], 'Tom');
$this->assertEquals($document1['body']['lastName'], 'Holland');
$this->assertEquals($document2['headers']['status-code'], 201);
2021-06-12 06:07:05 +12:00
$this->assertIsArray($document2['body']['$read']);
$this->assertIsArray($document2['body']['$write']);
$this->assertCount(1, $document2['body']['$read']);
$this->assertCount(1, $document2['body']['$write']);
$this->assertEquals($document2['body']['firstName'], 'Samuel');
$this->assertEquals($document2['body']['lastName'], 'Jackson');
// Delete the actors collection
2021-08-09 10:08:10 +12:00
$response = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $collectionId , array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], $this->getHeaders()));
$this->assertEquals($response['headers']['status-code'], 204);
$this->assertEquals($response['body'],"");
// Try to get the collection and check if it has been deleted
2021-08-09 10:08:10 +12:00
$response = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId , array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id']
], $this->getHeaders()));
$this->assertEquals($response['headers']['status-code'], 404);
}
2021-07-30 11:36:46 +12:00
public function testIndexLimitException()
{
$collection = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-08-12 05:25:22 +12:00
'collectionId' => 'testLimitException',
2021-07-30 11:36:46 +12:00
'name' => 'testLimitException',
'read' => ['role:all'],
'write' => ['role:all'],
2021-08-25 11:35:43 +12:00
'permission' => 'document',
2021-07-30 11:36:46 +12:00
]);
$this->assertEquals($collection['headers']['status-code'], 201);
$this->assertEquals($collection['body']['name'], 'testLimitException');
$collectionId = $collection['body']['$id'];
// add unique attributes for indexing
for ($i=0; $i < 64; $i++) {
// $this->assertEquals(true, static::getDatabase()->createAttribute('indexLimit', "test{$i}", Database::VAR_STRING, 16, true));
2021-08-12 05:25:22 +12:00
$attribute = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/string', array_merge([
2021-07-30 11:36:46 +12:00
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-08-12 05:25:22 +12:00
'attributeId' => "attribute{$i}",
2021-07-30 11:36:46 +12:00
'size' => 64,
'required' => true,
]);
$this->assertEquals($attribute['headers']['status-code'], 201);
}
2021-08-25 11:35:43 +12:00
sleep(5);
2021-07-31 06:40:37 +12:00
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
$this->assertEquals($collection['headers']['status-code'], 200);
$this->assertEquals($collection['body']['name'], 'testLimitException');
$this->assertIsArray($collection['body']['attributes']);
$this->assertIsArray($collection['body']['indexes']);
2021-08-12 05:25:22 +12:00
$this->assertCount(64, $collection['body']['attributes']);
$this->assertCount(0, $collection['body']['indexes']);
2021-07-30 11:36:46 +12:00
// testing for indexLimit = 64
// MariaDB, MySQL, and MongoDB create 3 indexes per new collection
// Add up to the limit, then check if the next index throws IndexLimitException
for ($i=0; $i < 61; $i++) {
// $this->assertEquals(true, static::getDatabase()->createIndex('indexLimit', "index{$i}", Database::INDEX_KEY, ["test{$i}"], [16]));
$index = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-08-12 05:24:24 +12:00
'indexId' => "key_attribute{$i}",
2021-07-31 06:40:37 +12:00
'type' => 'key',
2021-07-30 11:36:46 +12:00
'attributes' => ["attribute{$i}"],
]);
2021-07-31 06:40:37 +12:00
$this->assertEquals(201, $index['headers']['status-code']);
2021-08-25 11:35:43 +12:00
$this->assertEquals("key_attribute{$i}", $index['body']['key']);
2021-07-30 11:36:46 +12:00
}
2021-08-25 11:35:43 +12:00
sleep(5);
2021-07-30 11:36:46 +12:00
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
2021-07-31 06:40:37 +12:00
$this->assertEquals($collection['headers']['status-code'], 200);
$this->assertEquals($collection['body']['name'], 'testLimitException');
$this->assertIsArray($collection['body']['attributes']);
$this->assertIsArray($collection['body']['indexes']);
$this->assertCount(64, $collection['body']['attributes']);
$this->assertCount(61, $collection['body']['indexes']);
2021-07-30 11:36:46 +12:00
$tooMany = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/indexes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
2021-08-25 11:35:43 +12:00
'indexId' => 'tooMany',
2021-07-31 06:40:37 +12:00
'type' => 'key',
2021-08-25 11:35:43 +12:00
'attributes' => ['attribute61'],
2021-07-30 11:36:46 +12:00
]);
$this->assertEquals(400, $tooMany['headers']['status-code']);
2021-08-25 11:35:43 +12:00
$this->assertEquals('Index limit exceeded', $tooMany['body']['message']);
2021-07-30 11:36:46 +12:00
}
2021-06-12 06:47:23 +12:00
}