Skip to content

Commit

Permalink
fix external buckets tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zajca committed Jun 6, 2024
1 parent a331eca commit 8865bbe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 73 deletions.
5 changes: 2 additions & 3 deletions tests/Backend/ExternalBuckets/BaseExternalBuckets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Keboola\Test\Backend\ExternalBuckets;

use Keboola\StorageApi\Client;
use Keboola\Test\StorageApiTestCase;
use Keboola\Test\Backend\Workspaces\ParallelWorkspacesTestCase;
use Keboola\Test\Utils\MetadataUtils;

abstract class BaseExternalBuckets extends StorageApiTestCase
abstract class BaseExternalBuckets extends ParallelWorkspacesTestCase
{
use MetadataUtils;

Expand Down
84 changes: 14 additions & 70 deletions tests/Backend/ExternalBuckets/SnowflakeRegisterBucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ public function testRegisterTableWithWrongName(): void
{
$this->dropBucketIfExists($this->_client, 'in.bucket-registration-wrong-table-name', true);

$ws = new Workspaces($this->_client);
// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();
$externalBucketPath = [$workspace['connection']['database'], $workspace['connection']['schema']];
$externalBucketBackend = 'snowflake';

Expand Down Expand Up @@ -142,9 +141,8 @@ public function testRegisterTablesWithDuplicateNameWithDifferentCase(): void
{
$this->dropBucketIfExists($this->_client, 'in.bucket-registration-duplicate-table-name', true);

$ws = new Workspaces($this->_client);
// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();
$externalBucketPath = [$workspace['connection']['database'], $workspace['connection']['schema']];
$externalBucketBackend = 'snowflake';

Expand Down Expand Up @@ -223,7 +221,7 @@ public function testRegisterWSAsExternalBucket(): void
$ws = new Workspaces($this->_client);

// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();

$externalBucketPath = [$workspace['connection']['database'], $workspace['connection']['schema']];
$externalBucketBackend = 'snowflake';
Expand Down Expand Up @@ -476,10 +474,8 @@ public function testAliasFromExternalBucketNotAllowed(): void
$this->dropBucketIfExists($this->_client, 'in.test-bucket-registration', true);
$this->initEvents($this->_client);

$ws = new Workspaces($this->_client);

// prepare workspace
$workspace = $ws->createWorkspace([], true);
$workspace = $this->initTestWorkspace();
$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace);
$db->createTable('TEST', ['AMOUNT' => 'NUMBER', 'DESCRIPTION' => 'TEXT']);
$db->executeQuery('INSERT INTO "TEST" VALUES (1, \'test\')');
Expand Down Expand Up @@ -530,9 +526,8 @@ public function testRegistrationOfExternalTable(): void
$this->dropBucketIfExists($this->_client, 'in.test-bucket-registration', true);
$this->initEvents($this->_client);

$ws = new Workspaces($this->_client);
// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();

$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace);

Expand Down Expand Up @@ -611,9 +606,8 @@ public function testRegistrationOfView(): void
$this->dropBucketIfExists($this->_client, 'in.test-bucket-registration', true);
$this->initEvents($this->_client);

$ws = new Workspaces($this->_client);
// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();

$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace);

Expand Down Expand Up @@ -669,8 +663,7 @@ public function testCreateSnapshotFromExternalBucketIsNotSupported(): void

$this->dropBucketIfExists($this->_client, $bucketId, true);

$ws = new Workspaces($this->_client);
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();

$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace);

Expand Down Expand Up @@ -705,8 +698,7 @@ public function testAlteredColumnThrowsUserExAndAfterRefreshWillWork(): void

$this->dropBucketIfExists($this->_client, $bucketId, true);

$ws = new Workspaces($this->_client);
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();

$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace);

Expand Down Expand Up @@ -946,9 +938,7 @@ public function testRegisterExternalDB(): void
$this->assertCount(3, $tables);

// check that workspace user CAN READ from table in external bucket directly
$ws = new Workspaces($this->_client);

$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();
$db = WorkspaceBackendFactory::createWorkspaceBackend($workspace);

assert($db instanceof SnowflakeWorkspaceBackend);
Expand Down Expand Up @@ -999,59 +989,14 @@ public function testRegisterExternalDB(): void
}
}


public function testRegisterExternalDBWithNoWS(): void
{
$wsService = new Workspaces($this->_client);
$allWorkspacesInThisProject = $wsService->listWorkspaces();
foreach ($allWorkspacesInThisProject as $workspace) {
$wsService->deleteWorkspace($workspace['id']);
}

$this->dropBucketIfExists($this->_client, 'in.test-bucket-reg-ext-no-ws', true);
$this->initEvents($this->_client);
$runId = $this->setRunId();
// try same with schema outside of project database.
// This DB has been created when test project was inited
$idOfBucket = $this->_client->registerBucket(
'test-bucket-reg-ext-no-ws',
['TEST_EXTERNAL_BUCKETS', 'TEST_SCHEMA'],
'in',
'Iam in other database',
'snowflake',
'Iam-from-external-db-ext-no-ws',
);

$assertCallback = function ($events) {
$this->assertCount(1, $events);
};
$query = new EventsQueryBuilder();
$query->setEvent('storage.bucketCreated')
->setTokenId($this->tokenId)
->setRunId($runId);
$this->assertEventWithRetries($this->_client, $assertCallback, $query);

$assertCallback = function ($events) {
$this->assertCount(3, $events);
};
$query = new EventsQueryBuilder();
$query->setEvent('storage.tableCreated')
->setTokenId($this->tokenId)
->setRunId($runId);
$this->assertEventWithRetries($this->_client, $assertCallback, $query);

// it should be easily dropped even no WS exists
$this->_client->dropBucket($idOfBucket, ['force' => true]);
}

public function testRefreshBucketWhenSchemaDoesNotExist(): void
{
$this->dropBucketIfExists($this->_client, 'in.test-bucket-registration', true);
$this->initEvents($this->_client);

$ws = new Workspaces($this->_client);
// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();

// register workspace as external bucket including external table
$runId = $this->setRunId();
Expand Down Expand Up @@ -1107,7 +1052,7 @@ public function testDropBucketWhenSchemaDoesNotExist(): void

$ws = new Workspaces($this->_client);
// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();

// register workspace as external bucket including external table
$runId = $this->setRunId();
Expand Down Expand Up @@ -1144,9 +1089,8 @@ public function testDropExternalBucket(): void
$this->dropBucketIfExists($this->_client, 'in.external_bucket_1', true);
$this->dropBucketIfExists($this->_client, 'in.external_bucket_2', true);

$ws = new Workspaces($this->_client);
// prepare workspace
$workspace = $ws->createWorkspace();
$workspace = $this->initTestWorkspace();
$externalBucketPath = [$workspace['connection']['database'], $workspace['connection']['schema']];

// add first table to workspace with long name, table should be skipped
Expand All @@ -1161,7 +1105,7 @@ public function testDropExternalBucket(): void
$db->executeQuery('INSERT INTO "test1" VALUES (0)');

// prepare workspace
$workspace2 = $ws->createWorkspace();
$workspace2 = $this->initTestWorkspace();
$externalBucketPath2 = [$workspace2['connection']['database'], $workspace2['connection']['schema']];

// add first table to workspace with long name, table should be skipped
Expand Down Expand Up @@ -1194,7 +1138,7 @@ public function testDropExternalBucket(): void
);

// workspace which will check data using RO-IM
$workspaceForChecking = $ws->createWorkspace();
$workspaceForChecking = $this->initTestWorkspace();
/** @var SnowflakeConnection $workspaceDbForChecking */
$workspaceDbForChecking = WorkspaceBackendFactory::createWorkspaceBackend($workspaceForChecking)->getDb();

Expand Down

0 comments on commit 8865bbe

Please sign in to comment.