Files
librenms-librenms/lib/influxdb-php/tests/unit/ResultSetTest.php
T

146 lines
3.5 KiB
PHP
Raw Normal View History

2015-06-17 15:35:42 +02:00
<?php
2015-06-23 09:19:24 +02:00
namespace InfluxDB\Test;
2015-06-17 15:35:42 +02:00
2015-06-23 09:19:24 +02:00
use InfluxDB\ResultSet;
2015-06-17 16:03:21 +02:00
2015-06-17 15:35:42 +02:00
class ResultSetTest extends \PHPUnit_Framework_TestCase
{
2015-06-17 16:03:21 +02:00
/** @var ResultSet $resultSet*/
protected $resultSet;
public function setUp()
2015-06-17 15:35:42 +02:00
{
$resultJsonExample = file_get_contents(dirname(__FILE__) . '/json/result.example.json');
2015-06-17 16:03:21 +02:00
$this->resultSet = new ResultSet($resultJsonExample);
}
2015-06-18 10:11:19 +02:00
/**
* @expectedException \InvalidArgumentException
*/
public function testThrowsExceptionIfJSONisNotValid()
{
$invalidJSON = 'foo';
new ResultSet($invalidJSON);
}
2015-06-18 14:21:30 +02:00
/**
* Throws Exception if something went wrong with influxDB
2015-06-23 09:19:24 +02:00
* @expectedException \InfluxDB\Exception
2015-06-18 14:21:30 +02:00
*/
public function testThrowsInfluxDBException()
{
$errorResult = <<<EOD
{
"series": [],
"error": "Big error, many problems."
}
EOD;
new ResultSet($errorResult);
}
/**
* Throws Exception if something went wrong with influxDB after processing the query
2015-06-23 09:19:24 +02:00
* @expectedException \InfluxDB\Exception
*/
public function testThrowsInfluxDBExceptionIfAnyErrorInSeries()
{
$errorResult = <<<EOD
{
"results": [
{
"series": [],
"error": "There was an error after querying"
}
]
}
EOD;
new ResultSet($errorResult);
}
/**
* We can get points from measurement
*/
public function testGetPointsFromNameWithoudTags()
{
$resultJsonExample = file_get_contents(dirname(__FILE__) . '/json/result-no-tags.example.json');
$this->resultSet = new ResultSet($resultJsonExample);
$measurementName = 'cpu_load_short';
$expectedNumberOfPoints = 2;
$points = $this->resultSet->getPoints($measurementName);
$this->assertTrue(is_array($points));
$this->assertCount($expectedNumberOfPoints, $points);
}
2015-06-18 14:59:00 +02:00
/**
* We can get points from measurement
*/
public function testGetPoints()
{
$expectedNumberOfPoints = 3;
$points = $this->resultSet->getPoints();
$this->assertTrue(
is_array($points)
);
$this->assertCount($expectedNumberOfPoints, $points);
}
public function testGetSeries()
{
$this->assertEquals(['time', 'value'], $this->resultSet->getColumns());
}
2015-06-18 10:11:19 +02:00
/**
2015-06-18 13:40:16 +02:00
* We can get points from measurement
2015-06-18 10:11:19 +02:00
*/
2015-06-17 16:03:21 +02:00
public function testGetPointsFromMeasurementName()
{
$measurementName = 'cpu_load_short';
2015-06-18 10:11:19 +02:00
$expectedNumberOfPoints = 2;
$expectedValueFromFirstPoint = 0.64;
2015-06-17 16:03:21 +02:00
$points = $this->resultSet->getPoints($measurementName);
2015-06-17 15:35:42 +02:00
$this->assertTrue(
2015-06-17 16:03:21 +02:00
is_array($points)
2015-06-17 15:35:42 +02:00
);
2015-06-18 10:11:19 +02:00
$this->assertCount($expectedNumberOfPoints, $points);
$somePoint = array_shift($points);
$this->assertEquals($expectedValueFromFirstPoint, $somePoint['value']);
}
public function testGetPointsFromTags()
{
$tags = array("host" => "server01");
2015-06-18 13:40:16 +02:00
$expectedNumberOfPoints = 2;
2015-06-18 10:11:19 +02:00
$points = $this->resultSet->getPoints('', $tags);
2015-06-18 13:40:16 +02:00
$this->assertTrue(is_array($points));
$this->assertCount($expectedNumberOfPoints, $points);
}
public function testGetPointsFromNameAndTags()
{
$tags = array("host" => "server01");
$expectedNumberOfPoints = 2;
$points = $this->resultSet->getPoints('', $tags);
$this->assertTrue(is_array($points));
$this->assertCount($expectedNumberOfPoints, $points);
2015-06-17 15:35:42 +02:00
}
}