Files
librenms-librenms/tests/unit/DatabaseTest.php

124 lines
3.0 KiB
PHP
Raw Normal View History

2015-06-18 16:17:57 +02:00
<?php
2015-06-23 09:19:24 +02:00
namespace InfluxDB\Test;
2015-06-18 16:17:57 +02:00
2015-06-23 09:19:24 +02:00
use InfluxDB\Client;
use InfluxDB\Database;
use InfluxDB\Point;
use InfluxDB\ResultSet;
use PHPUnit_Framework_MockObject_MockObject;
2015-06-18 16:17:57 +02:00
class DatabaseTest extends \PHPUnit_Framework_TestCase
{
/** @var Database $db */
protected $db = null;
/** @var Client|PHPUnit_Framework_MockObject_MockObject $client */
2015-06-18 16:17:57 +02:00
protected $mockClient;
/**
* @var string
*/
2015-06-18 16:17:57 +02:00
protected $dataToInsert;
/**
* @var string
*/
protected $resultData;
/**
* @var string
*/
static $emptyResult = '{"results":[{}]}';
/**
* @var
*/
protected $mockResultSet;
2015-06-18 16:17:57 +02:00
public function setUp()
{
2015-06-23 09:19:24 +02:00
$this->mockClient = $this->getMockBuilder('\InfluxDB\Client')
2015-06-18 16:17:57 +02:00
->disableOriginalConstructor()
->getMock();
$this->resultData = file_get_contents(dirname(__FILE__) . '/result.example.json');
2015-06-18 16:17:57 +02:00
$this->mockClient->expects($this->any())
->method('getBaseURI')
->will($this->returnValue($this->equalTo('http://localhost:8086')));
$this->mockClient->expects($this->any())
->method('query')
->will($this->returnValue(new ResultSet($this->resultData)));
2015-06-18 16:17:57 +02:00
$this->db = new Database('influx_test_db', $this->mockClient);
$this->dataToInsert = file_get_contents(dirname(__FILE__) . '/input.example.json');
}
/**
*
*/
public function testQuery()
{
$testResultSet = new ResultSet($this->resultData);
$this->assertEquals($this->db->query('SELECT * FROM test_metric'), $testResultSet);
}
public function testCreateRetentionPolicy()
{
$retentionPolicy = new Database\RetentionPolicy('test', '1d', 1, true);
$mockClient = $this->getMockBuilder('\InfluxDB\Client')
->disableOriginalConstructor()
->getMock();
$mockClient->expects($this->once())
->method('query')
->will($this->returnValue(new ResultSet(self::$emptyResult)));
$database = new Database('test', $mockClient);
$this->assertEquals($database->createRetentionPolicy($retentionPolicy), new ResultSet(self::$emptyResult));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testEmptyDatabaseName()
{
new Database(null, $this->mockClient);
2015-06-18 16:17:57 +02:00
}
2015-06-19 12:04:25 +02:00
public function testWritePointsInASingleCall()
2015-06-18 16:17:57 +02:00
{
2015-06-19 09:49:58 +02:00
$point1 = new Point(
'cpu_load_short',
0.64,
array('host' => 'server01', 'region' => 'us-west'),
array('cpucount' => 10),
1435222310
2015-06-19 09:49:58 +02:00
);
2015-06-19 09:49:58 +02:00
$point2 = new Point(
'cpu_load_short',
0.84
2015-06-19 09:49:58 +02:00
);
2015-06-19 12:04:25 +02:00
$payloadExpected ="$point1\n$point2";
2015-06-19 09:49:58 +02:00
2015-06-19 12:04:25 +02:00
$this->mockClient->expects($this->once())
2015-06-22 09:44:26 +02:00
->method('write')
->with($this->equalTo($this->db->getName()), $this->equalTo($payloadExpected))
->will($this->returnValue(true));
2015-06-19 12:04:25 +02:00
$this->db->writePoints(array($point1, $point2));
2015-06-18 16:17:57 +02:00
}
}