Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add check and test for non-scalar values in primary key #8

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Keboola/CsvMap/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ protected function parseRow($row, array $userData)
}

$primaryKeyValue = $this->getPrimaryKeyValues($row, $userData);
$this->checkPrimaryKeyValues($primaryKeyValue);

if (empty($settings['parentKey']['disable'])) {
if (empty($this->getPrimaryKey())) {
Expand Down Expand Up @@ -161,6 +162,17 @@ protected function parseRow($row, array $userData)
return $result;
}

private function checkPrimaryKeyValues(array $values)
{
foreach ($values as $value) {
if (!is_scalar($value) && !is_null($value)) {
throw new BadConfigException(
'Only scalar values are allowed in primary key. Primary key: ' . json_encode($values)
);
}
}
}

public function getPrimaryKey()
{
$primaryKey = [];
Expand Down
58 changes: 58 additions & 0 deletions tests/Keboola/CsvMap/MapperNotAllowedPrimaryKeyValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Keboola\CsvMap;

class MapperNotAllowedPrimaryKeyValueTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Keboola\CsvMap\Exception\BadConfigException
* @expectedExceptionMessage Only scalar values are allowed in primary key.
* Primary key: [{"$oid":"5716054bee6e764c94fa85a6"}]
*/
public function testNotAllowedPrimaryKeyValue()
{
$config = [
'_id' => [
'type' => 'column',
'mapping' => [
'destination' => 'id',
'primaryKey' => true,
]
],
'coord' => [
'type' => 'table',
'destination' => 'coord',
'tableMapping' => [
'a' => 'a',
]
]
];

$data = $this->getSampleData();

$parser = new Mapper($config);
$parser->parse($data);
}

protected function getSampleData()
{
$json = <<<JSON
[
{
"_id": {
"\$oid": "5716054bee6e764c94fa85a6"
},
"coord": [
{
"a": 1
},
{
"a": 2
}
]
}
]
JSON;
return json_decode($json);
}
}