From 002fc19d608aeb7252f7b9070ebc06ec4b700397 Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 10 Oct 2023 21:02:24 +0300 Subject: [PATCH] tests fix --- app/controllers/api/databases.php | 3 +- app/controllers/api/users.php | 4 +- app/worker.php | 4 +- composer.json | 2 +- composer.lock | 21 ++--- phpunit.xml | 2 +- .../e2e/Services/Databases/DatabasesBase.php | 1 - .../Databases/DatabasesConsoleClientTest.php | 9 +-- tests/e2e/Services/Teams/TeamsBaseServer.php | 2 +- .../Services/Users/UsersConsoleClientTest.php | 43 +--------- tests/unit/Event/EventTest.php | 47 ++++++----- tests/unit/Usage/StatsTest.php | 79 ++++++++----------- 12 files changed, 80 insertions(+), 137 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index ace53b2f2..b4709e656 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -1201,7 +1201,6 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/attributes/enum') ->inject('queueForDatabase') ->inject('queueForEvents') ->action(function (string $databaseId, string $collectionId, string $key, array $elements, ?bool $required, ?string $default, bool $array, Response $response, Database $dbForProject, EventDatabase $queueForDatabase, Event $queueForEvents) { - //var_dump($elements); // use length of longest string as attribute size $size = 0; foreach ($elements as $element) { @@ -2453,7 +2452,7 @@ App::post('/v1/databases/:databaseId/collections/:collectionId/indexes') 'orders' => $orders, ]); - $validator = new IndexValidator($attributes, $dbForProject->getAdapter()->getMaxIndexLength()); + $validator = new IndexValidator($dbForProject->getAdapter()->getMaxIndexLength()); if (!$validator->isValid($collection->setAttribute('indexes', $index, Document::SET_TYPE_APPEND))) { throw new Exception(Exception::INDEX_INVALID, $validator->getDescription()); } diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 99322f5ba..bd709ee27 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -735,7 +735,7 @@ App::put('/v1/users/:userId/labels') ->inject('response') ->inject('dbForProject') ->inject('queueForEvents') - ->action(function (string $userId, bool $emailVerification, Response $response, Database $dbForProject, Event $queueForEvents) { + ->action(function (string $userId, array $labels, Response $response, Database $dbForProject, Event $queueForEvents) { $user = $dbForProject->getDocument('users', $userId); @@ -1222,7 +1222,7 @@ App::get('/v1/users/usage') ->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.model', Response::MODEL_USAGE_USERS) - ->param('range', '30d', new WhiteList(['24h', '7d', '30d', '90d'], true), 'Date range.', true) + ->param('range', '30d', new WhiteList(['24h', '30d', '90d'], true), 'Date range.', true) ->inject('response') ->inject('dbForProject') ->inject('register') diff --git a/app/worker.php b/app/worker.php index be4fd2c66..572d876d6 100644 --- a/app/worker.php +++ b/app/worker.php @@ -69,7 +69,7 @@ Server::setResource('dbForProject', function (Cache $cache, Registry $register, ->pop() ->getResource(); - $adapter = new Database($database, $cache); + $adapter = new Database($database, $cache); $adapter->setNamespace('_' . $project->getInternalId()); return $adapter; }, ['cache', 'register', 'message', 'dbForConsole']); @@ -224,7 +224,7 @@ if (isset($args[0])) { try { $platform->init(Service::TYPE_WORKER, [ - 'workersNum' => swoole_cpu_num() * intval(App::getEnv('_APP_WORKER_PER_CORE', 6)), + 'workersNum' => 1, 'connection' => $pools->get('queue')->pop()->getResource(), 'workerName' => strtolower($workerName) ?? null, ]); diff --git a/composer.json b/composer.json index cb86574d6..f7e4e56e0 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,7 @@ "utopia-php/cache": "0.8.*", "utopia-php/cli": "0.15.*", "utopia-php/config": "0.2.*", - "utopia-php/database": "dev-fix-index-validation as 0.43.1", + "utopia-php/database": "0.43.*", "utopia-php/domains": "0.3.*", "utopia-php/dsn": "0.1.*", "utopia-php/framework": "0.31.0", diff --git a/composer.lock b/composer.lock index 598394db9..981cc546a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "15224f89284384114e0eef2ae83f49ea", + "content-hash": "748f1a5e4fdf0a73ea26ddf0e7822f19", "packages": [ { "name": "adhocore/jwt", @@ -1433,16 +1433,16 @@ }, { "name": "utopia-php/database", - "version": "dev-fix-index-validation", + "version": "0.43.5", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "67adc586318c35b34aaa9f1eed8f3751b6e8c324" + "reference": "5f7b05189cfbcc0506090498c580c5765375a00a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/67adc586318c35b34aaa9f1eed8f3751b6e8c324", - "reference": "67adc586318c35b34aaa9f1eed8f3751b6e8c324", + "url": "https://api.github.com/repos/utopia-php/database/zipball/5f7b05189cfbcc0506090498c580c5765375a00a", + "reference": "5f7b05189cfbcc0506090498c580c5765375a00a", "shasum": "" }, "require": { @@ -1483,9 +1483,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/fix-index-validation" + "source": "https://github.com/utopia-php/database/tree/0.43.5" }, - "time": "2023-10-09T02:11:28+00:00" + "time": "2023-10-06T06:49:47+00:00" }, { "name": "utopia-php/domains", @@ -5275,12 +5275,6 @@ } ], "aliases": [ - { - "package": "utopia-php/database", - "version": "dev-fix-index-validation", - "alias": "0.43.1", - "alias_normalized": "0.43.1.0" - }, { "package": "utopia-php/platform", "version": "dev-integrate-workers", @@ -5296,7 +5290,6 @@ ], "minimum-stability": "stable", "stability-flags": { - "utopia-php/database": 20, "utopia-php/platform": 20, "utopia-php/queue": 20 }, diff --git a/phpunit.xml b/phpunit.xml index 3d15bbd21..f83f9f0fa 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,7 +6,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true" + stopOnFailure="false" > diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index f869782ab..88a7f9931 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -1057,7 +1057,6 @@ trait DatabasesBase $this->assertEquals($releaseYearIndex['body']['key'], $movies['body']['indexes'][1]['key']); $this->assertEquals($releaseWithDate['body']['key'], $movies['body']['indexes'][2]['key']); $this->assertEquals('available', $movies['body']['indexes'][0]['status']); - var_dump($movies['body']['indexes']); $this->assertEquals('available', $movies['body']['indexes'][1]['status']); $this->assertEquals('available', $movies['body']['indexes'][2]['status']); diff --git a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php index 1476c8b28..f51da53cb 100644 --- a/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php +++ b/tests/e2e/Services/Databases/DatabasesConsoleClientTest.php @@ -296,7 +296,7 @@ class DatabasesConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'limit' => 1 + 'queries' => ['offset(1)', 'limit(1)'], ]); $this->assertEquals(200, $logs['headers']['status-code']); @@ -308,8 +308,8 @@ class DatabasesConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'offset' => 1 - ]); + 'queries' => ['offset(1)'] + ]); $this->assertEquals(200, $logs['headers']['status-code']); $this->assertIsArray($logs['body']['logs']); @@ -319,8 +319,7 @@ class DatabasesConsoleClientTest extends Scope 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ - 'offset' => 1, - 'limit' => 1 + 'queries' => ['offset(1)', 'limit(1)'], ]); $this->assertEquals(200, $logs['headers']['status-code']); diff --git a/tests/e2e/Services/Teams/TeamsBaseServer.php b/tests/e2e/Services/Teams/TeamsBaseServer.php index 7bb6241c4..9db17abaa 100644 --- a/tests/e2e/Services/Teams/TeamsBaseServer.php +++ b/tests/e2e/Services/Teams/TeamsBaseServer.php @@ -234,7 +234,7 @@ trait TeamsBaseServer /** * @depends testUpdateMembershipRoles */ - public function testDeleteUserUpdatesTeamMembershipCount($data) + public function testDeleteUserUpdatesTeamMembershipCount($data): void { $teamUid = $data['teamUid'] ?? ''; $userUid = $data['userUid'] ?? ''; diff --git a/tests/e2e/Services/Users/UsersConsoleClientTest.php b/tests/e2e/Services/Users/UsersConsoleClientTest.php index 9cc23e825..8943bfab6 100644 --- a/tests/e2e/Services/Users/UsersConsoleClientTest.php +++ b/tests/e2e/Services/Users/UsersConsoleClientTest.php @@ -23,17 +23,6 @@ class UsersConsoleClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'] ], $this->getHeaders()), [ 'range' => '32h', - 'provider' => 'email' - ]); - - $this->assertEquals($response['headers']['status-code'], 400); - - $response = $this->client->call(Client::METHOD_GET, '/users/usage', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'] - ], $this->getHeaders()), [ - 'range' => '24h', - 'provider' => 'some-random-provider' ]); $this->assertEquals($response['headers']['status-code'], 400); @@ -46,38 +35,12 @@ class UsersConsoleClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'] ], $this->getHeaders()), [ 'range' => '24h', - 'provider' => 'email' ]); $this->assertEquals($response['headers']['status-code'], 200); - $this->assertEquals(count($response['body']), 9); + $this->assertEquals(count($response['body']), 3); $this->assertEquals($response['body']['range'], '24h'); - $this->assertIsArray($response['body']['usersCount']); - $this->assertIsArray($response['body']['usersCreate']); - $this->assertIsArray($response['body']['usersRead']); - $this->assertIsArray($response['body']['usersUpdate']); - $this->assertIsArray($response['body']['usersDelete']); - $this->assertIsArray($response['body']['sessionsCreate']); - $this->assertIsArray($response['body']['sessionsProviderCreate']); - $this->assertIsArray($response['body']['sessionsDelete']); - - $response = $this->client->call(Client::METHOD_GET, '/users/usage', array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'] - ], $this->getHeaders()), [ - 'range' => '24h' - ]); - - $this->assertEquals($response['headers']['status-code'], 200); - $this->assertEquals(count($response['body']), 9); - $this->assertEquals($response['body']['range'], '24h'); - $this->assertIsArray($response['body']['usersCount']); - $this->assertIsArray($response['body']['usersCreate']); - $this->assertIsArray($response['body']['usersRead']); - $this->assertIsArray($response['body']['usersUpdate']); - $this->assertIsArray($response['body']['usersDelete']); - $this->assertIsArray($response['body']['sessionsCreate']); - $this->assertIsArray($response['body']['sessionsProviderCreate']); - $this->assertIsArray($response['body']['sessionsDelete']); + $this->assertIsArray($response['body']['usersTotal']); + $this->assertIsArray($response['body']['sessionsTotal']); } } diff --git a/tests/unit/Event/EventTest.php b/tests/unit/Event/EventTest.php index a328c8d59..b48fc8f84 100644 --- a/tests/unit/Event/EventTest.php +++ b/tests/unit/Event/EventTest.php @@ -3,67 +3,74 @@ namespace Tests\Unit\Event; use Appwrite\Event\Event; +use Appwrite\URL\URL; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Utopia\App; +use Utopia\DSN\DSN; +use Utopia\Queue; +use Utopia\Queue\Client; + +require_once __DIR__ . '/../../../app/init.php'; class EventTest extends TestCase { + protected ?Event $object = null; protected string $queue = ''; public function setUp(): void { - $redisHost = App::getEnv('_APP_REDIS_HOST', ''); - $redisPort = App::getEnv('_APP_REDIS_PORT', ''); - \Resque::setBackend($redisHost . ':' . $redisPort); + $fallbackForRedis = URL::unparse([ + 'scheme' => 'redis', + 'host' => App::getEnv('_APP_REDIS_HOST', 'redis'), + 'port' => App::getEnv('_APP_REDIS_PORT', '6379'), + 'user' => App::getEnv('_APP_REDIS_USER', ''), + 'pass' => App::getEnv('_APP_REDIS_PASS', ''), + ]); + $dsn = App::getEnv('_APP_CONNECTIONS_QUEUE', $fallbackForRedis); + $dsn = explode('=', $dsn); + $dsn = $dsn[0] ?? ''; + $dsn = new DSN($dsn); + $connection = new Queue\Connection\Redis($dsn->getHost(), $dsn->getPort()); $this->queue = 'v1-tests' . uniqid(); - $this->object = new Event($this->queue, 'TestsV1'); + $this->object = new Event($connection); + $this->object->setClass('TestsV1'); + $this->object->setQueue($this->queue); } public function testQueue(): void { $this->assertEquals($this->queue, $this->object->getQueue()); - $this->object->setQueue('demo'); - $this->assertEquals('demo', $this->object->getQueue()); - $this->object->setQueue($this->queue); } public function testClass(): void { $this->assertEquals('TestsV1', $this->object->getClass()); - $this->object->setClass('TestsV2'); - $this->assertEquals('TestsV2', $this->object->getClass()); - $this->object->setClass('TestsV1'); } public function testParams(): void { + $this->object ->setParam('eventKey1', 'eventValue1') ->setParam('eventKey2', 'eventValue2'); $this->object->trigger(); - $this->assertEquals('eventValue1', $this->object->getParam('eventKey1')); $this->assertEquals('eventValue2', $this->object->getParam('eventKey2')); $this->assertEquals(null, $this->object->getParam('eventKey3')); - $this->assertEquals(\Resque::size($this->queue), 1); - } - - public function testPause(): void - { - $this->object->setPaused(true); - $this->assertTrue($this->object->isPaused()); - $this->object->setPaused(false); - $this->assertNotTrue($this->object->isPaused()); + global $register; + $pools = $register->get('pools'); + $client = new Client($this->object->getQueue(), $pools->get('queue')->pop()->getResource()); + $this->assertEquals($client->getQueueSize(), 1); } public function testReset(): void diff --git a/tests/unit/Usage/StatsTest.php b/tests/unit/Usage/StatsTest.php index f02184139..794caebdf 100644 --- a/tests/unit/Usage/StatsTest.php +++ b/tests/unit/Usage/StatsTest.php @@ -2,70 +2,53 @@ namespace Tests\Unit\Usage; -use Appwrite\Usage\Stats; +use Appwrite\URL\URL as AppwriteURL; use PHPUnit\Framework\TestCase; use Utopia\App; +use Utopia\DSN\DSN; +use Utopia\Queue; +use Utopia\Queue\Client; +use Utopia\Queue\Connection; class StatsTest extends TestCase { - /** - * @var Stats - */ - protected $object = null; + protected ?Connection $connection = null; + protected ?Client $client = null; + + protected const QUEUE_NAME = 'usage-test-q'; public function setUp(): void { - $host = App::getEnv('_APP_STATSD_HOST', 'telegraf'); - $port = App::getEnv('_APP_STATSD_PORT', 8125); + $env = App::getEnv('_APP_CONNECTIONS_QUEUE', AppwriteURL::unparse([ + 'scheme' => 'redis', + 'host' => App::getEnv('_APP_REDIS_HOST', 'redis'), + 'port' => App::getEnv('_APP_REDIS_PORT', '6379'), + 'user' => App::getEnv('_APP_REDIS_USER', ''), + 'pass' => App::getEnv('_APP_REDIS_PASS', ''), + ])); - $connection = new \Domnikl\Statsd\Connection\UdpSocket($host, $port); - $statsd = new \Domnikl\Statsd\Client($connection); - - $this->object = new Stats($statsd); + $dsn = explode('=', $env); + $dsn = count($dsn) > 1 ? $dsn[1] : $dsn[0]; + $dsn = new DSN($dsn); + $this->connection = new Queue\Connection\Redis($dsn->getHost(), $dsn->getPort()); + $this->client = new Client(self::QUEUE_NAME, $this->connection); } public function tearDown(): void { } - public function testNamespace(): void + public function testSamePayload(): void { - $this->object->setNamespace('appwritetest.usage'); - $this->assertEquals('appwritetest.usage', $this->object->getNamespace()); - } + $inToQueue = [ + 'key_1' => 'value_1', + 'key_2' => 'value_2', + ]; - public function testParams(): void - { - $this->object - ->setParam('projectId', 'appwrite_test') - ->setParam('projectInternalId', 1) - ->setParam('networkRequestSize', 100) - ; - - $this->assertEquals('appwrite_test', $this->object->getParam('projectId')); - $this->assertEquals(1, $this->object->getParam('projectInternalId')); - $this->assertEquals(100, $this->object->getParam('networkRequestSize')); - - $this->object->submit(); - - $this->assertEquals(null, $this->object->getParam('projectId')); - $this->assertEquals(null, $this->object->getParam('networkRequestSize')); - } - - public function testReset(): void - { - $this->object - ->setParam('projectId', 'appwrite_test') - ->setParam('networkRequestSize', 100) - ; - - $this->assertEquals('appwrite_test', $this->object->getParam('projectId')); - $this->assertEquals(100, $this->object->getParam('networkRequestSize')); - - $this->object->reset(); - - $this->assertEquals(null, $this->object->getParam('projectId')); - $this->assertEquals(null, $this->object->getParam('networkRequestSize')); - $this->assertEquals('appwrite.usage', $this->object->getNamespace()); + $result = $this->client->enqueue($inToQueue); + $this->assertTrue($result); + $outFromQueue = $this->connection->leftPopArray('utopia-queue.queue.' . self::QUEUE_NAME, 0)['payload']; + $this->assertNotEmpty($outFromQueue); + $this->assertSame($inToQueue, $outFromQueue); } }