1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00
appwrite/tests/e2e/Services/Database/DatabaseBase.php

506 lines
22 KiB
PHP
Raw Normal View History

2020-01-13 21:46:09 +13:00
<?php
namespace Tests\E2E\Services\Database;
use Tests\E2E\Client;
trait DatabaseBase
{
public function testCreateCollection():array
{
/**
* Test for SUCCESS
*/
2020-01-31 06:22:58 +13:00
$actors = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'Actors',
'read' => ['*'],
2021-01-14 03:11:07 +13:00
'write' => ['role:member', 'role:admin'],
2020-01-13 21:46:09 +13:00
'rules' => [
[
'label' => 'First Name',
'key' => 'firstName',
'type' => 'text',
'default' => '',
'required' => true,
'array' => false
],
[
'label' => 'Last Name',
'key' => 'lastName',
'type' => 'text',
'default' => '',
'required' => true,
'array' => false
],
],
]);
$this->assertEquals($actors['headers']['status-code'], 201);
$this->assertEquals($actors['body']['$collection'], 0);
$this->assertEquals($actors['body']['name'], 'Actors');
$this->assertIsArray($actors['body']['$permissions']);
$this->assertIsArray($actors['body']['$permissions']['read']);
$this->assertIsArray($actors['body']['$permissions']['write']);
$this->assertCount(1, $actors['body']['$permissions']['read']);
$this->assertCount(2, $actors['body']['$permissions']['write']);
2020-01-31 06:22:58 +13:00
$movies = $this->client->call(Client::METHOD_POST, '/database/collections', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
'x-appwrite-key' => $this->getProject()['apiKey']
]), [
'name' => 'Movies',
'read' => ['*'],
2021-01-14 03:11:07 +13:00
'write' => ['role:member', 'role:admin'],
2020-01-13 21:46:09 +13:00
'rules' => [
[
'label' => 'Name',
'key' => 'name',
'type' => 'text',
'default' => '',
'required' => true,
'array' => false
],
[
'label' => 'Release Year',
'key' => 'releaseYear',
'type' => 'numeric',
'default' => 0,
'required' => false,
'array' => false
],
[
'label' => 'Actors',
'key' => 'actors',
'type' => 'document',
'default' => [],
'required' => false,
'array' => true,
2020-02-17 20:16:11 +13:00
'list' => [$actors['body']['$id']],
2020-01-13 21:46:09 +13:00
],
],
]);
$this->assertEquals($movies['headers']['status-code'], 201);
$this->assertEquals($movies['body']['$collection'], 0);
$this->assertEquals($movies['body']['name'], 'Movies');
$this->assertIsArray($movies['body']['$permissions']);
$this->assertIsArray($movies['body']['$permissions']['read']);
$this->assertIsArray($movies['body']['$permissions']['write']);
$this->assertCount(1, $movies['body']['$permissions']['read']);
$this->assertCount(2, $movies['body']['$permissions']['write']);
2020-02-17 20:16:11 +13:00
return array_merge(['moviesId' => $movies['body']['$id'], 'actorsId' => $actors['body']['$id']]);
2020-01-13 21:46:09 +13:00
}
/**
* @depends testCreateCollection
*/
public function testCreateDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document1 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'name' => 'Captain America',
'releaseYear' => 1944,
'actors' => [
[
'$collection' => $data['actorsId'],
'$permissions' => ['read' => [], 'write' => []],
'firstName' => 'Chris',
'lastName' => 'Evans',
],
[
'$collection' => $data['actorsId'],
'$permissions' => ['read' => [], 'write' => []],
'firstName' => 'Samuel',
'lastName' => 'Jackson',
],
]
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document2 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'name' => 'Spider-Man: Far From Home',
'releaseYear' => 2019,
'actors' => [
[
'$collection' => $data['actorsId'],
'$permissions' => ['read' => [], 'write' => []],
'firstName' => 'Tom',
'lastName' => 'Holland',
],
[
'$collection' => $data['actorsId'],
'$permissions' => ['read' => [], 'write' => []],
'firstName' => 'Zendaya',
'lastName' => 'Maree Stoermer',
],
[
'$collection' => $data['actorsId'],
'$permissions' => ['read' => [], 'write' => []],
'firstName' => 'Samuel',
'lastName' => 'Jackson',
],
]
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document3 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'name' => 'Spider-Man: Homecoming',
'releaseYear' => 2017,
'actors' => [
[
'$collection' => $data['actorsId'],
'$permissions' => ['read' => [], 'write' => []],
'firstName' => 'Tom',
'lastName' => 'Holland',
],
[
'$collection' => $data['actorsId'],
'$permissions' => ['read' => [], 'write' => []],
'firstName' => 'Zendaya',
'lastName' => 'Maree Stoermer',
],
],
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-01-31 06:22:58 +13:00
$document4 = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'releaseYear' => 2020, // Missing title, expect an 400 error
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($document1['headers']['status-code'], 201);
$this->assertEquals($document1['body']['$collection'], $data['moviesId']);
$this->assertEquals($document1['body']['name'], 'Captain America');
$this->assertEquals($document1['body']['releaseYear'], 1944);
$this->assertIsArray($document1['body']['$permissions']);
$this->assertIsArray($document1['body']['$permissions']['read']);
$this->assertIsArray($document1['body']['$permissions']['write']);
2020-01-14 06:53:22 +13:00
$this->assertCount(1, $document1['body']['$permissions']['read']);
$this->assertCount(1, $document1['body']['$permissions']['write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(2, $document1['body']['actors']);
$this->assertEquals($document2['headers']['status-code'], 201);
$this->assertEquals($document2['body']['$collection'], $data['moviesId']);
$this->assertEquals($document2['body']['name'], 'Spider-Man: Far From Home');
$this->assertEquals($document2['body']['releaseYear'], 2019);
$this->assertIsArray($document2['body']['$permissions']);
$this->assertIsArray($document2['body']['$permissions']['read']);
$this->assertIsArray($document2['body']['$permissions']['write']);
2020-01-14 06:53:22 +13:00
$this->assertCount(1, $document2['body']['$permissions']['read']);
$this->assertCount(1, $document2['body']['$permissions']['write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(3, $document2['body']['actors']);
$this->assertEquals($document2['body']['actors'][0]['firstName'], 'Tom');
$this->assertEquals($document2['body']['actors'][0]['lastName'], 'Holland');
$this->assertEquals($document2['body']['actors'][1]['firstName'], 'Zendaya');
$this->assertEquals($document2['body']['actors'][1]['lastName'], 'Maree Stoermer');
$this->assertEquals($document2['body']['actors'][2]['firstName'], 'Samuel');
$this->assertEquals($document2['body']['actors'][2]['lastName'], 'Jackson');
$this->assertEquals($document3['headers']['status-code'], 201);
$this->assertEquals($document3['body']['$collection'], $data['moviesId']);
$this->assertEquals($document3['body']['name'], 'Spider-Man: Homecoming');
$this->assertEquals($document3['body']['releaseYear'], 2017);
$this->assertIsArray($document3['body']['$permissions']);
$this->assertIsArray($document3['body']['$permissions']['read']);
$this->assertIsArray($document3['body']['$permissions']['write']);
2020-01-14 06:53:22 +13:00
$this->assertCount(1, $document3['body']['$permissions']['read']);
$this->assertCount(1, $document3['body']['$permissions']['write']);
2020-01-13 21:46:09 +13:00
$this->assertCount(2, $document3['body']['actors']);
$this->assertEquals($document3['body']['actors'][0]['firstName'], 'Tom');
$this->assertEquals($document3['body']['actors'][0]['lastName'], 'Holland');
$this->assertEquals($document3['body']['actors'][1]['firstName'], 'Zendaya');
$this->assertEquals($document3['body']['actors'][1]['lastName'], 'Maree Stoermer');
$this->assertEquals($document4['headers']['status-code'], 400);
return $data;
}
/**
* @depends testCreateDocument
*/
public function testListDocuments(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'orderField' => 'releaseYear',
'orderType' => 'ASC',
'orderCast' => 'int',
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
$this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']);
$this->assertEquals(2019, $documents['body']['documents'][2]['releaseYear']);
$this->assertCount(3, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'orderField' => 'releaseYear',
'orderType' => 'DESC',
'orderCast' => 'int',
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(1944, $documents['body']['documents'][2]['releaseYear']);
$this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']);
$this->assertEquals(2019, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(3, $documents['body']['documents']);
return [];
}
/**
* @depends testCreateDocument
*/
public function testListDocumentsLimitAndOffset(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'limit' => 1,
'orderField' => 'releaseYear',
'orderType' => 'ASC',
'orderCast' => 'int',
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(1, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'limit' => 2,
'offset' => 1,
'orderField' => 'releaseYear',
'orderType' => 'ASC',
'orderCast' => 'int',
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals(2017, $documents['body']['documents'][0]['releaseYear']);
$this->assertEquals(2019, $documents['body']['documents'][1]['releaseYear']);
$this->assertCount(2, $documents['body']['documents']);
return [];
}
/**
* @depends testCreateDocument
*/
public function testDocumentsListSuccessSearch(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'search' => 'Captain America',
]);
$this->assertEquals(1944, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(1, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'search' => 'Homecoming',
]);
$this->assertEquals(2017, $documents['body']['documents'][0]['releaseYear']);
$this->assertCount(1, $documents['body']['documents']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'search' => 'spider',
]);
$this->assertEquals(2019, $documents['body']['documents'][0]['releaseYear']);
$this->assertEquals(2017, $documents['body']['documents'][1]['releaseYear']);
$this->assertCount(2, $documents['body']['documents']);
return [];
}
/**
* @depends testCreateDocument
*/
public function testListDocumentsFilters(array $data):array
{
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'filters' => [
'actors.firstName=Tom'
],
]);
$this->assertCount(2, $documents['body']['documents']);
$this->assertEquals('Spider-Man: Far From Home', $documents['body']['documents'][0]['name']);
$this->assertEquals('Spider-Man: Homecoming', $documents['body']['documents'][1]['name']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'filters' => [
'releaseYear=1944'
],
]);
$this->assertCount(1, $documents['body']['documents']);
$this->assertEquals('Captain America', $documents['body']['documents'][0]['name']);
2020-01-31 06:22:58 +13:00
$documents = $this->client->call(Client::METHOD_GET, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'filters' => [
'releaseYear!=1944'
],
]);
$this->assertCount(2, $documents['body']['documents']);
$this->assertEquals('Spider-Man: Far From Home', $documents['body']['documents'][0]['name']);
$this->assertEquals('Spider-Man: Homecoming', $documents['body']['documents'][1]['name']);
return [];
}
/**
* @depends testCreateDocument
*/
public function testUpdateDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'name' => 'Thor: Ragnaroc',
'releaseYear' => 2017,
2020-01-14 06:53:22 +13:00
],
'read' => ['user:'.$this->getUser()['$id'], 'testx'],
'write' => ['user:'.$this->getUser()['$id'], 'testy'],
2020-01-13 21:46:09 +13:00
]);
2020-02-17 20:16:11 +13:00
$id = $document['body']['$id'];
2020-01-13 21:46:09 +13:00
$collection = $document['body']['$collection'];
$this->assertEquals($document['headers']['status-code'], 201);
$this->assertEquals($document['body']['name'], 'Thor: Ragnaroc');
$this->assertEquals($document['body']['releaseYear'], 2017);
$this->assertEquals($document['body']['$permissions']['read'][1], 'testx');
$this->assertEquals($document['body']['$permissions']['write'][1], 'testy');
2020-01-13 21:46:09 +13:00
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_PATCH, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'name' => 'Thor: Ragnarok'
2021-03-16 00:44:11 +13:00
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
$this->assertEquals($document['headers']['status-code'], 200);
$this->assertEquals($document['body']['name'], 'Thor: Ragnarok');
$this->assertEquals($document['body']['releaseYear'], 2017);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
2020-02-17 20:16:11 +13:00
$id = $document['body']['$id'];
2020-01-13 21:46:09 +13:00
$collection = $document['body']['$collection'];
$this->assertEquals($document['headers']['status-code'], 200);
$this->assertEquals($document['body']['name'], 'Thor: Ragnarok');
$this->assertEquals($document['body']['releaseYear'], 2017);
return [];
}
/**
* @depends testCreateDocument
*/
public function testDeleteDocument(array $data):array
{
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_POST, '/database/collections/' . $data['moviesId'] . '/documents', array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()), [
'data' => [
'name' => 'Thor: Ragnarok',
'releaseYear' => 2017,
2020-01-14 06:53:22 +13:00
],
2020-02-17 20:16:11 +13:00
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
2020-01-13 21:46:09 +13:00
]);
2020-02-17 20:16:11 +13:00
$id = $document['body']['$id'];
2020-01-13 21:46:09 +13:00
$collection = $document['body']['$collection'];
$this->assertEquals($document['headers']['status-code'], 201);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 200);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_DELETE, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 204);
2020-01-31 06:22:58 +13:00
$document = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collection . '/documents/' . $id, array_merge([
2020-01-13 21:46:09 +13:00
'content-type' => 'application/json',
2020-02-17 20:16:11 +13:00
'x-appwrite-project' => $this->getProject()['$id'],
2020-01-13 21:46:09 +13:00
], $this->getHeaders()));
$this->assertEquals($document['headers']['status-code'], 404);
return [];
}
}