Skip to content

Commit

Permalink
Merge pull request #1372 from keboola/vb-CT-1670-validate-column-name…
Browse files Browse the repository at this point in the history
…s-endpoint

CT-1670 - test validate column names endpoint
  • Loading branch information
vojtabiberle committed Jul 29, 2024
2 parents d4b5f42 + 3793dd7 commit 10adaea
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
35 changes: 35 additions & 0 deletions apiary.apib
Original file line number Diff line number Diff line change
Expand Up @@ -9007,6 +9007,41 @@ Webalize strings. Do not validate if you can use them in database column names.
]
}
# Group Validate
### Validate column names [POST /v2/storage/validate/column-names]
Validate strings if they can be used as column names. Column names can be used for example:
- [creating a table](https://keboola.docs.apiary.io/#reference/tables/create-table-definition/create-new-table-definition)
- [adding a column](https://keboola.docs.apiary.io/#reference/tables/create-table-column/add-column-to-table)
+ Attributes
+ columnNames (required, array[string])
+ Request (application/json)
+ Body
{
"columnNames": [
"First$$Column-Name",
"Second Column-Name"
]
}
+ Response 204
If all columns are valid
+ Response 422
+ Body
{
[
""First$$Column-Name" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.",
""Second Column-Name" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed."
]
}
# Group Development Branches
**EXPERIMENTAL**
Expand Down
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5223,3 +5223,8 @@ parameters:
message: "#^Method Keboola\\\\StorageApi\\\\Client\\:\\:webalizeColumnNames\\(\\) has no return type specified\\.$#"
count: 1
path: src/Keboola/StorageApi/Client.php

-
message: "#^Method Keboola\\\\StorageApi\\\\Client\\:\\:validateColumnNames\\(\\) has no return type specified\\.$#"
count: 1
path: src/Keboola/StorageApi/Client.php
8 changes: 8 additions & 0 deletions src/Keboola/StorageApi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ public function webalizeColumnNames(array $columnNames)
);
}

public function validateColumnNames(array $columnNames)
{
return $this->apiPostJson(
'validate/column-names',
['columnNames' => $columnNames],
);
}

/**
* Get UserAgent name
*
Expand Down
95 changes: 95 additions & 0 deletions tests/Common/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Keboola\StorageApi\ClientException;
use Keboola\StorageApi\Options\IndexOptions;
use Keboola\Test\StorageApiTestCase;
use const PHP_EOL;

class IndexTest extends StorageApiTestCase
{
Expand Down Expand Up @@ -140,6 +141,100 @@ public function validColumnNameData(): Generator
];
}

/**
* @dataProvider validColumnNameDataForValidation
*/
public function testSuccessfullyValidateColumnNames(array $webalizedInput): void
{
$response = $this->_client->validateColumnNames($webalizedInput);
$this->assertEmpty($response, 'When empty, everything is validated.');
}

public function validColumnNameDataForValidation(): Generator
{
yield 'all' => [
[
'currency_EUR',
'eEsScCrRzZyYaAiIeE',
'lorem_EUR_EUR_dD_lL_ss_IPsum',
'muj_Bucket',
'account_EUR',
'some_name',
'MujBucketicek',
'loremIpsumDolorSitAmetWhateverNextLoremIpsumDolorSitAmetloremIps',
],
];
}

/**
* @dataProvider invalidColumnNameDataForValidation
*/
public function testInvalidColumnNames(array $columnNames, array $validationErrorMessages): void
{
try {
$this->_client->validateColumnNames($columnNames);
$this->fail('Invalid columns should fail');
} catch (ClientException $exception) {
$expectedMessages = [];
$expectedErrorMessages = [];
foreach ($validationErrorMessages as $key => $errorMessage) {
$keyName = sprintf('columnNames[%d]', $key);
$expectedMessages[] = sprintf(' - %s: "%s"', $keyName, $errorMessage);
$expectedErrorMessages[] = [
'key' => $keyName,
'message' => $errorMessage,
];
}
$expectedMessage = 'Invalid request:' . PHP_EOL . implode(PHP_EOL, $expectedMessages);
$this->assertEquals($expectedMessage, $exception->getMessage());
$this->assertEquals($expectedErrorMessages, $exception->getContextParams()['errors']);
}
}

public function invalidColumnNameDataForValidation(): Generator
{
yield 'all' => [
[
'oid',
'tableoid',
'xmin',
'cmin',
'xmax',
'cmax',
'ctid',
'currency €',
'ěĚšŠčČřŘžŽýÝáÁíÍéÉ',
'lorem &@€\\#˝´˙`˛°˘^ˇ~€||\đĐ[]łŁ}{{@@&##<>*$ߤ×÷¸¨ IPsum',
'muj Bucket',
'account € & $',
'$$ some name $$',
'_MůjBucketíček',
'loremIpsumDolorSitAmetWhateverNextLoremIpsumDolorSitAmetloremIpsumDolorSitAmetWhateverNextLoremIpsumDolorSitAmet',
base64_decode('AQIDBAUGBwgODxAREhMUFRYXGBkaGxwdHh9/'),
'----$$$$-----',
],
[
'"oid" is a system column used by the database for internal purposes.',
'"tableoid" is a system column used by the database for internal purposes.',
'"xmin" is a system column used by the database for internal purposes.',
'"cmin" is a system column used by the database for internal purposes.',
'"xmax" is a system column used by the database for internal purposes.',
'"cmax" is a system column used by the database for internal purposes.',
'"ctid" is a system column used by the database for internal purposes.',
'"currency €" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'"ěĚšŠčČřŘžŽýÝáÁíÍéÉ" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'"lorem &@€\#˝´˙`˛°˘^ˇ~€||\đĐ[]łŁ}{{@@&##<>*$ߤ×÷¸¨ IPsum" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'"muj Bucket" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'"account € & $" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'"$$ some name $$" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'"_MůjBucketíček" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'Column name cannot be longer than 64 characters.',
'"'.base64_decode('AQIDBAUGBwgODxAREhMUFRYXGBkaGxwdHh9/').'" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
'"----$$$$-----" contains not allowed characters. Only alphanumeric characters dash and underscores are allowed.',
],
];
}

/**
* @dataProvider invalidColumnNameData
*/
Expand Down

0 comments on commit 10adaea

Please sign in to comment.