|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backend\Snowflake; |
| 4 | + |
| 5 | +use Keboola\Csv\CsvFile; |
| 6 | +use Keboola\StorageApi\Components; |
| 7 | +use Keboola\StorageApi\Options\Components\Configuration; |
| 8 | +use Keboola\StorageApi\Options\Components\ListComponentsOptions; |
| 9 | +use Keboola\StorageApi\WorkspaceLoginType; |
| 10 | +use Keboola\StorageApi\Workspaces; |
| 11 | +use Keboola\Test\Backend\WorkspaceConnectionTrait; |
| 12 | +use Keboola\Test\Backend\WorkspaceCredentialsAssertTrait; |
| 13 | +use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory; |
| 14 | +use Keboola\Test\Backend\Workspaces\ParallelWorkspacesTestCase; |
| 15 | +use Keboola\Test\Utils\PemKeyCertificateGenerator; |
| 16 | + |
| 17 | +class WorkspacesReaderTest extends ParallelWorkspacesTestCase |
| 18 | +{ |
| 19 | + use WorkspaceConnectionTrait; |
| 20 | + use WorkspaceCredentialsAssertTrait; |
| 21 | + |
| 22 | + |
| 23 | + public function setUp(): void |
| 24 | + { |
| 25 | + parent::setUp(); |
| 26 | + $this->initEmptyTestBucketsForParallelTests(); |
| 27 | + |
| 28 | + $components = new Components($this->_client); |
| 29 | + foreach ($components->listComponents() as $component) { |
| 30 | + foreach ($component['configurations'] as $configuration) { |
| 31 | + $components->deleteConfiguration($component['id'], $configuration['id']); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // erase all deleted configurations |
| 36 | + foreach ($components->listComponents((new ListComponentsOptions())->setIsDeleted(true)) as $component) { |
| 37 | + foreach ($component['configurations'] as $configuration) { |
| 38 | + $components->deleteConfiguration($component['id'], $configuration['id']); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public function testLoadToReaderAccount(): void |
| 44 | + { |
| 45 | + $componentId = 'wr-db'; |
| 46 | + $configurationId = 'main-1'; |
| 47 | + $defaultBranchId = $this->getDefaultBranchId($this); |
| 48 | + $branchClient = $this->getBranchAwareDefaultClient($defaultBranchId); |
| 49 | + // create configuration |
| 50 | + $components = new Components($branchClient); |
| 51 | + $components->addConfiguration((new Configuration()) |
| 52 | + ->setComponentId('wr-db') |
| 53 | + ->setConfigurationId('main-1') |
| 54 | + ->setName('readerWS') |
| 55 | + ->setDescription('some desc')); |
| 56 | + |
| 57 | + $components = new Components($branchClient); |
| 58 | + $workspaces = new Workspaces($branchClient); |
| 59 | + |
| 60 | + $key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null); |
| 61 | + |
| 62 | + $workspace = $components->createConfigurationWorkspace( |
| 63 | + $componentId, |
| 64 | + $configurationId, |
| 65 | + [ |
| 66 | + 'useCase' => 'reader', |
| 67 | + 'backend' => 'snowflake', |
| 68 | + 'loginType' => WorkspaceLoginType::SNOWFLAKE_SERVICE_KEYPAIR, |
| 69 | + 'publicKey' => $key->getPublicKey(), |
| 70 | + ], |
| 71 | + ); |
| 72 | + |
| 73 | + //setup test tables |
| 74 | + $tableId = $this->_client->createTableAsync( |
| 75 | + $this->getTestBucketId(), |
| 76 | + 'languages', |
| 77 | + new CsvFile(__DIR__ . '/../../_data/languages.csv'), |
| 78 | + ); |
| 79 | + |
| 80 | + $workspaces->loadWorkspaceData($workspace['id'], [ |
| 81 | + 'input' => [ |
| 82 | + [ |
| 83 | + 'source' => $tableId, |
| 84 | + 'destination' => 'languages', |
| 85 | + ], |
| 86 | + [ |
| 87 | + 'source' => $tableId, |
| 88 | + 'destination' => 'languages_filtered', |
| 89 | + 'overwrite' => false, |
| 90 | + 'whereColumn' => 'id', |
| 91 | + 'whereValues' => [1], |
| 92 | + 'whereOperator' => 'eq', |
| 93 | + ], |
| 94 | + ], |
| 95 | + ]); |
| 96 | + |
| 97 | + $workspace['connection']['privateKey'] = $key->getPrivateKey(); |
| 98 | + |
| 99 | + // create the connection after LOAD!! because the schema will be created by LOAD |
| 100 | + $db = WorkspaceBackendFactory::createWorkspaceBackend($workspace, true); |
| 101 | + |
| 102 | + $data = $db->fetchAll('languages'); |
| 103 | + $this->assertCount(5, $data); |
| 104 | + |
| 105 | + $data = $db->fetchAll('languages_filtered'); |
| 106 | + $this->assertCount(1, $data); |
| 107 | + } |
| 108 | + |
| 109 | + public function testLoadCloneToReaderAccount(): void |
| 110 | + { |
| 111 | + $componentId = 'wr-db'; |
| 112 | + $configurationId = 'main-1'; |
| 113 | + $defaultBranchId = $this->getDefaultBranchId($this); |
| 114 | + $branchClient = $this->getBranchAwareDefaultClient($defaultBranchId); |
| 115 | + // create configuration |
| 116 | + $components = new Components($branchClient); |
| 117 | + $components->addConfiguration((new Configuration()) |
| 118 | + ->setComponentId('wr-db') |
| 119 | + ->setConfigurationId('main-1') |
| 120 | + ->setName('readerWS_clone') |
| 121 | + ->setDescription('some desc')); |
| 122 | + |
| 123 | + $components = new Components($branchClient); |
| 124 | + $workspaces = new Workspaces($branchClient); |
| 125 | + |
| 126 | + $key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null); |
| 127 | + |
| 128 | + $workspace = $components->createConfigurationWorkspace( |
| 129 | + $componentId, |
| 130 | + $configurationId, |
| 131 | + [ |
| 132 | + 'useCase' => 'reader', |
| 133 | + 'backend' => 'snowflake', |
| 134 | + 'loginType' => WorkspaceLoginType::SNOWFLAKE_SERVICE_KEYPAIR, |
| 135 | + 'publicKey' => $key->getPublicKey(), |
| 136 | + ], |
| 137 | + ); |
| 138 | + |
| 139 | + //setup test tables |
| 140 | + $tableId = $this->_client->createTableAsync( |
| 141 | + $this->getTestBucketId(self::STAGE_IN), |
| 142 | + 'languages', |
| 143 | + new CsvFile(__DIR__ . '/../../_data/languages.csv'), |
| 144 | + ); |
| 145 | + |
| 146 | + $workspaces->cloneIntoWorkspace($workspace['id'], [ |
| 147 | + 'input' => [ |
| 148 | + [ |
| 149 | + 'source' => $tableId, |
| 150 | + 'destination' => 'languages', |
| 151 | + ], |
| 152 | + [ |
| 153 | + 'source' => $tableId, |
| 154 | + 'destination' => 'langs', |
| 155 | + ], |
| 156 | + ], |
| 157 | + ]); |
| 158 | + |
| 159 | + $workspace['connection']['privateKey'] = $key->getPrivateKey(); |
| 160 | + |
| 161 | + // create the connection after LOAD!! because the schema will be created by LOAD |
| 162 | + $db = WorkspaceBackendFactory::createWorkspaceBackend($workspace, true); |
| 163 | + |
| 164 | + $data = $db->fetchAll('languages'); |
| 165 | + $this->assertCount(5, $data); |
| 166 | + |
| 167 | + $data = $db->fetchAll('langs'); |
| 168 | + $this->assertCount(5, $data); |
| 169 | + } |
| 170 | +} |
0 commit comments