Skip to content

Commit

Permalink
Merge pull request #340 from keboola/erik-PST-2101
Browse files Browse the repository at this point in the history
[output-mapping] phpunit - table structure modifier tests added to na…
  • Loading branch information
ErikZigo authored Dec 13, 2024
2 parents 4ae8c43 + bb9cc4a commit a35fdbf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/Storage/TableStructureModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Keboola\OutputMapping\Storage;

use Keboola\Datatype\Definition\BaseType;
use Keboola\OutputMapping\Exception\InvalidOutputException;
use Keboola\OutputMapping\Exception\PrimaryKeyNotChangedException;
use Keboola\OutputMapping\Mapping\MappingColumnMetadata;
use Keboola\OutputMapping\Mapping\MappingFromProcessedConfiguration;
use Keboola\OutputMapping\Writer\Helper\PrimaryKeyHelper;
use Keboola\OutputMapping\Writer\Table\MappingDestination;
use Keboola\OutputMapping\Writer\Table\TableDefinition\TableDefinitionColumnFactory;
use Keboola\StorageApi\ClientException;

class TableStructureModifier extends AbstractTableStructureModifier
{
Expand Down Expand Up @@ -99,14 +101,18 @@ private function addMissingColumns(
];
}

foreach ($missingColumnsData as $missingColumnData) {
[$columnName, $columnDefinition, $columnBasetype] = $missingColumnData;
$this->client->addTableColumn(
$currentTableInfo->getId(),
$columnName,
$columnDefinition,
$columnBasetype,
);
try {
foreach ($missingColumnsData as $missingColumnData) {
[$columnName, $columnDefinition, $columnBasetype] = $missingColumnData;
$this->client->addTableColumn(
$currentTableInfo->getId(),
$columnName,
$columnDefinition,
$columnBasetype,
);
}
} catch (ClientException $e) {
throw new InvalidOutputException($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down
53 changes: 51 additions & 2 deletions tests/Storage/TableStructureModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Generator;
use Keboola\Csv\CsvFile;
use Keboola\OutputMapping\Exception\InvalidOutputException;
use Keboola\OutputMapping\Mapping\MappingColumnMetadata;
use Keboola\OutputMapping\Mapping\MappingFromProcessedConfiguration;
use Keboola\OutputMapping\Storage\BucketInfo;
Expand All @@ -14,7 +15,6 @@
use Keboola\OutputMapping\Tests\AbstractTestCase;
use Keboola\OutputMapping\Tests\Needs\NeedsTestTables;
use Keboola\OutputMapping\Writer\Table\MappingDestination;
use Keboola\StorageApi\ClientException;

class TableStructureModifierTest extends AbstractTestCase
{
Expand Down Expand Up @@ -195,7 +195,7 @@ public function testErrorUpdateTableStructureEmptyColumnName(): void
$source->method('getMetadata')->willReturn([]);
$source->method('getColumnMetadata')->willReturn([]);

$this->expectException(ClientException::class);
$this->expectException(InvalidOutputException::class);
$this->expectExceptionMessage('Invalid parameters - name: This value should not be blank.');
$this->modifier->updateTableStructure(
$this->destinationBucket,
Expand Down Expand Up @@ -373,6 +373,55 @@ public function testUpdateTableStructureAddColumnsFromMetadataWithNativeTypes(
);
}

#[NeedsTestTables(typedTable: true)]
public function testUpdateTableStructureAddColumnsFailureThrowsInvalidOutputException(): void
{
$source = $this->createMock(MappingFromProcessedConfiguration::class);
$source->method('getColumns')->willReturn(array_merge(
$this->destinationTableInfo['columns'],
['newColumn'],
));
$source->method('getMetadata')->willReturn([
[
'key' => 'KBC.datatype.backend',
'value' => 'snowflake',
],
]);
$source->method('getColumnMetadata')->willReturn([
new MappingColumnMetadata(
'newColumn',
[
[
'key' => 'KBC.datatype.type',
'value' => 'VARCHAR',
],
[
'key' => 'KBC.datatype.basetype',
'value' => 'STRING',
],
[
'key' => 'KBC.datatype.nullable',
'value' => false,
],
],
),
]);

$this->expectException(InvalidOutputException::class);
$this->expectExceptionMessage(
'Non-nullable column "newColumn" cannot be added to non-empty table '
. '"test1" unless it has a non-null default value.',
);

$this->modifier->updateTableStructure(
$this->destinationBucket,
new TableInfo($this->destinationTableInfo),
$source,
$this->destination,
false,
);
}

public function newColumnNamesProvider(): Generator
{
yield ['new_column'];
Expand Down

0 comments on commit a35fdbf

Please sign in to comment.