1
0
Fork 0
mirror of synced 2024-06-14 00:34:51 +12:00
This commit is contained in:
Jake Barnby 2024-04-23 15:20:25 +12:00
parent d5335c4398
commit 476074cc5b
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
2 changed files with 204 additions and 184 deletions

View file

@ -16,142 +16,6 @@ use Utopia\Validator\JSON;
trait DatabasesBase trait DatabasesBase
{ {
/**
* @throws \Utopia\Database\Exception
* @throws \Utopia\Database\Exception\Query
*/
public function testOrQueries(): void
{
// Create database
$database = $this->client->call(Client::METHOD_POST, '/databases', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'databaseId' => ID::unique(),
'name' => 'Or queries'
]);
$this->assertNotEmpty($database['body']['$id']);
$this->assertEquals(201, $database['headers']['status-code']);
$this->assertEquals('Or queries', $database['body']['name']);
$databaseId = $database['body']['$id'];
// Create Collection
$presidents = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'collectionId' => ID::unique(),
'name' => 'USA Presidents',
'documentSecurity' => true,
'permissions' => [
Permission::create(Role::user($this->getUser()['$id'])),
],
]);
$this->assertEquals(201, $presidents['headers']['status-code']);
$this->assertEquals($presidents['body']['name'], 'USA Presidents');
// Create Attributes
$firstName = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'first_name',
'size' => 256,
'required' => true,
]);
$this->assertEquals(202, $firstName['headers']['status-code']);
$lastName = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'last_name',
'size' => 256,
'required' => true,
]);
$this->assertEquals(202, $lastName['headers']['status-code']);
// Wait for worker
sleep(2);
$document1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => [
'first_name' => 'Donald',
'last_name' => 'Trump',
],
'permissions' => [
Permission::read(Role::user($this->getUser()['$id'])),
]
]);
$this->assertEquals(201, $document1['headers']['status-code']);
$document2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => [
'first_name' => 'George',
'last_name' => 'Bush',
],
'permissions' => [
Permission::read(Role::user($this->getUser()['$id'])),
]
]);
$this->assertEquals(201, $document2['headers']['status-code']);
$document3 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => [
'first_name' => 'Joe',
'last_name' => 'Biden',
],
'permissions' => [
Permission::read(Role::user($this->getUser()['$id'])),
]
]);
$this->assertEquals(201, $document3['headers']['status-code']);
$documents = $this->client->call(
Client::METHOD_GET,
'/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents',
array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
[
'queries' => [
Query::select(['first_name', 'last_name'])->toString(),
Query::or([
Query::equal('first_name', ['Donald']),
Query::equal('last_name', ['Bush'])
])->toString(),
Query::limit(999)->toString(),
Query::offset(0)->toString()
],
]
);
$this->assertEquals(200, $documents['headers']['status-code']);
$this->assertCount(2, $documents['body']['documents']);
}
public function testCreateDatabase(): array public function testCreateDatabase(): array
{ {
/** /**
@ -4539,7 +4403,7 @@ trait DatabasesBase
/** /**
* @depends testOneToManyRelationship * @depends testOneToManyRelationship
*/ */
public function testSelectsQueries(array $data): void public function testSelectQueries(array $data): void
{ {
$response = $this->client->call(Client::METHOD_GET, '/databases/' . $data['databaseId'] . '/collections/' . $data['personCollection'] . '/documents', array_merge([ $response = $this->client->call(Client::METHOD_GET, '/databases/' . $data['databaseId'] . '/collections/' . $data['personCollection'] . '/documents', array_merge([
'content-type' => 'application/json', 'content-type' => 'application/json',
@ -4584,6 +4448,142 @@ trait DatabasesBase
$this->assertArrayNotHasKey('libraries', $response['body']); $this->assertArrayNotHasKey('libraries', $response['body']);
} }
/**
* @throws \Utopia\Database\Exception
* @throws \Utopia\Database\Exception\Query
*/
public function testOrQueries(): void
{
// Create database
$database = $this->client->call(Client::METHOD_POST, '/databases', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
], [
'databaseId' => ID::unique(),
'name' => 'Or queries'
]);
$this->assertNotEmpty($database['body']['$id']);
$this->assertEquals(201, $database['headers']['status-code']);
$this->assertEquals('Or queries', $database['body']['name']);
$databaseId = $database['body']['$id'];
// Create Collection
$presidents = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'collectionId' => ID::unique(),
'name' => 'USA Presidents',
'documentSecurity' => true,
'permissions' => [
Permission::create(Role::user($this->getUser()['$id'])),
],
]);
$this->assertEquals(201, $presidents['headers']['status-code']);
$this->assertEquals($presidents['body']['name'], 'USA Presidents');
// Create Attributes
$firstName = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'first_name',
'size' => 256,
'required' => true,
]);
$this->assertEquals(202, $firstName['headers']['status-code']);
$lastName = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/attributes/string', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'key' => 'last_name',
'size' => 256,
'required' => true,
]);
$this->assertEquals(202, $lastName['headers']['status-code']);
// Wait for worker
sleep(2);
$document1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => [
'first_name' => 'Donald',
'last_name' => 'Trump',
],
'permissions' => [
Permission::read(Role::user($this->getUser()['$id'])),
]
]);
$this->assertEquals(201, $document1['headers']['status-code']);
$document2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => [
'first_name' => 'George',
'last_name' => 'Bush',
],
'permissions' => [
Permission::read(Role::user($this->getUser()['$id'])),
]
]);
$this->assertEquals(201, $document2['headers']['status-code']);
$document3 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'documentId' => ID::unique(),
'data' => [
'first_name' => 'Joe',
'last_name' => 'Biden',
],
'permissions' => [
Permission::read(Role::user($this->getUser()['$id'])),
]
]);
$this->assertEquals(201, $document3['headers']['status-code']);
$documents = $this->client->call(
Client::METHOD_GET,
'/databases/' . $databaseId . '/collections/' . $presidents['body']['$id'] . '/documents',
array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
[
'queries' => [
Query::select(['first_name', 'last_name'])->toString(),
Query::or([
Query::equal('first_name', ['Donald']),
Query::equal('last_name', ['Bush'])
])->toString(),
Query::limit(999)->toString(),
Query::offset(0)->toString()
],
]
);
$this->assertEquals(200, $documents['headers']['status-code']);
$this->assertCount(2, $documents['body']['documents']);
}
/** /**
* @depends testCreateDatabase * @depends testCreateDatabase
* @param array $data * @param array $data

View file

@ -7,7 +7,9 @@ use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer; use Tests\E2E\Scopes\SideServer;
use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Exception;
use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role; use Utopia\Database\Helpers\Role;
@ -1227,59 +1229,77 @@ class DatabasesCustomServerTest extends Scope
$this->assertEquals(404, $response['headers']['status-code']); $this->assertEquals(404, $response['headers']['status-code']);
} }
// Adds several minutes to test to replicate coverage in Utopia\Database unit tests /**
// and messes with subsequent tests as DatabaseV1 queue gets overwhelmed * @throws Exception
// TODO@kodumbeats either fix or remove testAttributeCountLimit */
// Options to fix: public function testDeleteCollectionDeletesRelatedAttributes(): void
// - Enable attribute creation in batches {
// - Use additional database workers $database = $this->client->call(Client::METHOD_POST, '/databases', array_merge([
// - Wait for worker to complete before moving onto next test 'content-type' => 'application/json',
// - Remove since this is unit tested in Utopia\Database 'x-appwrite-project' => $this->getProject()['$id'],
// ], $this->getHeaders()), [
// public function testAttributeCountLimit() 'databaseId' => ID::unique(),
// { 'name' => 'TestDeleteCollectionDeletesRelatedAttributes',
// $collection = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ ]);
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey']
// ]), [
// 'collectionId' => ID::unique(),
// 'name' => 'attributeCountLimit',
// 'read' => ['any'],
// 'write' => ['any'],
// 'documentSecurity' => true,
// ]);
// $collectionId = $collection['body']['$id']; $databaseId = $database['body']['$id'];
// // load the collection up to the limit $collection1 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
// for ($i=0; $i < 1012; $i++) { 'content-type' => 'application/json',
// $attribute = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer', array_merge([ 'x-appwrite-project' => $this->getProject()['$id'],
// 'content-type' => 'application/json', 'x-appwrite-key' => $this->getProject()['apiKey']
// 'x-appwrite-project' => $this->getProject()['$id'], ]), [
// 'x-appwrite-key' => $this->getProject()['apiKey'] 'collectionId' => ID::unique(),
// ]), [ 'name' => 'Collection1',
// 'key' => "attribute{$i}", 'documentSecurity' => false,
// 'required' => false, 'permissions' => [],
// ]); ]);
// $this->assertEquals(201, $attribute['headers']['status-code']); $collection2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
// } 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'collectionId' => ID::unique(),
'name' => 'Collection2',
'documentSecurity' => false,
'permissions' => [],
]);
// sleep(30); $collection1 = $collection1['body']['$id'];
$collection2 = $collection2['body']['$id'];
// $tooMany = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer', array_merge([ $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $collection1 . '/attributes/relationship', array_merge([
// 'content-type' => 'application/json', 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey'] 'x-appwrite-key' => $this->getProject()['apiKey'],
// ]), [ ]), [
// 'key' => "tooMany", 'relatedCollectionId' => $collection2,
// 'required' => false, 'type' => Database::RELATION_MANY_TO_ONE,
// ]); 'twoWay' => false,
'key' => 'collection2'
]);
// $this->assertEquals(400, $tooMany['headers']['status-code']); sleep(2);
// $this->assertEquals('Attribute limit exceeded', $tooMany['body']['message']);
// } $this->client->call(Client::METHOD_DELETE, '/databases/' . $databaseId . '/collections/' . $collection2, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $this->getHeaders()));
sleep(2);
$attributes = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collection1 . '/attributes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $this->getHeaders()));
\var_dump($attributes['body']);
$this->assertEquals(0, $attributes['body']['total']);
}
public function testAttributeRowWidthLimit() public function testAttributeRowWidthLimit()
{ {