Commit 2361372d authored by Volker Krause's avatar Volker Krause
Browse files

Generate new sample ids when importing

Allows merging of multiple data sets, and matches what we actually get
from data export.
parent 929e32a4
......@@ -122,14 +122,14 @@ class Sample
throw new RESTException('Invalid sample data format.', 400);
foreach ($jsonArray as $sampleObj) {
if (!property_exists($sampleObj, 'id') || !property_exists($sampleObj, 'timestamp'))
if (property_exists($sampleObj, 'id') || !property_exists($sampleObj, 'timestamp'))
throw new RESTException('Invalid sample data.', 400);
self::insertScalar($db, $sampleObj, $product);
$sampleId = self::insertScalar($db, $sampleObj, $product);
foreach ($product->schema as $entry) {
if ($entry->isScalar())
continue;
self::insertNonScalar($db, $sampleObj, $entry, $sampleObj->id);
self::insertNonScalar($db, $sampleObj, $entry, $sampleId);
}
}
}
......@@ -143,11 +143,6 @@ class Sample
$binds = array();
$values = array();
if (property_exists($jsonObj, 'id')) {
array_push($columns, 'id');
array_push($binds, ':id');
array_push($values, intval($jsonObj->id));
}
if (property_exists($jsonObj, 'timestamp')) {
array_push($columns, 'timestamp');
array_push($binds, ':timestamp');
......
......@@ -141,20 +141,20 @@ class SurveyTest extends PHPUnit_Extensions_Database_TestCase
$p->insert(self::$db);
$input = '[{
"id": 42,
"timestamp": "2016-12-18 12:42:35",
"entry1": { "element11": "firstString" },
"entry2": [ { "element21": 12 } ]
}, {
"id": 43,
"timestamp": "2016-12-19 15:12:10",
"entry1": { "element12": true },
"entry2": [ { "element22": 1.3 } ]
}]';
Sample::import(self::$db, $input, $p);
$output = Sample::dataAsJson(self::$db, $p);
$this->assertJsonStringEqualsJsonString($input, $output);
$jsonOutput = json_decode(Sample::dataAsJson(self::$db, $p), true);
unset($jsonOutput[0]['id']);
unset($jsonOutput[1]['id']);
$this->assertJsonStringEqualsJsonString($input, json_encode($jsonOutput));
}
public function testInvalidImport_data()
......@@ -163,8 +163,8 @@ class SurveyTest extends PHPUnit_Extensions_Database_TestCase
'nothing' => [ '' ],
'empty' => [ '{}' ],
'object' => [ '{ "id": 42, "timestamp": "2016-12-18 12:42:35" }' ],
'missing id' => [ '{ "timestamp": "2016-12-18 12:42:35" }' ],
'missing timestamp' => [ '{ "id": 42 }' ]
'missing timestamp' => [ '[{ "id": 42 }]' ],
'id present' => [ '[{ "id": 42, "timestamp": "2016-12-18 12:42:35" }]' ]
];
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment