Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix/small fixes #13

Merged
merged 7 commits into from
Mar 13, 2020
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.1.0 [unreleased]

### Bugs
1. [#13](https://github.com/influxdata/influxdb-client-php/pull/13): Fixed throwing of InvalidArgumentException some option is empty
2. [#13](https://github.com/influxdata/influxdb-client-php/pull/13): FluxCsvParser: fixed throwing FluxQueryException with no reference, default value is 0
3. [#13](https://github.com/influxdata/influxdb-client-php/pull/13): Fixed error when querying empty data, now returns null

## 1.0.0 [2020-03-06]

### Features
Expand Down
15 changes: 13 additions & 2 deletions src/InfluxDB2/DefaultApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,19 @@ public function post($payload, $uriPath, $queryParams, $limit = self::DEFAULT_TI

function check($key, $value)
{
if ($value == null) {
throw new InvalidArgumentException("The '${key}' should be defined as argument or default option: {$this->options}");
if ((!isset($value) || trim($value) === '')) {
$options = implode(', ', array_map(
function ($v, $k) {
if(is_array($v)){
return $k.'[]='.implode('&'.$k.'[]=', $v);
}else{
return $k.'='.$v;
}
},
$this->options,
array_keys($this->options)
));
throw new InvalidArgumentException("The '${key}' should be defined as argument or default option: {$options}");
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/InfluxDB2/FluxCsvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function each()
if ($this->parsingStateError) {
$error = $csv[1];
$referenceValue = $csv[2];
throw new FluxQueryError($error, $referenceValue);
throw new FluxQueryError($error,
!isset($referenceValue) || trim($referenceValue) === '' ? 0 : $referenceValue);
}

$result = $this->parseLine($csv);
Expand Down
42 changes: 33 additions & 9 deletions src/InfluxDB2/QueryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ public function __construct(array $options)
* @param null $dialect csv dialect
* @return string
*/
public function queryRaw(string $query, $org = null, $dialect = null): string
public function queryRaw(string $query, $org = null, $dialect = null): ?string
{
return $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT)->getBody()->getContents();
$result = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT);

if ($result == null)
{
return null;
}

return $result->getBody()->getContents();
}

/**
Expand All @@ -46,8 +53,14 @@ public function queryRaw(string $query, $org = null, $dialect = null): string
*/
public function query($query, $org = null, $dialect = null)
{
$response = $response = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT)->getBody();
$parser = new FluxCsvParser($response);
$response = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT);

if ($response == null)
{
return null;
}

$parser = new FluxCsvParser($response->getBody());

foreach ($parser->parse() as $record);

Expand All @@ -60,26 +73,37 @@ public function query($query, $org = null, $dialect = null)
* @param $dialect
* @return FluxCsvParser generator
*/
public function queryStream($query, $org = null, $dialect = null): FluxCsvParser
public function queryStream($query, $org = null, $dialect = null): ?FluxCsvParser
{
$response = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT)->getBody();
return new FluxCsvParser($response, true);
$response = $this->postQuery($query, $org, $dialect ?: $this->DEFAULT_DIALECT);

if ($response == null)
{
return null;
}

return new FluxCsvParser($response->getBody(), true);
}

private function postQuery($query, $org, $dialect): ResponseInterface
private function postQuery($query, $org, $dialect): ?ResponseInterface
{
$orgParam = $org ?: $this->options["org"];
$this->check("org", $orgParam);

$payload = $this->generatePayload($query, $dialect);
$queryParams = ["org" => $orgParam];

if ($payload == null)
{
return null;
}

return $this->post($payload->__toString(), "/api/v2/query", $queryParams);
}

private function generatePayload($query, $dialect)
{
if ($query == null) {
if ((!isset($query) || trim($query) === '')) {
return null;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/DefaultApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace InfluxDB2Test;

use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;
use InfluxDB2\ApiException;

require_once('BasicTest.php');

Expand All @@ -18,4 +20,34 @@ public function testUserAgent()
$this->assertStringStartsWith('influxdb-client-php/',
strval($request->getHeader("User-Agent")[0]));
}

public function testContentType()
{
$this->mockHandler->append(new Response(204));
$this->writeApi->write('h2o,location=west value=33i 15');

$request = $this->mockHandler->getLastRequest();

$this->assertNotEmpty($request->getHeader("Content-Type"));
}

public function testApiException()
{
$this->mockHandler->append(new Response(400));

$this->expectException(ApiException::class);

$this->writeApi->write('h2o,location=west value=33i 15');
}

public function testInvalidArgument()
{
$this->mockHandler->append(new Response(204));

$this->writeApi->options["org"] = '';

$this->expectException(InvalidArgumentException::class);

$this->writeApi->write('h2o,location=west value=33i 15');
}
}
175 changes: 173 additions & 2 deletions tests/FluxTableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace InfluxDB2Test;

use InfluxDB2\FluxCsvParser;
use InfluxDB2\FluxCsvParserException;
use InfluxDB2\FluxQueryError;
use InfluxDB2\FluxRecord;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -88,7 +90,6 @@ public function testMappingBoolean()
*/
public function testMappingUnsignedLong()
{

$data = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,' .
"dateTime:RFC3339,long,string,string,string,unsignedLong\n" .
"#group,false,false,false,false,false,false,false,false,false,true\n" .
Expand All @@ -108,6 +109,177 @@ public function testMappingUnsignedLong()
$this->assertNull($records[1]->values['value']);
}

public function testMappingDouble()
{
$data = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,' .
"dateTime:RFC3339,long,string,string,string,double\n" .
"#group,false,false,false,false,false,false,false,false,false,true\n" .
"#default,_result,,,,,,,,,\n" .
",result,table,_start,_stop,_time,_value,_field,_measurement,host,value\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,12.25\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,\n";

$fluxCsvParser = new FluxCsvParser($data);
$tables = $fluxCsvParser->parse()->tables;

$records = $tables[0]->records;

$this->assertEquals(12.25, $records[0]->values['value']);
$this->assertNull($records[1]->values['value']);
}

public function testMappingBase64Binary()
{
$binaryData = 'test value';
$encodedData = base64_encode($binaryData);

$data = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,' .
"dateTime:RFC3339,long,string,string,string,base64Binary\n" .
"#group,false,false,false,false,false,false,false,false,false,true\n" .
"#default,_result,,,,,,,,,\n" .
",result,table,_start,_stop,_time,_value,_field,_measurement,host,value\n" .
',,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,' . $encodedData . "\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,\n";

$fluxCsvParser = new FluxCsvParser($data);
$tables = $fluxCsvParser->parse()->tables;

$records = $tables[0]->records;

$this->assertEquals($binaryData, $records[0]->values['value']);
$this->assertNull($records[1]->values['value']);
}

public function testMappingDuration()
{
$data = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339' .
",dateTime:RFC3339,long,string,string,string,duration\n" .
"#group,false,false,false,false,false,false,false,false,false,true\n" .
"#default,_result,,,,,,,,,\n" .
",result,table,_start,_stop,_time,_value,_field,_measurement,host,value\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,125\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,\n";

$fluxCsvParser = new FluxCsvParser($data);
$tables = $fluxCsvParser->parse()->tables;

$records = $tables[0]->records;

$this->assertEquals(125, $records[0]->values['value']);
$this->assertNull($records[1]->values['value']);
}

public function testGroupKey()
{
$data = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,' .
"dateTime:RFC3339,long,string,string,string,duration\n" .
"#group,false,false,false,false,true,false,false,false,false,true\n" .
"#default,_result,,,,,,,,,\n" .
",result,table,_start,_stop,_time,_value,_field,_measurement,host,value\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,125\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,\n";

$fluxCsvParser = new FluxCsvParser($data);
$tables = $fluxCsvParser->parse()->tables;

$this->assertEquals(10, count($tables[0]->columns));
$this->assertEquals(2, count($tables[0]->getGroupKey()));
}

public function testUnknownTypeAsString()
{
$data = '#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,' .
"dateTime:RFC3339,long,string,string,string,unknown\n" .
"#group,false,false,false,false,false,false,false,false,false,true\n" .
"#default,_result,,,,,,,,,\n" .
",result,table,_start,_stop,_time,_value,_field,_measurement,host,value\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,12.25\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,\n";

$fluxCsvParser = new FluxCsvParser($data);
$tables = $fluxCsvParser->parse()->tables;

$records = $tables[0]->records;

$this->assertEquals('12.25', $records[0]->values['value']);
$this->assertNull($records[1]->values['value']);
}

public function testError()
{
$data = "#datatype,string,string\n" .
"#group,true,true\n" .
"#default,,\n" .
",error,reference\n" .
',failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time,897';

$fluxCsvParser = new FluxCsvParser($data);

try {
$fluxCsvParser->parse();
$this->fail();
}
catch (FluxQueryError $e)
{
$this->assertEquals('failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time',
$e->getMessage());
$this->assertEquals(897, $e->getCode());
}
catch (\Exception $e)
{
$this->fail();
}
}

public function testErrorWithoutReference()
{
$data = "#datatype,string,string\n" .
"#group,true,true\n" .
"#default,,\n" .
",error,reference\n" .
',failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time,';

$fluxCsvParser = new FluxCsvParser($data);

try {
$fluxCsvParser->parse();
$this->fail();
}
catch (FluxQueryError $e)
{
$this->assertEquals('failed to create physical plan: invalid time bounds from procedure from: bounds contain zero time',
$e->getMessage());
$this->assertEquals(0, $e->getCode());
}
catch (\Exception $e)
{
$this->fail();
}
}

public function testWithoutTableReference()
{
$data = ",result,table,_start,_stop,_time,_value,_field,_measurement,host,value\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,12.25\n" .
",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,\n";

$fluxCsvParser = new FluxCsvParser($data);

try {
$fluxCsvParser->parse();
$this->fail();
}
catch (FluxCsvParserException $e)
{
$this->assertEquals('Unable to parse CSV response. FluxTable definition was not found.',
$e->getMessage());
}
catch (\Exception $e)
{
$this->fail();
}
}

private function assertColumns(array $columnHeaders, array $values)
{
$i = 0;
Expand Down Expand Up @@ -159,7 +331,6 @@ private function assertMultipleRecords(array $tables)

private function assertRecord(FluxRecord $fluxRecord, array $values, $size = 0, $value = null)
{

foreach ($values as $key => $val) {
$this->assertEquals($values[$key], $fluxRecord->values[$key]);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/QueryApiStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public function testQueryStreamBreak()
$this->assertTrue($parser->closed);
}

public function testQueryEmptyData()
{
$result = $this->queryApi->queryStream(null);

$this->assertNull($result);
}

private function write($values, $measurement)
{
for ($ii = 0; $ii < $values; $ii++) {
Expand Down
Loading