-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…or-null-conversion-in-bq CT-1361 default value for null conversion in bq
- Loading branch information
Showing
4 changed files
with
314 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
|
||
namespace Keboola\Test\Backend\Bigquery; | ||
|
||
use Generator; | ||
use Keboola\Csv\CsvFile; | ||
use Keboola\StorageApi\Workspaces; | ||
use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory; | ||
use Keboola\Test\Backend\Workspaces\ParallelWorkspacesTestCase; | ||
use PDO; | ||
|
||
class ImportTreatValuesAsNullTest extends ParallelWorkspacesTestCase | ||
{ | ||
/** | ||
* @dataProvider treatValuesAsNullData | ||
*/ | ||
public function testImportTreatValuesAsNull( | ||
string $importedFile, | ||
array $expectedData, | ||
?array $treatValuesAsNull = null | ||
): void { | ||
$this->allowTestForBackendsOnly([ | ||
self::BACKEND_BIGQUERY, | ||
]); | ||
$tableName = 'testImportTreatValuesAsNull'; | ||
$tableId = $this->createTable($tableName); | ||
|
||
$params = []; | ||
if ($treatValuesAsNull !== null) { | ||
$params = ['treatValuesAsNull' => $treatValuesAsNull,]; | ||
} | ||
$this->_client->writeTableAsync($tableId, new CsvFile($importedFile), $params); | ||
|
||
$workspaces = new Workspaces($this->workspaceSapiClient); | ||
$workspace = $workspaces->createWorkspace([], true); | ||
$backend = WorkspaceBackendFactory::createWorkspaceBackend($workspace); | ||
|
||
$options = [ | ||
'input' => [ | ||
[ | ||
'source' => $tableId, | ||
'destination' => $tableName, | ||
'useView' => true, | ||
], | ||
], | ||
'preserve' => false, | ||
]; | ||
$workspaces->loadWorkspaceData($workspace['id'], $options); | ||
$data = $backend->fetchAll($tableName, PDO::FETCH_NUM, 'Id'); | ||
$data = array_map(function ($row) { | ||
unset($row[4]); // remove timestamp | ||
return $row; | ||
}, $data); | ||
|
||
$this->assertEqualsCanonicalizing($expectedData, $data); | ||
} | ||
|
||
/** | ||
* @return Generator<string, array{importedFile: string, expectedData: array{array{mixed}}, treatValuesAsNull?: array<string>|null}> | ||
*/ | ||
public function treatValuesAsNullData(): Generator | ||
{ | ||
yield 'empty-array' => [ | ||
'importedFile' => __DIR__ . '/../../_data/languages-empty-string.csv', | ||
'expectedData' => [ | ||
0 => [ | ||
0 => 30, | ||
1 => 'armenia', | ||
2 => '', | ||
3 => 'b', | ||
], | ||
1 => [ | ||
0 => 31, | ||
1 => 'belarus', | ||
2 => '', | ||
3 => 'b', | ||
], | ||
2 => [ | ||
0 => 32, | ||
1 => 'malta', | ||
2 => 'a', | ||
3 => 'b', | ||
], | ||
], | ||
'treatValuesAsNull' => [], | ||
]; | ||
yield 'default' => [ | ||
'importedFile' => __DIR__ . '/../../_data/languages-empty-string.csv', | ||
'expectedData' => [ | ||
0 => [ | ||
0 => 30, | ||
1 => 'armenia', | ||
2 => null, | ||
3 => 'b', | ||
], | ||
1 => [ | ||
0 => 31, | ||
1 => 'belarus', | ||
2 => '', | ||
3 => 'b', | ||
], | ||
2 => [ | ||
0 => 32, | ||
1 => 'malta', | ||
2 => 'a', | ||
3 => 'b', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
private function createTable(string $tableName): string | ||
{ | ||
$bucketId = $this->getTestBucketId(); | ||
$data = [ | ||
'name' => $tableName, | ||
'primaryKeysNames' => ['Id'], | ||
'columns' => [ | ||
[ | ||
'name' => 'Id', | ||
'definition' => [ | ||
'type' => 'INT64', | ||
'nullable' => false, | ||
], | ||
], | ||
[ | ||
'name' => 'Name', | ||
'definition' => [ | ||
'type' => 'STRING', | ||
], | ||
], | ||
[ | ||
'name' => 'iso', | ||
'definition' => [ | ||
'type' => 'STRING', | ||
'nullable' => true, | ||
], | ||
], | ||
[ | ||
'name' => 'Something', | ||
'definition' => [ | ||
'type' => 'STRING', | ||
'nullable' => true, | ||
], | ||
], | ||
], | ||
]; | ||
|
||
$runId = $this->_client->generateRunId(); | ||
$this->_client->setRunId($runId); | ||
|
||
return $this->_client->createTableDefinition($bucketId, $data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
tests/Backend/Snowflake/ImportTreatValuesAsNullTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
<?php | ||
|
||
namespace Keboola\Test\Backend\Snowflake; | ||
|
||
use Generator; | ||
use Keboola\Csv\CsvFile; | ||
use Keboola\StorageApi\Workspaces; | ||
use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory; | ||
use Keboola\Test\Backend\Workspaces\ParallelWorkspacesTestCase; | ||
use PDO; | ||
|
||
class ImportTreatValuesAsNullTest extends ParallelWorkspacesTestCase | ||
{ | ||
/** | ||
* @dataProvider treatValuesAsNullData | ||
*/ | ||
public function testImportTreatValuesAsNull( | ||
string $importedFile, | ||
array $expectedData, | ||
?array $treatValuesAsNull = null | ||
): void { | ||
$this->allowTestForBackendsOnly([ | ||
self::BACKEND_SNOWFLAKE, | ||
]); | ||
$tableName = 'testImportTreatValuesAsNull'; | ||
$tableId = $this->createTable($tableName); | ||
|
||
$params = []; | ||
if ($treatValuesAsNull !== null) { | ||
$params = ['treatValuesAsNull' => $treatValuesAsNull,]; | ||
} | ||
$this->_client->writeTableAsync($tableId, new CsvFile($importedFile), $params); | ||
|
||
$workspaces = new Workspaces($this->workspaceSapiClient); | ||
$workspace = $workspaces->createWorkspace([], true); | ||
$backend = WorkspaceBackendFactory::createWorkspaceBackend($workspace); | ||
|
||
$options = [ | ||
'input' => [ | ||
[ | ||
'source' => $tableId, | ||
'destination' => $tableName, | ||
'useView' => true, | ||
], | ||
], | ||
'preserve' => false, | ||
]; | ||
$workspaces->loadWorkspaceData($workspace['id'], $options); | ||
|
||
$data = $backend->fetchAll($tableName); | ||
$data = array_map(function ($row) { | ||
unset($row[4]); // remove timestamp | ||
return $row; | ||
}, $data); | ||
|
||
$this->assertEqualsCanonicalizing($expectedData, $data); | ||
} | ||
|
||
/** | ||
* @return Generator<string, array{importedFile: string, expectedData: array{array{mixed}}, treatValuesAsNull?: array<string>|null}> | ||
*/ | ||
public function treatValuesAsNullData(): Generator | ||
{ | ||
yield 'empty-array' => [ | ||
'importedFile' => __DIR__ . '/../../_data/languages-empty-string.csv', | ||
'expectedData' => [ | ||
0 => [ | ||
0 => '30', | ||
1 => 'armenia', | ||
2 => null, | ||
3 => 'b', | ||
], | ||
1 => [ | ||
0 => '31', | ||
1 => 'belarus', | ||
2 => '', | ||
3 => 'b', | ||
], | ||
2 => [ | ||
0 => '32', | ||
1 => 'malta', | ||
2 => 'a', | ||
3 => 'b', | ||
], | ||
], | ||
'treatValuesAsNull' => [], | ||
]; | ||
yield 'default' => [ | ||
'importedFile' => __DIR__ . '/../../_data/languages-empty-string.csv', | ||
'expectedData' => [ | ||
0 => [ | ||
0 => '30', | ||
1 => 'armenia', | ||
2 => null, | ||
3 => 'b', | ||
], | ||
1 => [ | ||
0 => '31', | ||
1 => 'belarus', | ||
2 => null, | ||
3 => 'b', | ||
], | ||
2 => [ | ||
0 => '32', | ||
1 => 'malta', | ||
2 => 'a', | ||
3 => 'b', | ||
], | ||
], | ||
]; | ||
} | ||
|
||
private function createTable(string $tableName): string | ||
{ | ||
$bucketId = $this->getTestBucketId(self::STAGE_IN); | ||
$data = [ | ||
'name' => $tableName, | ||
'primaryKeysNames' => ['Id'], | ||
'columns' => [ | ||
[ | ||
'name' => 'Id', | ||
'definition' => [ | ||
'type' => 'INT', | ||
'nullable' => false, | ||
], | ||
], | ||
[ | ||
'name' => 'Name', | ||
'definition' => [ | ||
'type' => 'STRING', | ||
], | ||
], | ||
[ | ||
'name' => 'iso', | ||
'definition' => [ | ||
'type' => 'STRING', | ||
'nullable' => true, | ||
], | ||
], | ||
[ | ||
'name' => 'Something', | ||
'definition' => [ | ||
'type' => 'STRING', | ||
'nullable' => true, | ||
], | ||
], | ||
], | ||
]; | ||
|
||
$runId = $this->_client->generateRunId(); | ||
$this->_client->setRunId($runId); | ||
|
||
return $this->_client->createTableDefinition($bucketId, $data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"Id","Name","iso","Something" | ||
"30","armenia",,"b" | ||
"31","belarus","","b" | ||
"32","malta","a","b" |