diff --git a/composer.json b/composer.json index 522d17749..102a68369 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "microsoft/azure-storage-blob": "^1.4" }, "require-dev": { - "phpunit/phpunit": "^5.6", + "phpunit/phpunit": "7.5.*", + "brianium/paratest" :"3.*", "squizlabs/php_codesniffer": "^3", "apigen/apigen": "4.0.0-RC4", "keboola/php-csv-db-import": "^2.2", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9630a4b5f..08b726200 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,103 +9,120 @@ stopOnFailure="false" stopOnError="false" syntaxCheck="false" - verbose="true" + verbose="true" bootstrap="tests/bootstrap.php"> - - tests/Common - tests/Options - tests/S3Uploader - tests/Downloader - tests/File/CommonFileTests.php - tests/File/AwsFileTest.php - + + + + + + + + + + + + + - - tests/File/AzureFileTest.php - tests/File/CommonFileTest.php - + + + tests/Common + tests/Options + tests/S3Uploader + tests/Downloader + tests/File/CommonFileTests.php + tests/File/AwsFileTest.php + - - tests/Backend/Workspaces/MetadataFromSnowflakeWorkspaceTest.php - tests/Backend/CommonPart1 - tests/Backend/Workspaces/ComponentsWorkspacesTest.php - tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php - tests/Backend/Workspaces/LegacyWorkspacesSnowflakeTest.php - + + tests/File/AzureFileTest.php + tests/File/CommonFileTest.php + - - tests/Backend/CommonPart2 - tests/Backend/Workspaces/WorkspacesSnowflakeTest.php - tests/Backend/Workspaces/WorkspacesTest.php - tests/Backend/Workspaces/WorkspacesUnloadTest.php - tests/Backend/Workspaces/WorkspacesLoadTest.php - tests/Backend/Workspaces/WorkspacesRenameLoadTest.php - tests/Backend/Snowflake - tests/Backend/Export/ExportParamsTest.php - + + tests/Backend/Workspaces/MetadataFromSnowflakeWorkspaceTest.php + tests/Backend/CommonPart1 + tests/Backend/Workspaces/ComponentsWorkspacesTest.php + tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php + tests/Backend/Workspaces/LegacyWorkspacesSnowflakeTest.php + - - tests/Backend/Workspaces/MetadataFromRedshiftWorkspaceTest.php - tests/Backend/CommonPart1 - tests/Backend/Workspaces/ComponentsWorkspacesTest.php - tests/Backend/Workspaces/LegacyWorkspacesRedshiftTest.php - tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php - + + tests/Backend/CommonPart2 + tests/Backend/Workspaces/WorkspacesSnowflakeTest.php + tests/Backend/Workspaces/WorkspacesTest.php + tests/Backend/Workspaces/WorkspacesUnloadTest.php + tests/Backend/Workspaces/WorkspacesLoadTest.php + tests/Backend/Workspaces/WorkspacesRenameLoadTest.php + tests/Backend/Snowflake + tests/Backend/Export/ExportParamsTest.php + - - tests/Backend/CommonPart2 - tests/Backend/Workspaces/WorkspacesRedshiftTest.php - tests/Backend/Workspaces/WorkspacesTest.php - tests/Backend/Workspaces/WorkspacesUnloadTest.php - tests/Backend/Workspaces/WorkspacesLoadTest.php - tests/Backend/Workspaces/WorkspacesRenameLoadTest.php - tests/Backend/Redshift - tests/Backend/Export/ExportParamsTest.php - + + tests/Backend/Workspaces/MetadataFromRedshiftWorkspaceTest.php + tests/Backend/CommonPart1 + tests/Backend/Workspaces/ComponentsWorkspacesTest.php + tests/Backend/Workspaces/LegacyWorkspacesRedshiftTest.php + tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php + - - tests/Backend/Workspaces/MetadataFromSnowflakeWorkspaceTest.php - tests/Backend/CommonPart1 - tests/Backend/Workspaces/ComponentsWorkspacesTest.php - tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php - tests/Backend/Workspaces/LegacyWorkspacesSnowflakeTest.php - + + tests/Backend/CommonPart2 + tests/Backend/Workspaces/WorkspacesRedshiftTest.php + tests/Backend/Workspaces/WorkspacesTest.php + tests/Backend/Workspaces/WorkspacesUnloadTest.php + tests/Backend/Workspaces/WorkspacesLoadTest.php + tests/Backend/Workspaces/WorkspacesRenameLoadTest.php + tests/Backend/Redshift + tests/Backend/Export/ExportParamsTest.php + - - tests/Backend/CommonPart2 - tests/Backend/Workspaces/WorkspacesSnowflakeTest.php - tests/Backend/Workspaces/WorkspacesTest.php - tests/Backend/Workspaces/WorkspacesUnloadTest.php - tests/Backend/Workspaces/WorkspacesLoadTest.php - tests/Backend/Workspaces/WorkspacesRenameLoadTest.php - tests/Backend/Snowflake - tests/Backend/Export/ExportParamsTest.php - + + tests/Backend/CommonPart1/BucketsTest.php + tests/Backend/Workspaces/MetadataFromSnowflakeWorkspaceTest.php + tests/Backend/CommonPart1 + tests/Backend/Workspaces/ComponentsWorkspacesTest.php + tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php + tests/Backend/Workspaces/LegacyWorkspacesSnowflakeTest.php + - - tests/Backend/Mixed - tests/Backend/Mixed/Workspaces/S3WorkspacesTest.php - + + tests/Backend/CommonPart2 + tests/Backend/Workspaces/WorkspacesSnowflakeTest.php + tests/Backend/Workspaces/WorkspacesTest.php + tests/Backend/Workspaces/WorkspacesUnloadTest.php + tests/Backend/Workspaces/WorkspacesLoadTest.php + tests/Backend/Workspaces/WorkspacesRenameLoadTest.php + tests/Backend/Snowflake + tests/Backend/Export/ExportParamsTest.php + - - tests/Backend/Workspaces/MetadataFromSynapseWorkspaceTest.php - tests/Backend/CommonPart1 - - tests/Backend/CommonPart1/DataPreviewLimitsTest.php - tests/Backend/Workspaces/ComponentsWorkspacesTest.php - tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php - tests/Backend/Workspaces/LegacyWorkspacesSynapseTest.php - + + tests/Backend/Mixed + tests/Backend/Mixed/Workspaces/S3WorkspacesTest.php + - - tests/Backend/CommonPart2 - tests/Backend/Workspaces/WorkspacesSynapseTest.php - tests/Backend/Workspaces/WorkspacesTest.php - tests/Backend/Workspaces/SynapseWorkspacesUnloadTest.php - tests/Backend/Workspaces/WorkspacesLoadTest.php - tests/Backend/Workspaces/WorkspacesRenameLoadTest.php - tests/Backend/Synapse - tests/Backend/Export/ExportParamsTest.php - + + tests/Backend/Workspaces/MetadataFromSynapseWorkspaceTest.php + tests/Backend/CommonPart1 + + tests/Backend/CommonPart1/DataPreviewLimitsTest.php + tests/Backend/Workspaces/ComponentsWorkspacesTest.php + tests/Backend/Workspaces/LegacyWorkspacesLoadTest.php + tests/Backend/Workspaces/LegacyWorkspacesSynapseTest.php + + + + tests/Backend/CommonPart2 + tests/Backend/Workspaces/WorkspacesSynapseTest.php + tests/Backend/Workspaces/WorkspacesTest.php + tests/Backend/Workspaces/SynapseWorkspacesUnloadTest.php + tests/Backend/Workspaces/WorkspacesLoadTest.php + tests/Backend/Workspaces/WorkspacesRenameLoadTest.php + tests/Backend/Synapse + tests/Backend/Export/ExportParamsTest.php + + diff --git a/tests/Backend/CommonPart1/BucketsTest.php b/tests/Backend/CommonPart1/BucketsTest.php index b7e4861b2..834398c56 100644 --- a/tests/Backend/CommonPart1/BucketsTest.php +++ b/tests/Backend/CommonPart1/BucketsTest.php @@ -8,11 +8,8 @@ */ namespace Keboola\Test\Backend\CommonPart1; -use Keboola\StorageApi\ClientException; use Keboola\StorageApi\Metadata; -use Keboola\StorageApi\Options\BucketUpdateOptions; use Keboola\Test\StorageApiTestCase; -use Keboola\Csv\CsvFile; class BucketsTest extends StorageApiTestCase { @@ -22,116 +19,11 @@ public function setUp() $this->_initEmptyTestBuckets(); } - public function testBucketsList() - { - $buckets = $this->_client->listBuckets(); - - $this->assertTrue(count($buckets) >= 2); - - $inBucketFound = false; - $outBucketFound = false; - foreach ($buckets as $bucket) { - if ($bucket['id'] == $this->getTestBucketId(self::STAGE_IN)) { - $inBucketFound = true; - } - if ($bucket['id'] == $this->getTestBucketId(self::STAGE_OUT)) { - $outBucketFound = true; - } - } - $this->assertTrue($inBucketFound); - $this->assertTrue($outBucketFound); - - $firstBucket = reset($buckets); - $this->assertArrayHasKey('attributes', $firstBucket); - $this->assertArrayHasKey('displayName', $firstBucket); - $this->assertNotEquals('', $firstBucket['displayName']); - } - - public function testBucketDetail() - { - $displayName = "Romanov-Bucket"; - $bucketName = 'BucketsTest_testBucketDetail'; - - $tokenData = $this->_client->verifyToken(); - $this->dropBucketIfExists($this->_client, 'in.c-' . $bucketName); - $bucketId = $this->_client->createBucket($bucketName, self::STAGE_IN); - - $bucket = $this->_client->getBucket($bucketId); - - $this->assertFalse($bucket['directAccessEnabled']); - $this->assertNull($bucket['directAccessSchemaName']); - - $this->assertEquals($tokenData['owner']['defaultBackend'], $bucket['backend']); - $this->assertNotEquals($displayName, $bucket['displayName']); - - $asyncBucketDisplayName = $displayName . '-async'; - $bucketUpdateOptions = new BucketUpdateOptions($bucketId, $asyncBucketDisplayName, true); - $this->_client->updateBucket($bucketUpdateOptions); - - $bucket = $this->_client->getBucket($bucketId); - $this->assertEquals($asyncBucketDisplayName, $bucket['displayName']); - - $bucketUpdateOptions = new BucketUpdateOptions($bucketId, $displayName); - $bucket = $this->_client->updateBucket($bucketUpdateOptions); - try { - $this->_client->createBucket($displayName, self::STAGE_IN); - } catch (\Keboola\StorageApi\ClientException $e) { - $this->assertSame('The display name "Romanov-Bucket" already exists in project.', $e->getMessage()); - }; - - $this->assertEquals($displayName, $bucket['displayName']); - - $bucketUpdateOptions = new BucketUpdateOptions($this->getTestBucketId(), $displayName); - try { - $this->_client->updateBucket($bucketUpdateOptions); - $this->fail('The display name already exists in project'); - } catch (\Keboola\StorageApi\ClientException $e) { - $this->assertEquals('The display name "' . $displayName . '" already exists in project.', $e->getMessage()); - $this->assertEquals('storage.buckets.alreadyExists', $e->getStringCode()); - $this->assertEquals(400, $e->getCode()); - } - - $bucketUpdateOptions = new BucketUpdateOptions($bucketId, '$$$$$'); - try { - $this->_client->updateBucket($bucketUpdateOptions); - $this->fail('Wrong display name'); - } catch (\Keboola\StorageApi\ClientException $e) { - $this->assertEquals('Invalid data - displayName: Only alphanumeric characters dash and underscores are allowed.', $e->getMessage()); - $this->assertEquals('storage.buckets.validation', $e->getStringCode()); - $this->assertEquals(400, $e->getCode()); - } - - $bucket = $this->_client->getBucket($bucketId); - $this->assertEquals($displayName, $bucket['displayName']); - - // renaming bucket to the same name should be successful - $bucketUpdateOptions = new BucketUpdateOptions($bucketId, $displayName); - $bucket = $this->_client->updateBucket($bucketUpdateOptions); - - $this->_client->dropBucket($bucket['id']); - } - - public function testBucketEvents() - { - $events = $this->_client->listBucketEvents($this->getTestBucketId()); - $this->assertNotEmpty($events); - } - - public function testBucketsListWithIncludeParameter() - { - $buckets = $this->_client->listBuckets(array( - 'include' => '', - )); - - $firstBucket = reset($buckets); - $this->assertArrayNotHasKey('attributes', $firstBucket); - } - public function testBucketsListWithIncludeMetadata() { - $buckets = $this->_client->listBuckets(array( + $buckets = $this->_client->listBuckets([ 'include' => 'metadata', - )); + ]); $firstBucket = reset($buckets); $this->assertArrayNotHasKey('attributes', $firstBucket); @@ -142,13 +34,13 @@ public function testBucketsListWithIncludeMetadata() $metadataApi->postBucketMetadata($firstBucket['id'], 'storage-php-client-test', [ [ 'key' => 'test-key', - 'value' => 'test-value' - ] + 'value' => 'test-value', + ], ]); - $buckets = $this->_client->listBuckets(array( + $buckets = $this->_client->listBuckets([ 'include' => 'metadata', - )); + ]); $firstBucket = reset($buckets); $this->assertArrayHasKey('metadata', $firstBucket); $this->assertCount(1, $firstBucket['metadata']); @@ -157,299 +49,4 @@ public function testBucketsListWithIncludeMetadata() $this->assertArrayHasKey('value', $firstBucket['metadata'][0]); $this->assertEquals('test-value', $firstBucket['metadata'][0]['value']); } - - public function testBucketCreateWithInvalidBackend() - { - try { - $this->_client->createBucket('unknown-backend', 'in', 'desc', 'redshit'); - $this->fail('Exception should be thrown'); - } catch (\Keboola\StorageApi\ClientException $e) { - $this->assertEquals('storage.buckets.validation', $e->getStringCode()); - } - } - - public function testBucketManipulation() - { - $tokenData = $this->_client->verifyToken(); - - $bucketData = [ - 'name' => 'test', - 'displayName' => 'test-display-name', - 'stage' => 'in', - 'description' => 'this is just a test', - ]; - - $testBucketId = $bucketData['stage'] . '.c-'.$bucketData['name']; - - $this->dropBucketIfExists($this->_client, $testBucketId); - - $newBucketId = $this->_client->createBucket( - $bucketData['name'], - $bucketData['stage'], - $bucketData['description'], - null, - $bucketData['displayName'] - ); - - $importFile = __DIR__ . '/../../_data/languages.csv'; - // create and import data into source table - $sourceTableId = $this->_client->createTable( - $newBucketId, - 'languages', - new CsvFile($importFile) - ); - - try { - $this->_client->dropBucket($newBucketId); - } catch (ClientException $e) { - $this->assertSame('Only empty buckets can be deleted. There are 1 tables in the bucket.', $e->getMessage()); - $this->assertSame('buckets.deleteNotEmpty', $e->getStringCode()); - } - try { - $this->_client->dropBucket($newBucketId, ['async' => true]); - } catch (ClientException $e) { - $this->assertSame('Only empty buckets can be deleted. There are 1 tables in the bucket.', $e->getMessage()); - $this->assertSame('buckets.deleteNotEmpty', $e->getStringCode()); - } - - $this->_client->dropBucket($newBucketId, ['force' => true]); - - $newBucketId = $this->_client->createBucket( - $bucketData['name'], - $bucketData['stage'], - $bucketData['description'], - null, - $bucketData['displayName'] - ); - - $importFile = __DIR__ . '/../../_data/languages.csv'; - // create and import data into source table - $sourceTableId = $this->_client->createTable( - $newBucketId, - 'languages', - new CsvFile($importFile) - ); - - $this->_client->dropBucket($newBucketId, ['async' => true, 'force' => true]); - - $newBucketId = $this->_client->createBucket( - $bucketData['name'], - $bucketData['stage'], - $bucketData['description'], - null, - $bucketData['displayName'] - ); - - $newBucket = $this->_client->getBucket($newBucketId); - $this->assertEquals('c-' . $bucketData['name'], $newBucket['name'], 'bucket name'); - $this->assertEquals($bucketData['displayName'], $newBucket['displayName'], 'bucket displayName'); - $this->assertEquals($bucketData['stage'], $newBucket['stage'], 'bucket stage'); - $this->assertEquals($bucketData['description'], $newBucket['description'], 'bucket description'); - $this->assertEquals($tokenData['owner']['defaultBackend'], $newBucket['backend'], 'backend'); - - // check if bucket is in list - $buckets = $this->_client->listBuckets(); - $this->assertTrue(in_array($newBucketId, array_map(function ($bucket) { - return $bucket['id']; - }, $buckets))); - - try { - $this->_client->createBucket( - $bucketData['name'] . '-' . time(), - $bucketData['stage'], - $bucketData['description'], - null, - $bucketData['displayName'] - ); - $this->fail('Display name already exist for project'); - } catch (\Keboola\StorageApi\ClientException $e) { - $this->assertEquals('The display name "test-display-name" already exists in project.', $e->getMessage()); - $this->assertEquals('storage.buckets.alreadyExists', $e->getStringCode()); - } - - try { - $this->_client->createBucket( - $bucketData['name'] . '-' . time(), - $bucketData['stage'], - $bucketData['description'], - null, - '$$$$$' - ); - $this->fail('Display name provided is invalid'); - } catch (\Keboola\StorageApi\ClientException $e) { - $this->assertEquals('Invalid data - displayName: Only alphanumeric characters dash and underscores are allowed.', $e->getMessage()); - $this->assertEquals('storage.buckets.validation', $e->getStringCode()); - } - - $this->_client->dropBucket($newBucket['id']); - - $newBucketId = $this->_client->createBucket( - $bucketData['name'], - $bucketData['stage'], - $bucketData['description'] - ); - - $newBucket = $this->_client->getBucket($newBucketId); - $this->assertEquals('c-' . $bucketData['name'], $newBucket['name'], 'bucket name'); - $this->assertEquals($bucketData['name'], $newBucket['displayName'], 'bucket displayName'); - $this->assertEquals($bucketData['stage'], $newBucket['stage'], 'bucket stage'); - $this->assertEquals($bucketData['description'], $newBucket['description'], 'bucket description'); - $this->assertEquals($tokenData['owner']['defaultBackend'], $newBucket['backend'], 'backend'); - - // check if bucket is in list - $buckets = $this->_client->listBuckets(); - $this->assertTrue(in_array($newBucketId, array_map(function ($bucket) { - return $bucket['id']; - }, $buckets))); - } - - public function testBucketCreateWithoutDescription() - { - $bucketId = $this->_client->createBucket('something', self::STAGE_IN); - $bucket = $this->_client->getBucket($bucketId); - $this->assertEmpty($bucket['description']); - $this->_client->dropBucket($bucket['id']); - } - - public function testBucketAttributes() - { - $bucketId = $this->getTestBucketId(); - - $bucket = $this->_client->getBucket($bucketId); - $this->assertEmpty($bucket['attributes'], 'empty attributes'); - - - // create - $this->_client->setBucketAttribute($bucketId, 's', 'lala'); - $this->_client->setBucketAttribute($bucketId, 'other', 'hello', true); - $bucket = $this->_client->getBucket($bucketId); - $this->assertArrayEqualsSorted(array( - array( - 'name' => 's', - 'value' => 'lala', - 'protected' => false, - ), - array( - 'name' => 'other', - 'value' => 'hello', - 'protected' => true, - ), - ), $bucket['attributes'], 'name', 'attribute set'); - - // update - $this->_client->setBucketAttribute($bucketId, 's', 'papa'); - $bucket = $this->_client->getBucket($bucketId); - $this->assertArrayEqualsSorted($bucket['attributes'], array( - array( - 'name' => 's', - 'value' => 'papa', - 'protected' => false, - ), - array( - 'name' => 'other', - 'value' => 'hello', - 'protected' => true, - ), - ), 'name', 'attribute update'); - - // delete - $this->_client->deleteBucketAttribute($bucketId, 's'); - $bucket = $this->_client->getBucket($bucketId); - $this->assertArrayEqualsSorted($bucket['attributes'], array( - array( - 'name' => 'other', - 'value' => 'hello', - 'protected' => true, - ) - ), 'name', 'attribute delete'); - - $this->_client->deleteBucketAttribute($bucketId, 'other'); - } - - public function testBucketExists() - { - $this->assertTrue($this->_client->bucketExists($this->getTestBucketId())); - $this->assertFalse($this->_client->bucketExists('in.ukulele')); - } - - public function testBucketAttributesReplace() - { - $bucketId = $this->getTestBucketId(); - $this->clearBucketAttributes($bucketId); - $this->_client->setBucketAttribute($bucketId, 'first', 'something'); - - $newAttributes = array( - array( - 'name' => 'new', - 'value' => 'new', - ), - array( - 'name' => 'second', - 'value' => 'second value', - 'protected' => true, - ), - ); - $this->_client->replaceBucketAttributes($bucketId, $newAttributes); - - $bucket = $this->_client->getBucket($bucketId); - $this->assertCount(count($newAttributes), $bucket['attributes']); - - $this->assertEquals($newAttributes[0]['name'], $bucket['attributes'][0]['name']); - $this->assertEquals($newAttributes[0]['value'], $bucket['attributes'][0]['value']); - $this->assertFalse($bucket['attributes'][0]['protected']); - } - - public function testBucketAttributesClear() - { - $bucketId = $this->getTestBucketId(); - $this->clearBucketAttributes($bucketId); - - $this->_client->replaceBucketAttributes($bucketId); - $bucket = $this->_client->getBucket($bucketId); - - $this->assertEmpty($bucket['attributes']); - } - - /** - * @param $attributes - * @dataProvider invalidAttributes - */ - public function testBucketAttributesReplaceValidation($attributes) - { - $bucketId = $this->getTestBucketId(); - $this->clearBucketAttributes($bucketId); - - try { - $this->_client->replaceBucketAttributes($bucketId, $attributes); - $this->fail('Attributes should be invalid'); - } catch (\Keboola\StorageApi\ClientException $e) { - $this->assertEquals('storage.attributes.validation', $e->getStringCode()); - } - } - - public function invalidAttributes() - { - return array( - array( - array( - array( - 'nome' => 'ukulele', - ), - array( - 'name' => 'jehovista', - ), - ), - ) - ); - } - - - private function clearBucketAttributes($bucketId) - { - $bucket = $this->_client->getBucket($bucketId); - - foreach ($bucket['attributes'] as $attribute) { - $this->_client->deleteBucketAttribute($bucketId, $attribute['name']); - } - } } diff --git a/tests/Common/TokensShareTest.php b/tests/Common/TokensShareTest.php index fb01ff2d0..a61fde87a 100644 --- a/tests/Common/TokensShareTest.php +++ b/tests/Common/TokensShareTest.php @@ -19,6 +19,9 @@ public function testMasterTokenShouldNotBeShareable() } } + /** + * @doesNotPerformAssertions + */ public function testTokenShare() { $newTokenId = $this->_client->createToken(new TokenCreateOptions()); diff --git a/tests/Downloader/AbsUrlParserTest.php b/tests/Downloader/AbsUrlParserTest.php index 0b67dc7f1..7e36f210f 100644 --- a/tests/Downloader/AbsUrlParserTest.php +++ b/tests/Downloader/AbsUrlParserTest.php @@ -4,7 +4,7 @@ use Keboola\StorageApi\Downloader\AbsUrlParser; -class AbsUrlParserTest extends \PHPUnit_Framework_TestCase +class AbsUrlParserTest extends \PHPUnit\Framework\TestCase { public function testParseAbsUrlAzure() { diff --git a/tests/Parallel/Backend/Common/BucketsTest.php b/tests/Parallel/Backend/Common/BucketsTest.php new file mode 100644 index 000000000..a21a482a2 --- /dev/null +++ b/tests/Parallel/Backend/Common/BucketsTest.php @@ -0,0 +1,415 @@ +_initEmptyTestBuckets([self::STAGE_OUT, self::STAGE_IN], $this->getBucketNameForTest()); + } + + public function testBucketsList() + { + $buckets = $this->_client->listBuckets(); + + $this->assertTrue(count($buckets) >= 2); + + $inBucketFound = false; + $outBucketFound = false; + foreach ($buckets as $bucket) { + if ($bucket['id'] == $this->getTestBucketId(self::STAGE_IN)) { + $inBucketFound = true; + } + if ($bucket['id'] == $this->getTestBucketId(self::STAGE_OUT)) { + $outBucketFound = true; + } + } + $this->assertTrue($inBucketFound); + $this->assertTrue($outBucketFound); + + $firstBucket = reset($buckets); + $this->assertArrayHasKey('attributes', $firstBucket); + $this->assertArrayHasKey('displayName', $firstBucket); + $this->assertNotEquals('', $firstBucket['displayName']); + } + + public function testBucketDetail() + { + $displayName = "Romanov-Bucket"; + $bucketName = 'BucketsTest_testBucketDetail'; + + $tokenData = $this->_client->verifyToken(); + $this->dropBucketIfExists($this->_client, 'in.c-' . $bucketName); + $bucketId = $this->_client->createBucket($bucketName, self::STAGE_IN); + + $bucket = $this->_client->getBucket($bucketId); + + $this->assertFalse($bucket['directAccessEnabled']); + $this->assertNull($bucket['directAccessSchemaName']); + + $this->assertEquals($tokenData['owner']['defaultBackend'], $bucket['backend']); + $this->assertNotEquals($displayName, $bucket['displayName']); + + $asyncBucketDisplayName = $displayName . '-async'; + $bucketUpdateOptions = new BucketUpdateOptions($bucketId, $asyncBucketDisplayName, true); + $this->_client->updateBucket($bucketUpdateOptions); + + $bucket = $this->_client->getBucket($bucketId); + $this->assertEquals($asyncBucketDisplayName, $bucket['displayName']); + + $bucketUpdateOptions = new BucketUpdateOptions($bucketId, $displayName); + $bucket = $this->_client->updateBucket($bucketUpdateOptions); + try { + $this->_client->createBucket($displayName, self::STAGE_IN); + } catch (\Keboola\StorageApi\ClientException $e) { + $this->assertSame('The display name "Romanov-Bucket" already exists in project.', $e->getMessage()); + }; + + $this->assertEquals($displayName, $bucket['displayName']); + + $bucketUpdateOptions = new BucketUpdateOptions($this->getTestBucketId(), $displayName); + try { + $this->_client->updateBucket($bucketUpdateOptions); + $this->fail('The display name already exists in project'); + } catch (\Keboola\StorageApi\ClientException $e) { + $this->assertEquals('The display name "' . $displayName . '" already exists in project.', $e->getMessage()); + $this->assertEquals('storage.buckets.alreadyExists', $e->getStringCode()); + $this->assertEquals(400, $e->getCode()); + } + + $bucketUpdateOptions = new BucketUpdateOptions($bucketId, '$$$$$'); + try { + $this->_client->updateBucket($bucketUpdateOptions); + $this->fail('Wrong display name'); + } catch (\Keboola\StorageApi\ClientException $e) { + $this->assertEquals('Invalid data - displayName: Only alphanumeric characters dash and underscores are allowed.', $e->getMessage()); + $this->assertEquals('storage.buckets.validation', $e->getStringCode()); + $this->assertEquals(400, $e->getCode()); + } + + $bucket = $this->_client->getBucket($bucketId); + $this->assertEquals($displayName, $bucket['displayName']); + + // renaming bucket to the same name should be successful + $bucketUpdateOptions = new BucketUpdateOptions($bucketId, $displayName); + $bucket = $this->_client->updateBucket($bucketUpdateOptions); + + $this->_client->dropBucket($bucket['id']); + } + + public function testBucketEvents() + { + $events = $this->_client->listBucketEvents($this->getTestBucketId()); + $this->assertNotEmpty($events); + } + + public function testBucketsListWithIncludeParameter() + { + $buckets = $this->_client->listBuckets([ + 'include' => '', + ]); + + $firstBucket = reset($buckets); + $this->assertArrayNotHasKey('attributes', $firstBucket); + } + + public function testBucketCreateWithInvalidBackend() + { + try { + $this->_client->createBucket('unknown-backend', 'in', 'desc', 'redshit'); + $this->fail('Exception should be thrown'); + } catch (\Keboola\StorageApi\ClientException $e) { + $this->assertEquals('storage.buckets.validation', $e->getStringCode()); + } + } + + public function testBucketManipulation() + { + $tokenData = $this->_client->verifyToken(); + + $bucketData = [ + 'name' => 'test', + 'displayName' => 'test-display-name', + 'stage' => 'in', + 'description' => 'this is just a test', + ]; + + $testBucketId = $bucketData['stage'] . '.c-' . $bucketData['name']; + + $this->dropBucketIfExists($this->_client, $testBucketId); + + $newBucketId = $this->_client->createBucket( + $bucketData['name'], + $bucketData['stage'], + $bucketData['description'], + null, + $bucketData['displayName'] + ); + + $importFile = __DIR__ . '/../../../_data/languages.csv'; + // create and import data into source table + $sourceTableId = $this->_client->createTable( + $newBucketId, + 'languages', + new CsvFile($importFile) + ); + + try { + $this->_client->dropBucket($newBucketId); + } catch (ClientException $e) { + $this->assertSame('Only empty buckets can be deleted. There are 1 tables in the bucket.', $e->getMessage()); + $this->assertSame('buckets.deleteNotEmpty', $e->getStringCode()); + } + try { + $this->_client->dropBucket($newBucketId, ['async' => true]); + } catch (ClientException $e) { + $this->assertSame('Only empty buckets can be deleted. There are 1 tables in the bucket.', $e->getMessage()); + $this->assertSame('buckets.deleteNotEmpty', $e->getStringCode()); + } + + $this->_client->dropBucket($newBucketId, ['force' => true]); + + $newBucketId = $this->_client->createBucket( + $bucketData['name'], + $bucketData['stage'], + $bucketData['description'], + null, + $bucketData['displayName'] + ); + + $importFile = __DIR__ . '/../../../_data/languages.csv'; + // create and import data into source table + $sourceTableId = $this->_client->createTable( + $newBucketId, + 'languages', + new CsvFile($importFile) + ); + + $this->_client->dropBucket($newBucketId, ['async' => true, 'force' => true]); + + $newBucketId = $this->_client->createBucket( + $bucketData['name'], + $bucketData['stage'], + $bucketData['description'], + null, + $bucketData['displayName'] + ); + + $newBucket = $this->_client->getBucket($newBucketId); + $this->assertEquals('c-' . $bucketData['name'], $newBucket['name'], 'bucket name'); + $this->assertEquals($bucketData['displayName'], $newBucket['displayName'], 'bucket displayName'); + $this->assertEquals($bucketData['stage'], $newBucket['stage'], 'bucket stage'); + $this->assertEquals($bucketData['description'], $newBucket['description'], 'bucket description'); + $this->assertEquals($tokenData['owner']['defaultBackend'], $newBucket['backend'], 'backend'); + + // check if bucket is in list + $buckets = $this->_client->listBuckets(); + $this->assertTrue(in_array($newBucketId, array_map(function ($bucket) { + return $bucket['id']; + }, $buckets))); + + try { + $this->_client->createBucket( + $bucketData['name'] . '-' . time(), + $bucketData['stage'], + $bucketData['description'], + null, + $bucketData['displayName'] + ); + $this->fail('Display name already exist for project'); + } catch (\Keboola\StorageApi\ClientException $e) { + $this->assertEquals('The display name "test-display-name" already exists in project.', $e->getMessage()); + $this->assertEquals('storage.buckets.alreadyExists', $e->getStringCode()); + } + + try { + $this->_client->createBucket( + $bucketData['name'] . '-' . time(), + $bucketData['stage'], + $bucketData['description'], + null, + '$$$$$' + ); + $this->fail('Display name provided is invalid'); + } catch (\Keboola\StorageApi\ClientException $e) { + $this->assertEquals('Invalid data - displayName: Only alphanumeric characters dash and underscores are allowed.', $e->getMessage()); + $this->assertEquals('storage.buckets.validation', $e->getStringCode()); + } + + $this->_client->dropBucket($newBucket['id']); + + $newBucketId = $this->_client->createBucket( + $bucketData['name'], + $bucketData['stage'], + $bucketData['description'] + ); + + $newBucket = $this->_client->getBucket($newBucketId); + $this->assertEquals('c-' . $bucketData['name'], $newBucket['name'], 'bucket name'); + $this->assertEquals($bucketData['name'], $newBucket['displayName'], 'bucket displayName'); + $this->assertEquals($bucketData['stage'], $newBucket['stage'], 'bucket stage'); + $this->assertEquals($bucketData['description'], $newBucket['description'], 'bucket description'); + $this->assertEquals($tokenData['owner']['defaultBackend'], $newBucket['backend'], 'backend'); + + // check if bucket is in list + $buckets = $this->_client->listBuckets(); + $this->assertTrue(in_array($newBucketId, array_map(function ($bucket) { + return $bucket['id']; + }, $buckets))); + } + + public function testBucketCreateWithoutDescription() + { + $bucketId = $this->_client->createBucket('something', self::STAGE_IN); + $bucket = $this->_client->getBucket($bucketId); + $this->assertEmpty($bucket['description']); + $this->_client->dropBucket($bucket['id']); + } + + public function testBucketAttributes() + { + $bucketId = $this->getTestBucketId(); + + $bucket = $this->_client->getBucket($bucketId); + $this->assertEmpty($bucket['attributes'], 'empty attributes'); + + // create + $this->_client->setBucketAttribute($bucketId, 's', 'lala'); + $this->_client->setBucketAttribute($bucketId, 'other', 'hello', true); + $bucket = $this->_client->getBucket($bucketId); + $this->assertArrayEqualsSorted([ + [ + 'name' => 's', + 'value' => 'lala', + 'protected' => false, + ], + [ + 'name' => 'other', + 'value' => 'hello', + 'protected' => true, + ], + ], $bucket['attributes'], 'name', 'attribute set'); + + // update + $this->_client->setBucketAttribute($bucketId, 's', 'papa'); + $bucket = $this->_client->getBucket($bucketId); + $this->assertArrayEqualsSorted($bucket['attributes'], [ + [ + 'name' => 's', + 'value' => 'papa', + 'protected' => false, + ], + [ + 'name' => 'other', + 'value' => 'hello', + 'protected' => true, + ], + ], 'name', 'attribute update'); + + // delete + $this->_client->deleteBucketAttribute($bucketId, 's'); + $bucket = $this->_client->getBucket($bucketId); + $this->assertArrayEqualsSorted($bucket['attributes'], [ + [ + 'name' => 'other', + 'value' => 'hello', + 'protected' => true, + ], + ], 'name', 'attribute delete'); + + $this->_client->deleteBucketAttribute($bucketId, 'other'); + } + + public function testBucketExists() + { + $this->assertTrue($this->_client->bucketExists($this->getTestBucketId())); + $this->assertFalse($this->_client->bucketExists('in.ukulele')); + } + + public function testBucketAttributesReplace() + { + $bucketId = $this->getTestBucketId(); + $this->clearBucketAttributes($bucketId); + $this->_client->setBucketAttribute($bucketId, 'first', 'something'); + + $newAttributes = [ + [ + 'name' => 'new', + 'value' => 'new', + ], + [ + 'name' => 'second', + 'value' => 'second value', + 'protected' => true, + ], + ]; + $this->_client->replaceBucketAttributes($bucketId, $newAttributes); + + $bucket = $this->_client->getBucket($bucketId); + $this->assertCount(count($newAttributes), $bucket['attributes']); + + $this->assertEquals($newAttributes[0]['name'], $bucket['attributes'][0]['name']); + $this->assertEquals($newAttributes[0]['value'], $bucket['attributes'][0]['value']); + $this->assertFalse($bucket['attributes'][0]['protected']); + } + + private function clearBucketAttributes($bucketId) + { + $bucket = $this->_client->getBucket($bucketId); + + foreach ($bucket['attributes'] as $attribute) { + $this->_client->deleteBucketAttribute($bucketId, $attribute['name']); + } + } + + public function testBucketAttributesClear() + { + $bucketId = $this->getTestBucketId(); + $this->clearBucketAttributes($bucketId); + + $this->_client->replaceBucketAttributes($bucketId); + $bucket = $this->_client->getBucket($bucketId); + + $this->assertEmpty($bucket['attributes']); + } + + /** + * @param $attributes + * @dataProvider invalidAttributes + */ + public function testBucketAttributesReplaceValidation($attributes) + { + $bucketId = $this->getTestBucketId(); + $this->clearBucketAttributes($bucketId); + + try { + $this->_client->replaceBucketAttributes($bucketId, $attributes); + $this->fail('Attributes should be invalid'); + } catch (\Keboola\StorageApi\ClientException $e) { + $this->assertEquals('storage.attributes.validation', $e->getStringCode()); + } + } + + public function invalidAttributes() + { + return [ + [ + [ + [ + 'nome' => 'ukulele', + ], + [ + 'name' => 'jehovista', + ], + ], + ], + ]; + } +} diff --git a/tests/Parallel/Backend/Redshift/.gitkeep b/tests/Parallel/Backend/Redshift/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Parallel/Backend/Snowflake/.gitkeep b/tests/Parallel/Backend/Snowflake/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Parallel/Backend/Synapse/.gitkeep b/tests/Parallel/Backend/Synapse/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/tests/StorageApiTestCase.php b/tests/StorageApiTestCase.php index 18a2bd181..c387a3ac6 100644 --- a/tests/StorageApiTestCase.php +++ b/tests/StorageApiTestCase.php @@ -17,7 +17,7 @@ use Keboola\StorageApi\Options\FileUploadOptions; use Keboola\StorageApi\Options\ListFilesOptions; -abstract class StorageApiTestCase extends \PHPUnit_Framework_TestCase +abstract class StorageApiTestCase extends \PHPUnit\Framework\TestCase { const BACKEND_REDSHIFT = 'redshift'; const BACKEND_SNOWFLAKE = 'snowflake'; @@ -88,11 +88,18 @@ public function setUp() )); } + /** + * @return string + */ + public function getBucketNameForTest() + { + return 'API-tests' . md5($this->getName()); + } - protected function _initEmptyTestBuckets($stages = [self::STAGE_OUT, self::STAGE_IN]) + protected function _initEmptyTestBuckets($stages = [self::STAGE_OUT, self::STAGE_IN], $name = 'API-tests') { foreach ($stages as $stage) { - $this->_bucketIds[$stage] = $this->initEmptyBucket('API-tests', $stage); + $this->_bucketIds[$stage] = $this->initEmptyBucket($name, $stage); } } @@ -174,7 +181,7 @@ public function assertArrayEqualsSorted($expected, $actual, $sortKey, $message = }; usort($expected, $comparsion); usort($actual, $comparsion); - return $this->assertEquals($expected, $actual, $message); + $this->assertEquals($expected, $actual, $message); } public function tableExportFiltersData()