From f9adffc56918315552544b17d725d1d692659c5c Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Fri, 31 Mar 2023 14:56:38 +0200 Subject: [PATCH] tests: add tests for cache on updating attributes --- app/controllers/api/databases.php | 5 +- .../Databases/DatabasesCustomServerTest.php | 107 ++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index c2ada25373..a7f4f7f928 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -348,7 +348,8 @@ function updateAttribute( } $dbForProject->updateDocument('attributes', $db->getInternalId() . '_' . $collection->getInternalId() . '_' . $key, $attribute); - $dbForProject->deleteCachedDocument('database_' . $db->getInternalId(), $collectionId); + //TODO: this purges the cache of all collections + $dbForProject->deleteCachedCollection('database_' . $db->getInternalId()); $events ->setContext('collection', $collection) @@ -668,7 +669,7 @@ App::delete('/v1/databases/:databaseId') throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove collection from DB'); } - $dbForProject->deleteCachedCollection('databases' . $database->getInternalId()); + $dbForProject->deleteCachedCollection('databases_' . $database->getInternalId()); $deletes ->setType(DELETE_TYPE_DOCUMENT) diff --git a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php index fa9cd30c2c..ce6602c176 100644 --- a/tests/e2e/Services/Databases/DatabasesCustomServerTest.php +++ b/tests/e2e/Services/Databases/DatabasesCustomServerTest.php @@ -1426,6 +1426,17 @@ class DatabasesCustomServerTest extends Scope $this->assertFalse($new['body']['required']); $this->assertEquals('lorem', $new['body']['default']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals('lorem', $attribute['default']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/string/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -1557,6 +1568,17 @@ class DatabasesCustomServerTest extends Scope $this->assertFalse($new['body']['required']); $this->assertEquals('torsten@appwrite.io', $new['body']['default']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals('torsten@appwrite.io', $attribute['default']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/email/' . $key, array_merge([ 'content-type' => 'application/json', @@ -1689,6 +1711,17 @@ class DatabasesCustomServerTest extends Scope $this->assertFalse($new['body']['required']); $this->assertEquals('127.0.0.1', $new['body']['default']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals('127.0.0.1', $attribute['default']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/ip/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -1820,6 +1853,17 @@ class DatabasesCustomServerTest extends Scope $this->assertFalse($new['body']['required']); $this->assertEquals('http://appwrite.io', $new['body']['default']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals('http://appwrite.io', $attribute['default']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/url/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -1955,6 +1999,19 @@ class DatabasesCustomServerTest extends Scope $this->assertEquals(0, $new['body']['min']); $this->assertEquals(1000, $new['body']['max']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals(123, $attribute['default']); + $this->assertEquals(0, $attribute['min']); + $this->assertEquals(1000, $attribute['max']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/integer/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -2205,6 +2262,19 @@ class DatabasesCustomServerTest extends Scope $this->assertEquals(0, $new['body']['min']); $this->assertEquals(1000, $new['body']['max']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals(123.456, $attribute['default']); + $this->assertEquals(0, $attribute['min']); + $this->assertEquals(1000, $attribute['max']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/float/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -2451,6 +2521,17 @@ class DatabasesCustomServerTest extends Scope $this->assertFalse($new['body']['required']); $this->assertEquals(true, $new['body']['default']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals(true, $attribute['default']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/boolean/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -2582,6 +2663,17 @@ class DatabasesCustomServerTest extends Scope $this->assertFalse($new['body']['required']); $this->assertEquals('1975-06-12 14:12:55+02:00', $new['body']['default']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals('1975-06-12 14:12:55+02:00', $attribute['default']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/datetime/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -2718,6 +2810,21 @@ class DatabasesCustomServerTest extends Scope $this->assertContains('ipsum', $new['body']['elements']); $this->assertContains('dolor', $new['body']['elements']); + $new = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $collectionId, array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $attribute = array_values(array_filter($new['body']['attributes'], fn (array $a) => $a['key'] === $key))[0] ?? null; + $this->assertNotNull($attribute); + $this->assertFalse($attribute['required']); + $this->assertEquals('lorem', $attribute['default']); + $this->assertCount(3, $attribute['elements']); + $this->assertContains('lorem', $attribute['elements']); + $this->assertContains('ipsum', $attribute['elements']); + $this->assertContains('dolor', $attribute['elements']); + $update = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $collectionId . '/attributes/enum/' . $key, array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'],