-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic write using line protocol and Point structure #5
* tests
- Loading branch information
Showing
5 changed files
with
241 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |