Skip to content

Commit

Permalink
Basic write using line protocol and Point structure #5
Browse files Browse the repository at this point in the history
* tests
  • Loading branch information
rolincova committed Mar 2, 2020
1 parent 5aba92f commit bf9592b
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/InfluxDB2/DefaultApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class DefaultApi
{
const DEFAULT_TIMEOUT = 10;
public $options;
/** @var Client */
public $http;

/**
* DefaultApi constructor.
Expand All @@ -19,6 +21,11 @@ class DefaultApi
public function __construct(array $options)
{
$this->options = $options;

$this->http = new Client([
'base_uri' => $this->options["url"],
'timeout' => self::DEFAULT_TIMEOUT
]);
}

/**
Expand All @@ -29,11 +36,6 @@ public function __construct(array $options)
*/
public function post($payload, $uriPath, $queryParams, $limit = self::DEFAULT_TIMEOUT): ResponseInterface
{
$http = new Client([
'base_uri' => $this->options["url"],
'timeout' => self::DEFAULT_TIMEOUT
]);

try {
$options = [
'headers' => [
Expand All @@ -49,7 +51,7 @@ public function post($payload, $uriPath, $queryParams, $limit = self::DEFAULT_TI
}

//execute post call
$response = $http->post($uriPath, $options);
$response = $this->http->post($uriPath, $options);

$statusCode = $response->getStatusCode();

Expand Down
10 changes: 9 additions & 1 deletion src/InfluxDB2/WriteApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,15 @@ private function generatePayload($data, string $precision = null, string $bucket

foreach ($data as $item)
{
$payload .= $this->generatePayload($item, $precision, $bucket, $org) . "\n";
if (isset($item)) {
$payload .= $this->generatePayload($item, $precision, $bucket, $org) . "\n";
}
}

// remove last new line
if (isset($payload) && trim($payload) !== '')
{
$payload = rtrim($payload, "\n");
}

return $payload;
Expand Down
59 changes: 59 additions & 0 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace InfluxDB2Test;


use GuzzleHttp\Handler\MockHandler;
use InfluxDB2\Client;
use InfluxDB2\Model\WritePrecision;
use InfluxDB2\QueryApi;
use InfluxDB2\WriteApi;
use PHPUnit\Framework\TestCase;

/**
* Class BasicTest
* @package InfluxDB2Test
*/
class BasicTest extends TestCase
{
/** @var Client */
protected $client;
/** @var WriteApi */
protected $writeApi;
/** @var QueryApi */
protected $queryApi;
/** @var MockHandler */
protected $mockHandler;

/**
* @before
*/
public function setUp()
{
$this->client = new Client([
"url" => "http://localhost:9999",
"token" => "my-token",
"bucket" => "my-bucket",
"precision" => WritePrecision::NS,
"org" => "my-org"
]);

$this->writeApi = $this->client->createWriteApi();
$this->queryApi = $this->client->createQueryApi();

$this->mockHandler = new MockHandler();

$this->writeApi->http = new \GuzzleHttp\Client([
'handler' => $this->mockHandler,
]);

$this->queryApi->http = new \GuzzleHttp\Client([
'handler' => $this->mockHandler,
]);
}

public function tearDown()
{
$this->client->close();
}
}
30 changes: 30 additions & 0 deletions tests/QueryApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace InfluxDB2Test;


use GuzzleHttp\Psr7\Response;

require_once('BasicTest.php');

class QueryApiTest extends BasicTest
{
private const SUCCESS_DATA =
"#datatype,string,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,long,string,string,string,string\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,region\n"
. ",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,10,free,mem,A,west\n"
. ",,0,1970-01-01T00:00:10Z,1970-01-01T00:00:20Z,1970-01-01T00:00:10Z,20,free,mem,B,west\n"
. ",,0,1970-01-01T00:00:20Z,1970-01-01T00:00:30Z,1970-01-01T00:00:20Z,11,free,mem,A,west\n"
. ",,0,1970-01-01T00:00:20Z,1970-01-01T00:00:30Z,1970-01-01T00:00:20Z,22,free,mem,B,west";

public function testQueryRaw()
{
$this->mockHandler->append(new Response(204, [], QueryApiTest::SUCCESS_DATA));

$result = $this->queryApi->queryRaw(
'from(bucket:"my-bucket") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()');

$this->assertEquals(QueryApiTest::SUCCESS_DATA, $result);
}
}
135 changes: 135 additions & 0 deletions tests/WriteApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace InfluxDB2Test;

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use InfluxDB2\ApiException;
use InfluxDB2\Point;

/**
* Class WriteApiTest
* @package InfluxDB2Test
*/
require_once('BasicTest.php');

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

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

$this->assertEquals('/api/v2/write?org=my-org&bucket=my-bucket&precision=ns',
strval($request->getUri()));
$this->assertEquals('h2o,location=west value=33i 15', $request->getBody());
}

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

$point = Point::measurement('h2o')
->addTag('location', 'europe')
->addField('level', 2);

$this->writeApi->write($point);

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

$this->assertEquals('/api/v2/write?org=my-org&bucket=my-bucket&precision=ns',
strval($request->getUri()));
$this->assertEquals('h2o,location=europe level=2i', $request->getBody());
}

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

$array = array('name' => 'h2o',
'tags' => array('host' => 'aws', 'region' => 'us'),
'fields' => array('level' => 5, 'saturation' => '99%'),
'time' => 123);

$this->writeApi->write($array);

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

$this->assertEquals('/api/v2/write?org=my-org&bucket=my-bucket&precision=ns',
strval($request->getUri()));
$this->assertEquals('h2o,host=aws,region=us level=5i,saturation="99%" 123', $request->getBody());
}

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

$point = Point::measurement('h2o')
->addTag('location', 'europe')
->addField('level', 2);

$array = array('name' => 'h2o',
'tags' => array('host' => 'aws', 'region' => 'us'),
'fields' => array('level' => 5, 'saturation' => '99%'),
'time' => 123);

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

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

$expected = "h2o,location=west value=33i 15\n"
. "h2o,location=europe level=2i\n"
. "h2o,host=aws,region=us level=5i,saturation=\"99%\" 123";

$this->assertEquals('/api/v2/write?org=my-org&bucket=my-bucket&precision=ns',
strval($request->getUri()));
$this->assertEquals($expected, strval($request->getBody()));
}

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

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

$this->assertEquals('/api/v2/write?org=my-org&bucket=my-bucket&precision=ns',
strval($request->getUri()));
$this->assertEquals('Token my-token', implode(' ', $request->getHeaders()['Authorization']));
}

public function testWithoutData()
{
$this->mockHandler->append(new Response(204));
$this->writeApi->write('');

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

$this->assertNull($request);
}

public function testInfluxException()
{
$errorBody = '{"code":"invalid","message":"unable to parse \'h2o_feet, location=coyote_creek water_level=1.0 1\': missing tag key"}';

$this->mockHandler->append(new Response(400, array('X-Platform-Error-Code' => 'invalid'), $errorBody));

try {
$this->writeApi->write('h2o,location=west value=33i 15');
$this->fail();
}
catch (ApiException $e)
{
$this->assertEquals(400, $e->getCode());
$this->assertEquals('invalid', implode($e->getResponseHeaders()['X-Platform-Error-Code']));
$this->assertEquals($errorBody, strval($e->getResponseBody()));
}
catch (\Exception $e)
{
$this->fail();
}
}
}

0 comments on commit bf9592b

Please sign in to comment.